1

I have the following JSON returned from the server, and I'm trying to access to the Values (timestamp/data):

{
   "queries": [
     {
       "sample_size": 1,
       "results": [
         {
           "name": "data",
           "group_by": [
             {
               "name": "type",
               "type": "number"
             }
           ],
           "tags": {
             "hostname": [
               "host"
             ]
           },
           "values": [
             [
               1438775895302,
               143
             ]
           ]
         }
       ]
     }
   ]
 }

I am using json-c, and am using slightly modified version of the complete json parser. However I keep getting a segmentation fault (core dumped) when attempting to access the values section using json_parse_array.

How would I go about accessing the values section?

5
  • Any pointers to where the segmentation fault occurs? Have you tried valgrind to see where you might access non initialized memory (one possible reason for the seg fault) Commented Aug 5, 2015 at 15:06
  • @wiseveri From what I've found - the fault occurs here: int arraylen = json_object_array_length(jarray); /*Getting the length of the array*/ in in json_parse_array function, when it reaches the values section. Commented Aug 5, 2015 at 15:10
  • Well are you sure that the key you specified exists? That the object you're passing isn't NULL? Commented Aug 6, 2015 at 6:38
  • @wiseveri Well, isn't the key-value pair: values : [1438775895302, 143] ? As that's all I'm attempting to get access to, those two values. Commented Aug 6, 2015 at 7:36
  • 1
    No, the structure you posted has just one key: "queries". The object is an array containing one JSON-object, containing two keys... You'll have to unwrap your JSON-structure Commented Aug 6, 2015 at 7:48

1 Answer 1

1

Thanks @wiseveri for the tip, managed to access the values section with the following:

// gcc json_c_test.c -ljson-c -o json_c_test && clear && ./json_c_test

#include <json/json.h>
#include <stdio.h>

void json_parse_input( json_object *jobj )
{
    int exists, i, j, k, l;
    char *results;
    json_object *queriesObj, *resultsObj, *valuesObj, *tmpQueries, *tmpResults, *tmpValues, *tmpSeparateVals;

    /* Get query key */
    exists = json_object_object_get_ex( jobj, "queries", &queriesObj );
    if ( FALSE == exists )
    {
        printf( "\"queries\" not found in JSON\n" );
        return;
    }

    /* Loop through array of queries */
    for ( i = 0; i < json_object_array_length( queriesObj ); i++ )
    {
        tmpQueries = json_object_array_get_idx( queriesObj, i );

        /* Get results info */
        exists = json_object_object_get_ex( tmpQueries, "results", &resultsObj );
        if ( FALSE == exists )
        {
            printf( "\"results\" not found in JSON\n" );
            return;
        }

        /* Loop through array of results */
        for ( j = 0; j < json_object_array_length( resultsObj ); j++ )
        {
            tmpResults = json_object_array_get_idx ( resultsObj, j );

            /* Get values */
            exists = json_object_object_get_ex( tmpResults, "values", &valuesObj );
            if ( FALSE == exists )
            {
                printf( "\"values\" not found in JSON\n" );
                return;
            }

            /* Loop through array of array of values */
            for ( k = 0; k < json_object_array_length( valuesObj ); k++ )
            {
                tmpValues = json_object_array_get_idx ( valuesObj, k );

                /* Loop through array of values */
                for ( l = 0; l < json_object_array_length( tmpValues ); l++ )
                {
                    tmpSeparateVals = json_object_array_get_idx ( tmpValues, l );
                    printf( "Values:[%d] = %s \n", l, json_object_to_json_string( tmpSeparateVals ) );
                }
            }
        }
    }
}

int main()
{
    json_object *jobj;

    char * string = " { \"queries\" : [ { \"sample_size\" : 1, \"results\" : [ { \"name\" : \"data\", \"group_by\" : [{ \"name\" : \"type\", \"type\" : \"number\" }], \"tags\" : { \"hostname\" : [ \"host\" ]}, \"values\": [[1438775895302, 143]] } ], } ] } ";
    printf ( "JSON string: %s\n\n", string );

    jobj = json_tokener_parse( string );
    json_parse_input( jobj );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.