0

I have following json pattern

{"property":[{"id":"1","name":"Property 1"},{"id":"2","name":"Property 2"}]}

How can i validate json schema? Thanks in advance.

3
  • Programatically, or will a linter such as JSONLint suffice? Commented Oct 16, 2013 at 13:38
  • github.com/justinrainbow/json-schema Commented Oct 16, 2013 at 13:38
  • Do you want to verify the syntax or the content (property names and types)? Commented Oct 16, 2013 at 13:57

1 Answer 1

1

Use json_decode($string); to convert the JSON string into native PHP. If NULL is returned the string cannot be decoded. You can then use json_last_error() to get the error code, which may be helpful.

http://www.php.net/manual/en/function.json-decode.php

http://www.php.net/manual/en/function.json-last-error.php

Sign up to request clarification or add additional context in comments.

1 Comment

For strict JSON validation, you must use is_object($result) || is_array($result). Simple values like strings or numbers are allowed by json_decode() and many other parsers but are not strictly valid JSON. For looser validation checking for NULL is fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.