1

I have a string in json form which I decode to a php variable. But if the json is not a valid json string, the decode gives a NULL.

I'm looking for a script which can tell me, on which part the decode will fail.

For example I have the following string:

{ "error": "OK", "pathlist": [ { "path": "datu5" "pathId": "10100010" }, { "path": "datum", "pathId": "10100011" } ] }

Then I want to show that the error is an error near line 4: For example:

Error: Parse error on line 4: ... "path" : "datu5" "pathId" : "10100010 ----------------------^ Expecting 'EOF', '}', ':', ',', ']', got 'STRING'

I there such a php script?

3
  • You forgot coma "path": "datu5", "pathId": "10100010" batwan datu5 and pathId Commented Feb 29, 2016 at 14:46
  • Naumov, I know, this is an example of a wrong json. Commented Feb 29, 2016 at 15:03
  • sorry I don't finish understand Commented Feb 29, 2016 at 15:05

1 Answer 1

1

Try this lib: https://github.com/Seldaek/jsonlint

Usage

use Seld\JsonLint\JsonParser;

$parser = new JsonParser();

// returns null if it's valid json, or a ParsingException object.
$parser->lint($json);

// Call getMessage() on the exception object to get
// a well formatted error message error like this

// Parse error on line 2:
// ... "key": "value"    "numbers": [1, 2, 3]
// ----------------------^
// Expected one of: 'EOF', '}', ':', ',', ']'

// Call getDetails() on the exception to get more info.

// returns parsed json, like json_decode() does, but slower, throws
// exceptions on failure.
$parser->parse($json);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.