RE: Parsing JSON object

Hi

I want to parse a JSON object(complicated one).

#include <ArduinoJson.h>

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    // wait serial port initialization
  }

  StaticJsonBuffer<1200> jsonBuffer;


  char json[] =
      "{\"node_data\":[{\"mac_address\":\"22:22:22:01\",\"sensor_frequency\":\"120\",\"asset_name\":\"Motion\",\"conversion_factor\":\"1,n\",\"min_absence_time\":\"30\",\"rssi_value\":\"-90\",\"sensor_range\":\"0,150,0,115,130,degC,sensor_2\",\"refresh_rate\":\"5\",\"arm\":\"on\",\"node_type\":\"locker\",\"node_no\":\"001\",\"asset name\":\"Motion\"},{\"mac_address\":\"22:22:22:02\",\"sensor_frequency\":\"120\",\"asset_name\":\"Employee 1\",\"conversion_factor\":\"1,n\",\"min_absence_time\":\"30\",\"rssi_value\":\"-90\",\"sensor_range\":\"0,150,0,115,130,degC,sensor_2\",\"refresh_rate\":\"5\",\"arm\":\"on\",\"node_type\":\"locker\",\"node_no\":\"002\",\"asset name\":\"Employee 1\"}]";

  JsonObject& root = jsonBuffer.parseObject(json);

  if (!root.success()) {
    Serial.println("parseObject() failed");
    return;
  }

  const char* sensor = root["node_data"];

  // Print values.
  Serial.println(sensor);

}

void loop() {
  // not used in this example
}

When I debug the above code I find that the if loop is being satisified and “parseObject() failed” being displayed, what changes do I need to make to detect the object

And how can I extract the JSON array from that object?

Hope I am clear?

Thanks
Sid

When I debug the above code I find that the if loop is being satisified and "parseObject() failed" being displayed, what changes do I need to make to detect the object

I do not understand what you mean by "detect" the object. There is a JSON object that is not well formed. parseObject() returns null when the JSON object is not valid, due to not being well formed.

I see that the JSON array contains "asset_name" in some places and "asset name" in others. I've not seen cases where object names have spaces. Perhaps that is because they can't.

This isn’t valid JSON…

"{\"node_data\":[{\"mac_address\":\"22:22:22:01\",\"sensor_frequency\":\"120\",\"asset_name\":\"Motion\",\"conversion_factor\":\"1,n\",\"min_absence_time\":\"30\",\"rssi_value\":\"-90\",\"sensor_range\":\"0,150,0,115,130,degC,sensor_2\",\"refresh_rate\":\"5\",\"arm\":\"on\",\"node_type\":\"locker\",\"node_no\":\"001\",\"asset name\":\"Motion\"},{\"mac_address\":\"22:22:22:02\",\"sensor_frequency\":\"120\",\"asset_name\":\"Employee 1\",\"conversion_factor\":\"1,n\",\"min_absence_time\":\"30\",\"rssi_value\":\"-90\",\"sensor_range\":\"0,150,0,115,130,degC,sensor_2\",\"refresh_rate\":\"5\",\"arm\":\"on\",\"node_type\":\"locker\",\"node_no\":\"002\",\"asset name\":\"Employee 1\"}]";

you are missing a closing brace at the end, it seems to me.

Hi all,

Rectifyed all the errors

here is the json verified it online

      "{\"node_data\":[{\"mac_address\":\"22:22:22:01\",\"sensor_frequency\":\"120\",\"asset_name\":\"Motion\",\"conversion_factor\":\"1,n\",\"min_absence_time\":\"30\",\"rssi_value\":\"-90\",\"sensor_range\":\"0,150,0,115,130,degC,sensor_2\",\"refresh_rate\":\"5\",\"arm\":\"on\",\"node_type\":\"locker\",\"node_no\":\"001\",\"asset_name\":\"Motion\"},{\"mac_address\":\"22:22:22:02\",\"sensor_frequency\":\"120\",\"asset_name\":\"Employee 1\",\"conversion_factor\":\"1,n\",\"min_absence_time\":\"30\",\"rssi_value\":\"-90\",\"sensor_range\":\"0,150,0,115,130,degC,sensor_2\",\"refresh_rate\":\"5\",\"arm\":\"on\",\"node_type\":\"locker\",\"node_no\":\"002\",\"asset_name\":\"Employee 1\"}]}";

now the asset name alos has an '_'. Still thhe error persists.

The only thing I see now is that each object in the array has two "asset_name":"value" objects in the list of values.

PaulS:
The only thing I see now is that each object in the array has two "asset_name":"value" objects in the list of values.

yup, probably valid JSON but your parser may not be that smart!

Google “arduino JSON library”. One does not simply write a JSON parser.

PaulMurrayCbr:
Google "arduino JSON library". One does not simply write a JSON parser.

Did you look at OP's code? He/she IS using the ArduinoJson library.

PaulS:
Did you look at OP’s code? He/she IS using the ArduinoJson library.

!!

Mea culpa. Obviously I didn’t. Looks like I shall have to move on to standard advice nugget #2: “Read and understand the documentation for the library you are using”.

Hmm. I’m having all sorts of trouble with this library. It seems very sensitive to how much space you give it (I assume that’s what the template is), it seems to really, really dislike JSON attribute names with spaces in them, and there’s no debugging output that you can turn on.

And it’s a big, complicated library. Parsing JSON is actually not all that difficult.

Oh, and it uses C++ operator overloading.

For JSON that’s this bulky, find a library that has a “stream” model. Rather than keeping the JSON in memory, it runs through the input and invokes callbacks as it finds things.

This one GitHub - giacomodrago/minijson_reader: A DOM-less JSON parser that can parse a JSON object without allocating a single byte of memory looks promising. There are others at json.org .

I remember reading somewhere, but gosh darn it I cannot find it now, that this (maybe) library struggled with JSON objects with more than 15 (or was it 13? I cannot remember) key-value pairs.

OP could try to shorten the JSON to say 15 (then 10 perhaps) key-value pairs and see what happens....