RE: [jansson-users] JSON formatted file parsing.

1,246 views
Skip to first unread message

Petri Lehtinen

unread,
Jan 20, 2013, 5:39:08 AM1/20/13
to jansso...@googlegroups.com
Hi.

You can use the json_object_foreach macro to iterate over all key-value pairs of an object:

Hope this help.

Petri



Alan Lewis <alanl...@gmail.com> kirjoitti:


Hi,
I'm a new Jansson user, so forgive me if this question is stupid!
I am parsing a JSON formatted file, that contains an array of objects associated with a "key". I can locate the key, but I then want to iterate over the objects in the array that follow the key. Then I want to compare the (string) key value to see if it matches. If it does, I then need to get the values. Here's a sample of the file......

{ ...
  REST OF THE FILE LEFT OUT FOR BREVITY. IT IS CORRECTLY FORMATTED
  "domain" : [
  { "EN" : "http://www.awebURL.com", "action" : [{"lostpassword" : "myaccount.aspx"}, {"support" : "support.aspx"}] },
  { "GR" : "http://gr.somegreekURL.com" },
  { "ES" : "http://www.someotherURL.es" }
  ],
  "action" : [
  { "lostpassword" : "myaccount.aspx" },
  { "changepassword" : "myaccount.aspx" },
....]
}


I can find the object "domain", I then want to compare the object 'keys' within the array to find the one being sought.  How can I check for the key string in the "domain" array objects?

Sample code..... (again, not all the file is here for brevity)
 
   json_t *dom = json_object_get(root, "domain");
 
     if ( json_is_array(dom) ) {
        for ( int i = 0; i < json_array_size(dom); i++) {
           country = json_array_get(dom, i); // HERE I AM TRYING TO RETRIEVE THE "key" STRING TO COMPARE??
           if ( json_is_object(country)) {
              if ( 0 = strcmp("EN", country_key_value() ) {  // this is the logic I would like to implement!!
                     url = json_string_value(country);
                     fprintf(stdout, "We found country %s; ", url;
              } // inner if
           }  // if
        } // loop
     } // outer if


I hope this makes some sense. I can't see how to achieve this from the Jansson documentation. The documentation seems to lean more on generating JSON than parsing.

Thanks for any help.

Alan

--
Jansson users mailing list
jansso...@googlegroups.com
http://groups.google.com/group/jansson-users

Alan Lewis

unread,
Jan 23, 2013, 10:38:33 AM1/23/13
to jansso...@googlegroups.com
Hi Petri,

Thank you for your reply to my question.

I have tried the json_object_foreach() loop, but it does not seem to work. The json_t * passed as the first argument is the array, but even when the first statement inside the loop is an fprintf ( stderr, ...) nothing is displayed. Is the json_object_foreach() loop intending for traversing an array of json objects?  Or, as it's more likely I am using it incorrectly, can you provide a tested example of using the loop to iterate over an array of objects, testing for the value for the key in each object?

Many thanks,
Alan

Petri Lehtinen

unread,
Jan 23, 2013, 2:31:21 PM1/23/13
to jansso...@googlegroups.com
Alan Lewis wrote:
> I have tried the json_object_foreach() loop, but it does not seem to work. The
> json_t * passed as the first argument is the array, but even when the first
> statement inside the loop is an fprintf ( stderr, ...) nothing is displayed. Is
> the json_object_foreach() loop intending for traversing an array of json
> objects? Or, as it's more likely I am using it incorrectly, can you provide a
> tested example of using the loop to iterate over an array of objects, testing
> for the value for the key in each object?

json_object_foreach() can be used to iterate over all key-value pairs
of an object. If used with an array, it doesn't work correctly.

Here's an example of what I think you're trying to do:

size_t i;
json_t *array; /* this holds the array of objects */

for(i = 0; i < json_array_size(array); i++) {
const char *key;
json_t *value;
json_t *object = json_array_get(array, i);

json_object_foreach(object, key, value) {
if(!strcmp(key, "my key")) {
printf("found %s\n", json_string_value(value));
break;
}
}
}

Petri

Alan Lewis

unread,
Feb 13, 2013, 11:38:52 AM2/13/13
to jansso...@googlegroups.com
Hi Petri,

Thanks for the reply to my earlier enquiry. However, I cannot get Jansson to work in the way I need to. I've been trying to get this to work for nearly 2 weeks, (in between other tasks). I have an array of objects that can be reached with the json_object_get(json_t *doc, "KEY"); then, I want to perform another object get on the contents of the (found) array. None of the methods I've tried work. I have tried json_object_foreach(), json_object_iter(), json_object_get(), and all permutations thereof. It seems that your library does not work when an array contains object type elements; especially when those objects can contain multiple "key" : "value" pairs.

Can you offer any suggestion for how Jansson can work with the structure desribed?


Regards,
Alan


On Sunday, 20 January 2013 10:39:08 UTC, Petri Lehtinen wrote:

Alan Lewis

unread,
Feb 13, 2013, 11:54:01 AM2/13/13
to jansso...@googlegroups.com
Hi Petri,

Sorry, I think I was posting a reply almost at the same time you were following up your earlier reply.  I tried it the way you suggested in your sample code, and it now works. Many thanks.

Regards,
Alan

Graeme Smecher

unread,
Feb 13, 2013, 11:56:40 AM2/13/13
to jansso...@googlegroups.com
Hi Alan,

If you haven't yet, please take a look at the test suites in "jansson/test/suites/api". (You can browse it on-line at https://github.com/akheron/jansson/tree/master/test/suites/api) For example, test_object.c contains code that demonstrates the use of json_object_foreach, json_object_iter, and json_object_get.

Otherwise, can you post some simple, hopefully self-contained code that demonstrates the problem? There are plenty of lurkers on this mailing list who are happy to jump in and help with debugging, but it's hard to do without code to inspect.

best,
Graeme
--
---
You received this message because you are subscribed to the Google Groups "Jansson users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jansson-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages