# JSON Parser and Generator in Device OS

**URL:** <https://community.particle.io/t/json-parser-and-generator-in-device-os/56147>\
**Category:** Firmware\
**Created:** [May 14, 2020, 1:07pm UTC](https://community.particle.io/t/json-parser-and-generator-in-device-os/56147 "2020-05-14T13:07:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [May 14, 2020, 1:07pm UTC](https://community.particle.io/t/json-parser-and-generator-in-device-os/56147/1 "2020-05-14T13:07:51Z")

</div>

Did you know that there has been a JSON parser and generator built into Device OS since 0.6.1? It’s now documented!

Instead of having to do this:

```cpp
int a = 123;
bool b = true;
const char *c = "testing";

snprintf(buf, sizeof(buf), "{\"a\":%d,\"b\":%s,\"c\":\"%s\"}", 
    a, 
    b ? "true" : "false",
    c);

```

You can now do this, which is much more readable and only adds 240 bytes of code.

```cpp
JSONBufferWriter writer(buf, sizeof(buf) - 1);
writer.beginObject();
    writer.name("a").value(a);
    writer.name("b").value(b);
    writer.name("c").value(c);
writer.endObject();
writer.buffer()[std::min(writer.bufferSize(), writer.dataSize())] = 0;

```

Documentation:

[https://docs.particle.io/reference/device-os/firmware/#json](https://docs.particle.io/reference/device-os/firmware/#json)

---

<div class="post-metadata">

**Author:** ![TrikkStar](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@TrikkStar](https://community.particle.io/u/TrikkStar)\
**Post date:** [May 15, 2020, 5:09pm UTC](https://community.particle.io/t/json-parser-and-generator-in-device-os/56147/2 "2020-05-15T17:09:58Z")

</div>

Will be looking into this further, but could you possibly give some advantages/disadvantages of using the built in library vs your own [JsonParserGeneratorRK](https://github.com/rickkas7/JsonParserGeneratorRK) library?

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [May 15, 2020, 5:13pm UTC](https://community.particle.io/t/json-parser-and-generator-in-device-os/56147/3 "2020-05-15T17:13:57Z")

</div>

The biggest advantage of the built-in one is code size, since the JSMN parser is built into Device OS, not user firmware.

JsonParserGeneratorRK has some additional features that may make continuing to use it worthwhile if you are not short on code space:

- Easy access to object values by key name
- Easy access to array values by index
- Fluent accessors for nested objects and arrays
- Unicode UTF-8 support

---

<div class="post-metadata">

**Author:** ![gusgonnet](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/gusgonnet/32/27491_2.png) [@gusgonnet](https://community.particle.io/u/gusgonnet)\
**Post date:** [May 15, 2020, 5:49pm UTC](https://community.particle.io/t/json-parser-and-generator-in-device-os/56147/4 "2020-05-15T17:49:30Z")

</div>

MAGICAL! no more errors like escaping this double quote here or there.  
Thanks Rick

---

<div class="post-metadata">

**Author:** ![gusgonnet](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/gusgonnet/32/27491_2.png) [@gusgonnet](https://community.particle.io/u/gusgonnet)\
**Post date:** [May 15, 2020, 5:51pm UTC](https://community.particle.io/t/json-parser-and-generator-in-device-os/56147/5 "2020-05-15T17:51:03Z")

</div>

in order to bring more awareness to it, do you guys think it can make it to the next release notes?  
@rickkas7 @avtolstoy  
thanks

---

<div class="post-metadata">

**Author:** ![robc](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@robc](https://community.particle.io/u/robc)\
**Post date:** [May 18, 2020, 1:06pm UTC](https://community.particle.io/t/json-parser-and-generator-in-device-os/56147/6 "2020-05-18T13:06:06Z")

</div>

Very nice. Works great, thanks!

---

<div class="post-metadata">

**Author:** ![mawrob](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mawrob/32/8209_2.png) [@mawrob](https://community.particle.io/u/mawrob)\
**Post date:** [October 6, 2020, 6:50pm UTC](https://community.particle.io/t/json-parser-and-generator-in-device-os/56147/7 "2020-10-06T18:50:52Z")

</div>

This is great thanks. I had not noticed it.

Could you provide an example of creating a nested structure such as:

```auto
{
   "f":
       {
           "g":"Call me \"John\"",
           "h":-0.78
       }
}

```

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [October 6, 2020, 7:04pm UTC](https://community.particle.io/t/json-parser-and-generator-in-device-os/56147/8 "2020-10-06T19:04:09Z")

</div>

```cpp
JsonWriterStatic<256> jw;

jw.startObject(); // Start outer object

jw.setFloatPlaces(2);

jw.insertKeyObject("f"); // Start inner object 
	jw.insertKeyValue("g", "Call me \"John\"");
	jw.insertKeyValue("h", -.78);
jw.finishObjectOrArray(); // End inner object

jw.finishObjectOrArray(); // End outer object

```

The JSON created looks like:

```json
{"f":{"g":"Call me \"John\"","h":-0.78}}

```

And if you run it through JSONLint in looks good:

 ![Screen Shot 2020-10-06 at 3.03.34 PM](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/6/e/6ef083dbcf7b0b8819da59fe1ce521560e455a1d.png)

---

<div class="post-metadata">

**Author:** ![mawrob](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mawrob/32/8209_2.png) [@mawrob](https://community.particle.io/u/mawrob)\
**Post date:** [October 7, 2020, 12:24pm UTC](https://community.particle.io/t/json-parser-and-generator-in-device-os/56147/9 "2020-10-07T12:24:48Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [April 8, 2021, 12:24am UTC](https://community.particle.io/t/json-parser-and-generator-in-device-os/56147/10 "2021-04-08T00:24:53Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
