Using JSON format to send data on mqtt client

Hi i am using the library JsonparseGeneratorRK to generate the JSON format and share my data on cloud. I use jw.insertkeyvalue(“data”, “hello”) to have something like

“data”: “hello”

However I need to create a nested structure like:

“data:” {
“Sentence”: " hello"
}

How can I create a nested structure using this library? Can someonw provide an example?

I guess it should be

This are actually two entries

  1. a key/value pair where the the key = data and the value is another JSON object
  2. that object is a key/value pair that can be created the way you did but not as member of jw but as member of the object of step 1.
void createEventPayload(int orario,  String status, double batt)
{ 
  JsonWriterStatic<256> pw;
  {
      JsonWriterAutoObject obj(&pw);
      pw.insertKeyValue("batte:",batt);
  }
  JsonWriterStatic<256> jw;
  { 
     
    JsonWriterAutoObject obj(&jw);

    jw.insertKeyValue("version", "0.0.1.0");
    jw.insertKeyValue("id", "device");
    
  }

  Particle.publish("T", jw.getBuffer(), PRIVATE);
}

This doesn’t seem to work,maybe I didn’t understand.

Sticking with one example would prevent additional confusion :wink:

As to your original request, this is how you'd do it

  JsonWriterStatic<256> jw;
  JsonWriterAutoObject obj(&jw);

  jw.insertKeyObject("data");                 // create the key/value(object)
  jw.insertKeyValue("Sentence", "hello");     // add the key/value(string) to that open object
  jw.finishObjectOrArray();                   // close the object

BTW

You shouldn't have a colon in your key there. While it's not forbidden, it's not best style either :wink:
The JSON string would look like this

{ "data:": {             // <-- 🤮
    "Sentence":"hello" 
  }
}

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