JSON Library?

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?

[quote author=Nick Gammon date=1416984106 link=msg=1976274]The cross-posting is strong in this one.[/quote]

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.

hi
the arduino json library is unable to parse the following file. I have validated online that it is a valid json file…any clues as to why it might not be parsing ? it simply crashes my WEMOS MINI

here is my serial output:

{
“fileversion”: 1,
“date_created”: “07April2017”,
“latest_build”: “mains.bin”,
“mac_address”: “08:dd:0f:1c:2a”,
“data_activated”: “TBC”,
“auth_code”: “shjkhsjk839903klkkl”,
“blynk_server”: “blynk.xxxx.com”,
“hardware” : “wemosmini”,

“sv”: {
“appliance”: “mains”,
“flow_sensor”: “true”,
“water_sensor”: “true”,
“valve”: “true”,
“number_valves”: 1,
“type” : “master”
},

“children”: [
{
“child” : 1,
“appliance”: “dishwasher”,
“mac_address”: “08:dd:0f:1c:2a”,
“auth_code”: “shjkhsjk839903klkkl”
},
{
“child”: 2,
“appliance” : “washing_machine”,
“mac_address” : “4c:2a:24:43”,
“auth_code”: “jdhjjks88392ui”
}
]
}
waiting for parsing to occur…

Exception (28):
epc1=0x4000bf0e epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: cont
sp: 3ffef7c0 end: 3ffefa30 offset: 01a0

stack>>>
3ffef960: 3fffdad0 3ffef9b0 00000001 40202392
3ffef970: 3ffef980 3fff124b 3ffef980 0000000a <
3ffef980: 3ffe85b8 00000000 3fff1254 00000800
3ffef990: 00000000 00000001 3ffee94c 3ffeea08
3ffef9a0: 3fffdad0 00000000 3ffe8350 4020259d
3ffef9b0: 00000000 3ffe89e8 00000000 3ffe89e8
3ffef9c0: 00000000 00000000 ffffffff fe000001
3ffef9d0: 3ffe8496 00000000 fe01ef35 00000000
3ffef9e0: 00000000 feefeffe feefeffe feefeffe
3ffef9f0: feef0031 feefeffe feefeffe feefeffe
3ffefa00: feefeffe feefeffe feefeffe feefeffe
3ffefa10: feefeffe feefeffe 3ffeea00 40204018
3ffefa20: feefeffe feefeffe 3ffeea10 40100718
<<<stack<<<

ets Jan 8 2013,rst cause:2, boot mode:(1,7)