Unable to parse JSON with Built-in JSON Parser

Using the webhook, I receive a response from my request. The JSON response that I receive is as follows:

{
  "value" : [ {
    "Datastreams" : [ ],
    "Locations" : [ {
      "name" : "Place Name",
      "description" : "Somewhere in the world",
      "@iot.id" : 53750
    } ],
    "@iot.id" : 53610
  } ]
}

This is the script I used to parse the JSON. data being the JSON response from the webhook.

    JSONValue outerObj = JSONValue::parseCopy(data);
    JSONObjectIterator iter(outerObj);
    while(iter.next()) {
        bool isArray = iter.value().isArray();
        String x = String::format("{\"val\":%s}", isArray ? "true" : "false");
        //Particle.publish("CheckingLoop", x, PRIVATE);
        JSONArrayIterator iter2(iter.value());
        while(iter2.next()){
            JSONObjectIterator iter3(iter2.value());
            while(iter3.next()){
                if(iter3.name() == id){
                    int id = iter3.value().toInt();
                    String x = String::format("{\"val\":%d}", id);
                    Particle.publish("CheckingLoop", x, PRIVATE);
                }
                
            }
        }
    }

However, if I hardcode this response onto my script as shown in the script below, the script will work and is able to parse the data.

String data = String::format("{\"value\" : [ {\"Datastreams\" : [ ],\"Locations\" : [ {\"name\" : \"Place Name\", \"description\" : \"Somewhere in the world\",\"@iot.id\" : 53750} ], \"@iot.id\" : 53610}]}");

So I have tried to convert the const char data into a String data like the script below, the result i get when I published the converted string is incomplete as shown below:

String newData= String::format(data);
tastreams" : [ ],
    "Locations" : [ {
      "name" : "Place Name",
      "description" : "Somewhere in the world",
      "@iot.id" : 53750
    } ],
    "@iot.id" : 53610
  } ]
}"@iot.id" : 53610
  } ]
}

I cannot really figure out what is happening. It seems like the parsing script is working but just not with the const char data that is coming in from the my request webhook. What seems to be the issue here? Thanks.

What does the code in the subscription handler look like? Perhaps something is modifying the subscription data before it is copied with JSONValue::parseCopy(data).

For example, publish and subscribe share a single buffer, so if from your subscription handler you publish something before calling parseCopy the data will be corrupt when parsed.

Not that’s necessarily what’s happening for you, but the code above seems OK, so it might be something outside it.

Hi rickkas7 thanks for the advice. It is exactly what you have suggested, I have a publish line of code before the JSONValue::parseCopy(data), once I remove the publish code it is parsing the JSON as expected. Good thing to know that publishing might corrupt the original data. I was treating publishing like a print statement when I am coding.

That's why it's documented here :wink:
https://docs.particle.io/reference/device-os/firmware/photon/#particle-subscribe-

Thanks will pay more attention to the documentation next time :+1:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.