Kiran Budhe
Kiran Budhe

Reputation: 21

How to parse JSON response in C Linux?

I have used many tools such as cJSON, nxjson and jsmn parsers to parse the JSON response but none of the tools which i had used is giving the output in some structure format. Below is my JSON response in string:

{"Code":1,"MSN":0,"HWID":7001,"Data":{"SignOffRequest":{"messageId":0,"barCodeReadErrorCnt":0,"markSenseReadErrorCnt":0,"markSenseValidationErrorCnt":0,"postPrintErrorCnt":0,"custTicketFeedErrorCnt":0,"custInputTicketJamsCnt":0,"keyStrokeCnt":0,"keyStrokeErrorCnt":0,"commCrcErrorCnt":0,"readTxnCnt":0,"keyedTxnCnt":0,"ticketMotionErrorCnt":0,"blankFeedErrorCnt":0,"blankTicketJamCnt":0,"startupNormalRespCnt":0,"startupErrorRespCnt":0,"primCommMesgSentCnt":0,"commRetransmitTxnCnt":0,"rawMessage":null,"TableUpdateNo":1,"FixtureUpdateNo":0}}}

I have used cJSON tool and the output is as below which is in also a string:

{
    "Code": 1,
    "MSN":  0,
    "HWID": 7001,
    "Data": {
        "SignOffRequest":   {
            "messageId":    0,
            "barCodeReadErrorCnt":  0,
            "markSenseReadErrorCnt":    0,
            "markSenseValidationErrorCnt":  0,
            "postPrintErrorCnt":    0,
            "custTicketFeedErrorCnt":   0,
            "custInputTicketJamsCnt":   0,
            "keyStrokeCnt": 0,
            "keyStrokeErrorCnt":    0,
            "commCrcErrorCnt":  0,
            "readTxnCnt":   0,
            "keyedTxnCnt":  0,
            "ticketMotionErrorCnt": 0,
            "blankFeedErrorCnt":    0,
            "blankTicketJamCnt":    0,
            "startupNormalRespCnt": 0,
            "startupErrorRespCnt":  0,
            "primCommMesgSentCnt":  0,
            "commRetransmitTxnCnt": 0,
            "rawMessage":   null,
            "TableUpdateNo":    1,
            "FixtureUpdateNo":  0
        }
    }
}

but I don't want the output in the above format. I want the output in the form of a C structure. Is it possible to get the output in C structure?

Upvotes: 2

Views: 610

Answers (1)

You need to add explicit code extracting from parsed JSON values the relevant fields, etc... This cannot be magically automated in general.

Some JSON libraries slightly facilitate this task. For instance jansson has a quite useful json_unpack function with which you could extract (in a single call) some fields from a parsed JSON value.

But it is your responsability to code the extraction and the validation of information from the JSON value, because only you can know what that JSON really means.

JSON is simply a convenient textual serialization format. It is up to you to give actual meaning to the data. It is also up to you to decide what kind of validation you want to code (at what degree do you trust the emitter of that JSON data?). If the data is coming from the Internet (e.g. AJAX queries, etc...) you should trust it as less as possible and validate it as much as possible.

Don't forget to document the meaning of the JSON data.

Upvotes: 4

Related Questions