# JSON on MangOH from file

**URL:** <https://mangoh.discourse.group/t/json-on-mangoh-from-file/204>\
**Category:** Uncategorized\
**Created:** [September 1, 2016, 2:33pm UTC](https://mangoh.discourse.group/t/json-on-mangoh-from-file/204 "2016-09-01T14:33:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brookz](https://avatars.discourse-cdn.com/v4/letter/b/f4b2a3/32.png) [@Brookz](https://mangoh.discourse.group/u/Brookz)\
**Post date:** [September 1, 2016, 2:33pm UTC](https://mangoh.discourse.group/t/json-on-mangoh-from-file/204/1 "2016-09-01T14:33:36Z")

</div>

Hi,

I’m trying to read/parse a json document that i’ve download to the mangOH, does someone have an example of this for legato?

Also, which fd i’m supposed to use?

Thanks,

Pau

---

<div class="post-metadata">

**Author:** ![dfrey](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/dfrey/32/303_2.png) [@dfrey](https://mangoh.discourse.group/u/dfrey)\
**Post date:** [September 1, 2016, 8:02pm UTC](https://mangoh.discourse.group/t/json-on-mangoh-from-file/204/2 "2016-09-01T20:02:05Z")

</div>

I have written a simple Legato app which parses a JSON string and prints out the content. You can see from the filenames below that the app is named “JsonReadWrite”. I didn’t bother implementing the writing part, but it should be fairly straightforward to figure out if you follow the [jansson documentation](https://jansson.readthedocs.io).

## JsonReadWrite/jsonReadWrite.adef

```auto
version: 0.1.0
sandboxed: true
start: manual

executables:
{
    jsonReadWrite = (jsonReadWrite)
}

processes:
{
    run:
    {
        (jsonReadWrite)
    }
}

```

## JsonReadWrite/jsonReadWrite/Component.cdef

```auto
cflags:
{
    -I$LEGATO_BUILD/framework/libjansson/include
}

ldflags:
{
    -ljansson
}

requires:
{
    file:
    {
        /legato/systems/current/lib/libjansson.so /lib/
        /legato/systems/current/lib/libjansson.so.4 /lib/
        /legato/systems/current/lib/libjansson.so.4.6.0 /lib/
    }
}

sources:
{
    main.c
}

```

## JsonReadWrite/jsonReadWrite/main.c

```auto
#include <jansson.h>
#include "legato.h"

const char jsonText[] =
    "{\"i2cDevices\":[{\"name\":\"eeprom\",\"bus\":0,\"address\":64},{\"name\":\"temperature sensor\",\"bus\":1,\"address\":20}]}";

COMPONENT_INIT
{
    json_error_t jsonError;
    json_t* root = json_loads(jsonText, 0, &jsonError);
    // NOTE: To read from a file instead, use:
    // json_t* root = json_load_file("/path/to/file.json", 0, &jsonError);
    LE_FATAL_IF(!root, "JSON decoding error on line (%d): %s", jsonError.line, jsonError.text);

    LE_FATAL_IF(!json_is_object(root), "JSON is not an object");

    json_t* i2cDevices = json_object_get(root, "i2cDevices");
    LE_FATAL_IF(!i2cDevices, "Couldn't get i2cDevices object from JSON");
    LE_FATAL_IF(!json_is_array(i2cDevices), "i2cDevices is not an array");

    size_t index;
    json_t* value;
    json_array_foreach(i2cDevices, index, value)
    {
        LE_FATAL_IF(!json_is_object(value), "i2cDevices index %d is not an object", index);

        json_t* i2cName = json_object_get(value, "name");
        LE_FATAL_IF(
            !i2cName || !json_is_string(i2cName), "name either doesn't exist or is not a string");
        const char* name = json_string_value(i2cName);

        json_t* i2cBus = json_object_get(value, "bus");
        LE_FATAL_IF(
            !i2cBus || !json_is_integer(i2cBus), "bus either doesn't exist or is not an integer");
        const json_int_t bus = json_integer_value(i2cBus);

        json_t* i2cAddress = json_object_get(value, "address");
        LE_FATAL_IF(
            !i2cAddress || !json_is_integer(i2cAddress),
            "address either doesn't exist or is not an integer");
        const json_int_t address = json_integer_value(i2cAddress);

        LE_INFO("I2C device #%d: name=\"%s\", bus=%lld, address=%lld", index, name, bus, address);
    }

    // Reduce the reference count of the root object which should cause the memory to be freed
    json_decref(root);
}

```

---

<div class="post-metadata">

**Author:** ![Madan](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@Madan](https://mangoh.discourse.group/u/Madan)\
**Post date:** [November 17, 2018, 6:36am UTC](https://mangoh.discourse.group/t/json-on-mangoh-from-file/204/4 "2018-11-17T06:36:26Z")

</div>

Hello,

I am trying to use this sample for may application for some app configurations/settings.  
Getting couple errors -  
`Failed to create link at '/lib/libjansson.so.4' in app 'jsonreader`  
when I comment the `libjansson.so.4` in .adef, getting the other error.  
`Failed to create link at '/lib/libjansson.so.4' in app 'jsonreader`

Searched in mangOH and Legato folders, but couldnt find these files.  
Let me know where to get them or any other workaround?

Thanks  
Madan

---

<div class="post-metadata">

**Author:** ![Madan](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@Madan](https://mangoh.discourse.group/u/Madan)\
**Post date:** [November 19, 2018, 2:46pm UTC](https://mangoh.discourse.group/t/json-on-mangoh-from-file/204/5 "2018-11-19T14:46:24Z")

</div>

Commenting these did the trick. Seems there’s no dependency on these libraries, for now.  
`/legato/systems/current/lib/libjansson.so.4 /lib/ /legato/systems/current/lib/libjansson.so.4.6.0 /lib/`

Thanks.
