Easy access to nested Objects

218 views
Skip to first unread message

Anemonenmoni

unread,
Mar 2, 2021, 3:19:46 PM3/2/21
to json-c

Hello!

I have a problem and I cannot see a good way to handle it with json-c. I have a JSON document. I want to read data from the JSON document (primitiv and/or object), depending on a path i get where the keys to this information are connected with dots.
For example:
The path I get: example.anotherObject.title
JSON document:
    {
        "example":{
            "title":"example JSON",
            "anotherObject":{
                "title":"blueberry"
            }
        }
    }

I dont know how many elements the path i get has.
Do I need to step through every layer with json_object_object_get_ex or is there an easier way?

Thank you

Simone

Eric Hawicz

unread,
Mar 2, 2021, 6:42:44 PM3/2/21
to jso...@googlegroups.com
On Tue, Mar 2, 2021, 3:19 PM Anemonenmoni <ma...@simoneesch.de> wrote:
I have a problem and I cannot see a good way to handle it with json-c. I have a JSON document. I want to read data from the JSON document (primitiv and/or object), depending on a path i get where the keys to this information are connected with dots.
For example:
The path I get: example.anotherObject.title
...
I dont know how many elements the path i get has.
Do I need to step through every layer with json_object_object_get_ex or is there an easier way?

Can you use RFC6901 (https://tools.ietf.org/html/rfc6901) path expressions?  If yes, have you seen json_pointer.h (http://json-c.github.io/json-c/json-c-0.15/doc/html/json__pointer_8h.html) ?

If that doesn't work, and your path expressions are simple lists of dot separated keys (i.e. no need to handle arrays) you could probably put something together pretty easily by splitting it into parts and using json_c_visit (http://json-c.github.io/json-c/json-c-0.15/doc/html/json__visit_8h.html) to walk the tree.

Eric

Anemonenmoni

unread,
Mar 3, 2021, 5:28:25 AM3/3/21
to json-c
Hello Eric,
thank you for your answer!

Yes my path will be a simple list of dot separated keys, I had a look at json_c_visit and the unit test of this function and it seems promissing to me. But still I have issues to understand how it works. Are there further examples available for the usage of json_c_visit?

Thank you and have a nice day

Simone

Eric Hawicz

unread,
Mar 11, 2021, 10:06:09 PM3/11/21
to jso...@googlegroups.com
I don't really have an examples to point you at, but I think briefly what you'll want to do is:

split the input on dots
create a "params" structure to pass in to json_c_visit to be passed as the userarg argument that includes:
   the split input as an array of char *'s.
   the number of components of that split input
   the depth that you're currently at
   a json_object *result for passing back the final result, if any
write a handler function that, on each call,
  checks whether you're handling a field in an object whose key matches the input component at the current depth.
  If it doesn't, or it's not an object field at all, return JSON_C_VISIT_RETURN_SKIP.
  If it does match, set params->depth++ and return JSON_C_VISIT_RETURN_CONTINUE.
  When you reach the end of your split input, set that jso in the params structure and return JSON_C_VISIT_RETURN_STOP.
That's just a rough outline, and there are a number of details you'd need to figure out especially with respect to what to do when you _don't_ find a matching object field, and handling the 1st & 2nd visits of each json_type_object, etc...

Eric

--
You received this message because you are subscribed to the Google Groups "json-c" group.
To unsubscribe from this group and stop receiving emails from it, send an email to json-c+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/json-c/bbeb6ea1-d2d4-416e-a986-47823a6c5e5fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages