Received JSON string at Notehub not in same order?

Hi,

I send this JSON string at Notecard WiFi v1. The sensor data is in this order temp, humid, light, batt, rssi.

const char  testnote2[200] = "{"
                               "\"req\": \"note.add\","
                               "\"file\": \"sensor.qo\","
                               "\"sync\": true,"
                               "\"body\": {"
                                "\"temp\":20.5,"
                                "\"humid\":45,"
                                "\"light\":60,"
                                "\"batt\":3300,"
                                "\"rssi\":88"
                               "}}\n";

This is the JSON string I received at Notehub.

“file”: “sensor.qo”,
“body”: {
“batt”: 3300,
“humid”: 45,
“light”: 60,
“rssi”: 88,
“temp”: 20.5
},

Why is the received JSON string not in the correct order?

Regards,
Markel

Hi @mtrobregado,

Due to the way Notehub processes inbound events, there is no guarantee on the order of fields in the resulting JSON. We always recommend processing events by field name and not by the expected location of the field.

Rob

2 Likes

According to RFC 4627 (the JSON RFC), a JSON object is “an unordered collection of zero of more name/value pairs”. Because a JSON object is not required to keep the same ordering, you shouldn’t expect to find the same ordering when you read it compared to when you sent it. It is best to validate and access your JSON data by the field name.

3 Likes