InvalidJSONException ... and now ?

Hi,

I’m trying to open a JSON file that is malformed, as I get a invalidjsonexception error.
the error message is “malformed json file”

and now, what can i do ?
what are the tools that could give me the line where the error is ? the first line with an error ?
does these tools even exist ?

thanks for any tips !

PS: same question with XML exception…

XMLException should tell you the line number unless it was unable to begin parsing.

[quote=350791:@Jean-Yves Pochez]and now, what can i do ?
what are the tools that could give me the line where the error is ? the first line with an error ?
does these tools even exist ?[/quote]

It isn’t clear to me if by “tools” you mean things you can do programatically inside your code, or if you mean generically to help determine why a given JSON is failing.

For generic, manual review my preference is JSON lint but web searches will yield plenty of online validators for both JSON and XML.

I’m using JSON Wizard (http://www.insili.co.uk/apps/json-wizard/) in order to “beautify” JSON and/or to inspect malformed strings.

json-wizard opens the file with NO error … :wink:

I recently had a case where a PHP page using PHP’s json_decode() function failed because I had neglected to ensure the JSON string passed to me was properly UTF-8 encoded. There was one byte in it with a non-ASCII character. I encoded the JSON that I received as UTF-8 and then PHP was able to parse it properly.

So if you are not seeing other online validation errors, you may want to check your text encoding.

it’s a local text file I have, coming from a database export. I will try your encoding suggestion. thanks.

I now have another error : lexical error: invalid char in json text.
json-wizard still opens it with no error.

[quote=350832:@Jean-Yves Pochez]I now have another error : lexical error: invalid char in json text.
json-wizard still opens it with no error.[/quote]

I believe the JSON standard calls for UTF-8.

You could try another online JSON validator to see if it tells you WHICH characters is invalid. Do a web search on JSON online validator and you should find many free sites. Maybe one is smart enough to identify the problem character.

well … well … in fact json-wizard indicates a small red dot at the bottom, there is a non-json chars at the beginning and the end of the text. removed them and voil. the json is now parsed ok in xojo.
thanks to all.