Hello all,
I'd like to use json_unpack to extract foo's value for both of the below JSON data. Problem is, I don't know a format string that will handle both cases. Please help me know the best approach - thanks.
{"foo": "foostr"} - works with format string "s:s" - fails with "s:n"
{"foo": null} - works with format string "s:n" - fails with "s:s"
I was hoping there is some format string which combines 's' and 'n' so I can get "foostr" for the 1st case and a NULL pointer returned for 2nd case. But, I couldn't find it.
From my online searching, it seems one option is to avoid json_unpack if string and null are possible values, and instead manually extract each field (see below), but I'm wondering for a simpler approach since there are many JSON variables to handle.
Here's my current option (pardon my pseudocode):
json_t * pObj = json_object_get (root, "foo");
if (!pObj) {return ERROR};
if (json_is_null (pObj) {return NULL;}
else if (json_is_string(pObj)) {return json_string_value(pObj);}
else {return ERROR;}
Thanks for your help.
Best regards,
Shaun