Validating JSON syntax

180 views
Skip to first unread message

Pi

unread,
Aug 10, 2011, 7:24:47 AM8/10/11
to json-c
Hello,

I'm new to json-c and I'm trying to detect when the JSON message is
now a valid JSON format or not in well formed JSON syntax.

I'm using the function json_tokener_parse, so when the syntax is
incorrect the returned object should be not valid.

Can you please explain how can I detect it?

Thank you

Eric Haszlakiewicz

unread,
Aug 10, 2011, 2:10:47 PM8/10/11
to jso...@googlegroups.com

What do you get when you try it?  I believe that it'll just return NULL when it can't parse the json message.  Do you get something else?

If you need more detail about where the error happened you can use json_tokener_parse_ex() and the char_offset field in the json_tokener object. 
Also, there's a json_tokener_get_stack() function to generate a friendlier "path" within the message where the error happened.

Eric

Message has been deleted

nags

unread,
Nov 20, 2012, 1:33:15 AM11/20/12
to jso...@googlegroups.com
Hi Eric,

Can you please explore about using the char_offset field and json_tokener_get_stack() function?

Thanks.

Eric Haszlakiewicz

unread,
Nov 20, 2012, 1:35:47 PM11/20/12
to jso...@googlegroups.com
er.. oops, I think I forgot to add the json_tokener_get_stack
function. I'll put that on my todo list.

As for the char_offset field, have you looked at the docs for the
json_tokener_parse_ex function?
http://json-c.github.com/json-c/json-c-0.10/doc/html/json__tokener_8h.html#a0d9a666c21879647e8831f9cfa691673

Basically, the char_offset field (which at some point will be replaced
with an accessor function) tells you where the parser stopped, so if
there's a problem with the input you'll have a clue as to what's wrong
with it.

Eric

nags

unread,
Nov 21, 2012, 12:54:00 AM11/21/12
to jso...@googlegroups.com
Really helpful.
Thanks Eric :-)
Reply all
Reply to author
Forward
0 new messages