Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a small JSON file with jsmn on an embedded system?

I need to parse a small JSON file on an embedded system (only 10K RAM/flash). The JSON is:

{
"data1":[1,2,3,4,5,6,7,8,9],
"data2":[
     [3,4,5,6,1],
     [8,4,5,6,1],
     [10,4,5,3,61],
     [3,4,5,6,1],
     [3,4,5,6,1],
     [3,4,5,6,1] 
]}

jsmn looks great to fit the requirement, but it's not like most JSON parsers, since it only gives you tokens. I tried, but could not figure it out.

Could someone share an example of how to parse it with jsmn?

like image 636
Vincent Zhou Avatar asked Jan 17 '13 21:01

Vincent Zhou


People also ask

What is JSMN?

jsmn (pronounced like 'jasmine') is a minimalistic JSON parser in C. It can be easily integrated into resource-limited or embedded projects. You can find more information about JSON format at json.org. Library sources are available at https://github.com/zserge/jsmn.

What are the methods to parse JSON?

parse() The JSON. parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

What is the fastest JSON parser?

We released simdjson 0.3: the fastest JSON parser in the world is even better! Last year (2019), we released the simjson library. It is a C++ library available under a liberal license (Apache) that can parse JSON documents very fast.

Can you use JSON in C?

Parsing JSON in C using microjson Developed originally for server-browser communication, the use of JSON has since expanded into a universal data interchange format. This tutorial will provide a simple introduction to parsing JSON strings in the C programming language using the microjson library.


2 Answers

You can use tiny-json. It gives you more than tokens. I have used with microcontrollers of 16-bits and 32-bits and it works fine.

char str[] = "{"
                 "\"data1\":[1,2,3,4,5,6,7,8,9],"
                 "\"data2\":["
                     "[3,4,5,6,1],"
                     "[8,4,5,6,1],"
                     "[10,4,5,3,61],"
                     "[3,4,5,6,1],"
                     "[3,4,5,6,1],"
                     "[3,4,5,6,1]"
                 "]"
             "}";
puts( str );

json_t pool[64];
json_t const* root = json_create( str, pool, 64 );

json_t const* data1 = json_getProperty( root, "data1" );
if ( data1 && JSON_ARRAY == json_getType( data1 ) ) {

    json_t const* field = json_getChild( data1 );
    while( field ) {

        if ( JSON_INTEGER == json_getType( field ) ) {
            long long int data = json_getInteger( field );
            printf("Integer from data1: %lld\n", data );
        }

        field = json_getSibling( field );
    }
}

json_t const* data2 = json_getProperty( root, "data2" );
if ( data2 && JSON_ARRAY == json_getType( data2 ) ) {

    json_t const* array = json_getChild( data2 );
    while( array ) {

        if ( JSON_ARRAY == json_getType( array ) ) {
            puts("Array in data2");

            json_t const* field = json_getChild( array );
            while( field ) {

                if ( JSON_INTEGER == json_getType( field ) ) {
                    long long int data = json_getInteger( field );
                    printf("Integer from array of data2: %lld\n", data );
                }

                field = json_getSibling( field ); 
            }
        }
        array = json_getSibling( array );
    }
} 
like image 153
Rafa Avatar answered Oct 01 '22 04:10

Rafa


I've just created jsmnRipper. This code lets you extract the tokens parsed with jsmn in a very simple way.

As an example here is the result of parsed the JSON message returned by ACRCloud

  • metadata.music[1].score:100:
  • metadata.music[1].album.name:The Best Of Depeche Mode Volume 1:
  • metadata.music[1].artists[0].name:Depeche Mode:
  • metadata.music[1].title:Enjoy The Silence (Remastered Version) (Original):

You can find the code for doing that at https://github.com/ipserc/jsmnRipper

like image 41
jose luis nuñez crespi Avatar answered Oct 01 '22 06:10

jose luis nuñez crespi