JSON Library?

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

Maybe we should ban him?

Well, it looks like I misquoted.

The cross-posting is strong in with this one.

Benoit et al:

I am trying to use this library, it looks cool and it is well documented.
It works for me on plain JSON strings, however I wonder how to use it on nested objects (curly braces inside curly braces), it parses ok, at least no errors, however I wonder how to get an object inside another objects.
In the below example, I need to get the temp, pressure and humidity.

See this example:

output from: http://api.openweathermap.org/data/2.5/weather?id=3117735

The ArduinoJson pretty print output is:

{
"coord": {
"lon": -3.7,
"lat": 40.42
},
"sys": {
"message": 0.0401,
"country": "ES",
"sunrise": 1420789051,
"sunset": 1420823192
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "Sky is Clear",
"icon": "01d"
}
],
"base": "cmc stations",
"main": {
"temp": 271.744,
"temp_min": 271.744,
"temp_max": 271.744,
"pressure": 980.94,
"sea_level": 1058.7,
"grnd_level": 980.94,
"humidity": 71
},
"wind": {
"speed": 1.51,
"deg": 20.5025
},
"clouds": {
"all": 0
},
"dt": 1420797371,
"id": 3117735,
"name": "Madrid",
"cod": 200
}

and the ArduinoJson iteration of value key pairs is:

coord =>
sys =>
weather =>
base => cmc stations
main =>
wind =>
clouds =>
dt =>
id =>
name => Madrid
cod =>
sys.sunrise =>
sunset =>
pressure =>
humidity =>

Thanks in advance for your help.
AC/.

Folks,
I found the answer myself and the solution is very cool indeed. It is to create some other objects based on the previous objects, cleaver the implementation indeed.
My congratulations for the great implementation.
See my excerpts of how I solved it.
Cheers.
AC/.

// =========================== Get the JSON objects ============
JsonObject& root = jsonBuffer.parseObject(json);

if (!root.success()) {
Serial.println("parseObject() failed");
delay(10000);
return;
}
Serial.println();
if (Debug)
root.prettyPrintTo(Serial); // print it on prettyPrint format
Serial.println();
JsonObject& sys = root["sys"]; // create the object sys inside of root
JsonArray& weather = root["weather"]; // weather is an array of objects
JsonObject& wea = weather[0]; // we care only for the first object, the main weather station
JsonObject& main = root["main"]; // main is the main objct on the weather observations

MADsunrise = sys["sunrise"]; // get the sunrise time on time_t format
MADsunset = sys["sunset"];
MADdesc = wea["description"];
MADbase = root["name"];
MADtemp = main["temp"];
MADpressure = main["pressure"];
MADhumidity = main["humidity"];
MADtemp -= 273.15; // from kelvin to degree celsius
// =========================== Done ===============================

I'm trying to figure out a way to construct a JSON document with nested string value pairs. The below example you can see "phoneNumber" is a nested array with string value pairs, this is unlike the example where you have a nested array named "data" then just the values without tags. Is there a way to do this with this library?

Any help is much appreciated.

{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": null
}

The below example you can see "phoneNumber" is a nested array with string value pairs

Actually, I can't. I can't see phoneNumber anywhere. I do see phoneNumbers.

Seems to me that storing that data in an XML format would be much simpler.

Resurrecting a fairly old thread I know, but anyway...

I'm using the 'ArduinoJson' library and it works great, but I have a question that I hope someone can help with.

The JSON representation of my nested objects contains a couple of nested arrays.

I can encode and decode the object just fine but I would like to iterate through the object to output the entire structure.

I am using the JsonObject::iterator (recursively for nested objects) and that all works.

The problem I am having is when I encounter one of the nested arrays during the iteration.

I currently have this:

void iterate(JsonObject& json) {
	for (JsonObject::iterator it=json.begin(); it!=json.end(); ++it) {
		if ((it->value).is<JsonObject&>()) {
			iterate(it->value);
		} else if ((it->value).is<JsonArray&>()) {
			JsonArray& myArray = (it->value).asArray();
			int count = sizeof(myArray) / sizeof(myArray[0]);
			Serial.print("Key : ");
			Serial.print(it->key);
			Serial.print(" - Values : ");
			for (int i = 0; i < count; ++i) {
				Serial.print(myArray[i].asString());
				if (i != count - 1) {
					Serial.print(", ");
				} else {
					Serial.println();
				}
			}
		} else {
			Serial.print("Key : ");
			Serial.print(it->key);
			Serial.print(" - Value : ");
			Serial.println(it->value.asString());
		}
	}
}

It all does seem to work except that count always seems to equal 1.

Is there something wrong with this:

int count = sizeof(myArray) / sizeof(myArray[0]);

The only arrays I am encoding so far have 3 elements and when I use

int count = 3;

I works perfectly and prints the three values correctly.

if I print the values returned for sizeof(myArray) and sizeof(myArray[0]) they both return 4.

It's probably me being a bit dim, but myArray seems to be an array with 3 valid elements.
Not sure where I am going wrong here.

The JsonArray type is a class that derives from List. List has a size() method. Why not use it?

int count = myArray.size();

@PaulS - Thank you!

I spent an hour or two during over the various files without spotting that. Amazing how much easier it is to find something when you now what you are looking for...

I've spent the last two weeks trying to collect data from a JSON parser, in a meaningful way, for several different kinds of JSON files. So, I had a fair idea about the types involved, and the inheritance, so it wasn't too difficult to find that the expected class had the expected method.