Issue #3 resolved

how to access the json elements after they are parsed

Anonymous created an issue

Would you be kind enough to help me understand how to access json element's values. There seem no to be a good or limited documentation for the parser. Thank you

Comments (5)

  1. Serge Zaitsev repo owner

    Some kind of documentation is on my bitbucket "site" at I hope it gives you a brief idea of how jsmn works. I believe, I should add better examples for this project. In a few words, every token has a type and two fields - start/end. The fields are token start/end positions inside JSON string. So, you can do something like:

    strncpy(buf, js + token->start, token->end - token->start)

    I prefer wrapping it into a mkstr() function that takes JSON string and a token as arguements, does strncpy() to a static buffer and returns it. Then I can do things like:

    if (strcmp(mkstr(js, token), "status") == 0) { /* found "status" token */
  2. Anonymous

    so, assuming you have a json object like this: {status:"yes", params: {param1: 100, param2: 393}}

    How can you possibly retrieve the value of 'status' which is "yes", with the given example?

    I believe you should provide comprehensive documentation and how-tos, there are people willing to use and contribute to the work you have done but it feels like a dead end as far as documentation is concerned.

    Thank you.

  3. Anonymous


    I think this is not so hard to understand if you read the code. Here is a simple function I wrote to retrieve the value of a string (that itself might be embedded in another object).

    277 static int get_jsmn_val_idx(unsigned char* buffer, jsmn_parser *p, jsmntok_t *toks, const char **path) {
    278         int i = 0;
    279         int path_idx = 0;
    281         for(i = 0; i < p->toknext; i++) {
    282                 if(TOKEN_STRING(buffer, toks[i], path[path_idx])) {
    283                         if(path[path_idx+1] == NULL)
    284                                 return i+1;
    285                         path_idx++;
    286                 }
    287         }
    289         return -1;
    290 }

    The call looks like this:

    319         // we want the value of the string compaction_enabled which is embedded in the object compaction_settings
    320         const char* path[] = { "COMPACTION_SETTINGS", "COMPACTION_ENABLED", NULL };
    322         // lookup the index of the value for string COMPACTION_ENABLED
    323         int rv = get_jsmn_val_idx(buffer, &p, toks, path);
    325         // print the value
    326         for(i = toks[rv].start; i < toks[rv].end; i++) {
    327                 putchar(buffer[i]);
    328         }
    329         putchar('\n');

    Of course I left out error checking in the caller, but the principle is there. If you are trying to get an array value or object value that can similarly be accomplished by retrieving the appropriate index. When retrieving an array, you can determine the array members automatically by inspecting the .size element of the array jsmntok_t array. (the array members will be the next 'size' consecutive indices in the array of jsmntok_t.

    Hope this helps. Examples above can certainly be improved upon. :)

  4. Log in to comment