JSON Library?

There is a json lib here that my colleage wrote for chipKIT (Microchip PIC32 Arduino Compatible boards). It may work with Arduino. At its core it makes calls to Arduino libs used in chipKIT. I believe it does make use of malloc, which from my understanding does not work so great on Arduino. Its maintained and used heavily but not documented.

Jacob

I don't know if this is something you still need, but aJson seems to be available on Github.
See the project homepage here: http://interactive-matter.eu/how-to/ajson-arduino-json-library/

Regards
Andrea

For some reasons, the Github link from the page I've linked before gives an empty repository.
You can find the aJson library here:

Regards
Andrea

I was not satisfied with "aJson" and "json-arduino" because both of them rely on malloc() and therefore doesn't perform well in very low memory conditions.

So I wrote my own "malloc-free JSON parser":

Hello,
i try your library but the parsing not extract the name the age etc etc.

alfio76:
Hello,
i try your library but the parsing not extract the name the age etc etc.

Fail. You're going to have to do much better than that. It doesn't extract what information from what? Be very specific!

Hey BenoitB - thanks for making that library!

I've tried running the example in the jsonparser library on a Teensy 3.0 and Teensy 3.1, however, and can't get the ParseAnObject() subroutine to complete successfully (this calls the hashTable.getString, hashTable.getArray... or other hashTable commands). The array.getArray command works fine, and the ParseAnArray() subroutine runs fine. I have managed to get ParseAnObject() subroutine to successfully complete on an Arduino Uno and Teensy 2.0, however, so it must be some compatability issue with 3.0 and 3.1 (ARM perhaps??).

There's no compile errors, but the program simply stops at line 25 when it runs .getString for the first time.

char* name = hashTable.getString("Name");

So, to be clear and perhaps too obvious, the output looks like this:

Parse {"Name":"Blanchon","Skills":["C","C++","C#"],"Age":32,"Online":true}

I tried commenting out getString and attempting the getArray and getLong and they also do not work. I also tried explicitely adding the following libraries to the top of the file...

#include <JsonParser.h>
#include "utility/jsmn.h"
#include <stdlib.h> // for strtol, strtod
#include <string.h> // for strcmp()
#include "JsonObjectBase.h"
#include "JsonHashTable.h"
#include "JsonArray.h"

seems silly but I've had previous examples (Adafruit's sensor library) not compile without explicitely adding the AdafruitSensor.h library to the top of my .ino file (even though it was referenced in the library .h file...). Anyway, that didn't work either.

Any ideas?

Greg

BenoitB:
I was not satisfied with "aJson" and "json-arduino" because both of them rely on malloc() and therefore doesn't perform well in very low memory conditions.

So I wrote my own "malloc-free JSON parser":
GitHub - bblanchon/ArduinoJson: 📟 JSON library for Arduino and embedded C++. Simple and efficient.

I tried the first two before trying your library. I always ran into memory problem after a while. I have no such problem so far with your library. It is working perfectly with my web services.

Thanks Benoit for a very well polished product!

Jean-Francois.

I'm having an issue pulling all of the tokens from a JSON - I can get some of them, but not others. I'm using a Teensy 3.1 with 64K SRAM.

Here's two JSONs I've tested. In both case, the last few tokens are not parsed. Thinking it was a problem with the length of the JSON, I tried the 2nd JSON which is <32 tokens, so it's fairly small but still has the same issue:

-- does not parse measlights, measlights2, altc, and altd

{"protocol_name":"fluorescence","repeats":1,"wait":0,"averages":1,"measurements":3,"meas2_light":15,"meas1_baseline":0,"act_light":20,"pulsesize":25,"pulsedistance":10000,"actintensity1":50,"actintensity2":255,"measintensity":255,"calintensity":255,"pulses":[50,50,50],"act":[2,1,2,2],"red":[2,2,2,2],"detectors":[[34,34,34,34],[34,34,34,34],[34,34,34,34],[34,34,34,34]],"alta":[2,2,2,2],"altb":[2,2,2,2],"measlights":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]],"measlights2":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]],"altc":[2,2,2,2],"altd":[2,2,2,2]}

-- does not parse altc and altd

{"measlights":[[15],[15],[15],[15]],"end":[[15],[15],[15],[15]],"alta":[2,2],"altb":[2,2],"altc":[2,2],"altd":[2,2]}

I can't seem to get those last few values to be parsed correctly. And it's always the last few values - if you switch the order of the tokens, it's always the ones at the end which aren't parsed. It makes me think it's a memory issue, but I know that's not the case, the program is small and there's plenty of memory left. I've messed around with lots of things, like changing the JsonParser size, changing the varaible names, moving them around within the JSON itself, calling the values in a different way... but haven't been able to figure it out.

Any suggestions, ideas, or similar problems?

See attached for the code I used to test. Just enter any number followed by the JSON in the serial com window to parse (one of the above 2). It tries to find the variables it's looking for, it prints their values if they are found.

json_parser_testing.ino (14.6 KB)

Hi gbathree,

Sorry for making you wait so long.

About your first post.
There was a mistake in the sample code.
You need to replace char* json by char json[].
It was generating an access violation because the memory was write protected and the parser needs to alter the string.
I didn't see that on Arduino because there is no memory protection.

About your second post.
You are right: there was a bug in the parser's code.
The bug occurred as soon as the JSON string contains a multi-dimensional array.
I fixed it in the master branch, you can download it at:

Please keep me informed.

Regards,
Benoit

Confirmed - works great now! Thanks so much - this is an awesome library! If you want to see what we're doing with it - the project is located at www.photosynq.org.

Greg

Hi, this other library goes one step beyond than bblanchon one. Apart form not doing mallocs it does not need to have the whole json string in memory, it receives a Stream and reads it char by char because sometime just the json string is too big. It is called StreamJsonReader. Here's a blog post about the motivation and a little example

ArduinoJsonParser looks like a great library BenoitB, well done.

Something I have noticed that I cant seem to get fixed. This library doesn't seem to parse tiered objects that are not array types. I am trying to parse something like the following;

{"method":"methodname","params":{"id":"fdtf5ere","status":4}}

I can successfully extract the "method" key, and can validate the "params" key with

hashTable.containsKey("params")

I have tried to extract the params hashTable with

JsonHashTable paramsTable = hashTable.getHashTable("params");

but that also seems to fail. Am I missing something, or is this a fault?

Cheers.
Bruce

As is always the case.

After stepping away for 10 minutes, and rechecking everything, it appears I made the mistake as mentioned

"char* json by char json[]"

Changing that, and all is now working as expected.

Thanks
Bruce

No doubt it is me :grin: But, I tried to compile this example under Eclipse using Arduino IDE 1.5.2 and Jantjes plugin and I am getting a bunch of linker errors :

Starting combiner
"D:/Arduino/hardware/tools/avr/bin/avr-gcc" -Os -Wl,--gc-sections -mmcu=atmega328p -o "D:/workspace/ArduinoJsonTest/Release/ArduinoJsonTest.elf"    ./ArduinoJsonTest.cpp.o  ./Libraries/ArduinoJson/JsonGenerator/EscapedString.cpp.o ./Libraries/ArduinoJson/JsonGenerator/JsonArrayBase.cpp.o ./Libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp.o ./Libraries/ArduinoJson/JsonGenerator/JsonValue.cpp.o ./Libraries/ArduinoJson/JsonGenerator/Print.cpp.o ./Libraries/ArduinoJson/JsonGenerator/StringBuilder.cpp.o  ./Libraries/ArduinoJson/JsonGenerator.cpp.o   D:/workspace/ArduinoJsonTest/Release/arduino.ar   "D:/workspace/ArduinoJsonTest/Release/arduino.ar" "-LD:/workspace/ArduinoJsonTest/Release" -lm
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonArrayBase::printTo(Print&) const':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonArrayBase.cpp:11: multiple definition of `ArduinoJson::Generator::JsonArrayBase::printTo(Print&) const'
./Libraries/ArduinoJson/JsonGenerator/JsonArrayBase.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonArrayBase.cpp:11: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Internals::StringBuilder::write(unsigned char)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/StringBuilder.cpp:10: multiple definition of `ArduinoJson::Internals::StringBuilder::write(unsigned char)'
./Libraries/ArduinoJson/JsonGenerator/StringBuilder.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/StringBuilder.cpp:10: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonValue::printPrintableTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:22: multiple definition of `ArduinoJson::Generator::JsonValue::printPrintableTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)'
./Libraries/ArduinoJson/JsonGenerator/JsonValue.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:22: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonValue::printBoolTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:12: multiple definition of `ArduinoJson::Generator::JsonValue::printBoolTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)'
./Libraries/ArduinoJson/JsonGenerator/JsonValue.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:12: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Internals::EscapedString::printTo(char const*, Print&)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/EscapedString.cpp:33: multiple definition of `ArduinoJson::Internals::EscapedString::printTo(char const*, Print&)'
./Libraries/ArduinoJson/JsonGenerator/EscapedString.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/EscapedString.cpp:33: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonValue::printStringTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:30: multiple definition of `ArduinoJson::Generator::JsonValue::printStringTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)'
./Libraries/ArduinoJson/JsonGenerator/JsonValue.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:30: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonObjectBase::printTo(Print&) const':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:14: multiple definition of `ArduinoJson::Generator::JsonObjectBase::printTo(Print&) const'
./Libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:14: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonValue::printLongTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:17: multiple definition of `ArduinoJson::Generator::JsonValue::printLongTo(ArduinoJson::Generator::JsonValue::Content const&, Print&)'
./Libraries/ArduinoJson/JsonGenerator/JsonValue.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonValue.cpp:17: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonObjectBase::getMatchingPair(char const*) const':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:42: multiple definition of `ArduinoJson::Generator::JsonObjectBase::getMatchingPair(char const*) const'
./Libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:42: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonObjectBase::remove(char const*)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:86: multiple definition of `ArduinoJson::Generator::JsonObjectBase::remove(char const*)'
./Libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:86: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonObjectBase::containsKey(char const*) const':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:83: multiple definition of `ArduinoJson::Generator::JsonObjectBase::containsKey(char const*) const'
./Libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:83: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o: In function `ArduinoJson::Generator::JsonObjectBase::operator[](char const*)':
D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:57: multiple definition of `ArduinoJson::Generator::JsonObjectBase::operator[](char const*)'
./Libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:57: first defined here
./Libraries/ArduinoJson/JsonGenerator.cpp.o:(.bss._ZN11ArduinoJson9Generator14JsonObjectBase9nullValueE+0x0): multiple definition of `ArduinoJson::Generator::JsonObjectBase::nullValue'
./Libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp.o:D:/Documents/Arduino/libraries/ArduinoJson/JsonGenerator/JsonObjectBase.cpp:42: first defined here
make: *** [ArduinoJsonTest.elf] Error 1

The example is this one:

/*
 * Arduino JSON library - Generator example
 * Benoit Blanchon 2014 - MIT License
 */

#include "Arduino.h"
#include <JsonGenerator.h>

using namespace ArduinoJson::Generator;

void setup()
{
    Serial.begin(9600);

    JsonArray<2> array;
    array.add<6>(48.756080); // 6 is the number of decimals to print
    array.add<6>(2.302038);  // if not specified, 2 digits are printed

    JsonObject<3> root;
    root["sensor"] = "gps";
    root["time"] = 1351824120;
    root["data"] = array;

    Serial.print(root); // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
}

void loop()
{

}

Any tips where I missed the boat :grin:

@edit: This is interesting. If I compile it direct in the IDE 1.5.2 it works fine....

I think I know what's going on here...

To make the library work properly with the Arduino IDE, I added 4 files at the root:

  • JsonGenerator.cpp
  • JsonGenerator.h
  • JsonParser.cpp
  • JsonParser.h

If you open them, you'll see that they only contain #include of the files in the subfolders.
That's a workaround for the Arduino IDE, because it doesn't look for files in the subfolders.

Unfortunately for you, Eclipse does :stuck_out_tongue:
I suggest you remove the two .cpp files at the root or exclude them from the build.

Thanks

That solved the problem :grin:

Nice library :grin:

Nico

In Arduino how to use both json generator and parser library in a single sketch ?

While i use ,
#include <JsonGenerator.h>
#include <JsonParser.h>

It throws errors stating,

"ArduinoJsonParser\utility\jsmn.cpp.o: In function jsmn_parse(jsmn_parser*, char const*, jsmntok_t*, unsigned int)': C:\Users\Guna Qruize\Documents\Arduino\libraries\ArduinoJsonParser\utility/jsmn.cpp:135: multiple definition of jsmn_parse(jsmn_parser*, char const*, jsmntok_t*, unsigned int)'
ArduinoJson\JsonParser.cpp.o:C:\Users\Guna Qruize\Documents\Arduino\libraries\ArduinoJson/JsonParser/jsmn.cpp:135: first defined here
ArduinoJsonParser\utility\jsmn.cpp.o: In function jsmn_init(jsmn_parser*)': C:\Users\Guna Qruize\Documents\Arduino\libraries\ArduinoJsonParser\utility/jsmn.cpp:250: multiple definition of jsmn_init(jsmn_parser*)'
ArduinoJson\JsonParser.cpp.o:C:\Users\Guna Qruize\Documents\Arduino\libraries\ArduinoJson/JsonParser/jsmn.cpp:250: first defined here"

The cross-posting is strong in this one.

http://forum.arduino.cc/index.php?topic=281554.0

@Gunaseelan: When you didn't get a response within 2 minutes, you just decided to post again, is that it? And that was only 30 minutes after your own thread?

Have you not considered that the person who might be able to help is asleep? And might be for another 8 hours?

Very strong...
http://forum.arduino.cc/index.php?topic=281557.0