How to convert Struct to Json

Hi,

I have some structs in my app, and in some cases structs within structs to organize the data efficiently. I’m have a requirement to serialize that struct to Json and then to be able to deserialize the Json to a string.

I believe the latter can be achieved with @rickkas7 JsonParserGeneratorRK library, but looking for recommendations on how to take the struct and convert it to Json.

Thanks.

If you want help, you will need to post more than

Some code, some structures.....anything

1 Like

Okay,

here are two structures:

struct state_t{
  bool power = false;
  uint8_t brightness = 254;
  uint16_t hue = 20000;
  uint8_t saturation  = 254;
} state;

struct device_t{
  const char* id = "okow8gFKURexsc7Ifqp2i";
  const char* name = "Office - Wall Sconce";
  const char* mode = "other";
  struct state_t state;
} device;

So if you use the example 2 in the library,

Particle Web IDE

You assign your struct elements to an appropriately name json key

Give it a try and post some code if you get stuck

@shanevanj, thanks.

Yes, I had thought of that but then essentially I am manually building structs, then manually rebuilding them as JSON and then deserializing them to strings. Ideally I was hoping there would be a quicker way to cast the struct to a JSON object, similiar to JSON.stringify() for javascript - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify or SerializeJSON for a language like ColdFusion - https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-s/serializejson.html

There is no automated way of doing it, you just need to convert each member in your struct to and from JSON primitive types (int, bool, float, string, etc.), inner objects (struct in struct) and arrays. And of course do the reverse for deserialization.

1 Like

If you wanted to store the resulting JSON in a char array you could use snprintf, and if you wanted to parse the JSON char array back into the struct you could use sscanf.

In a lot of my projects when I need to format my data as JSON into a char buffer so that I can publish it I’ll use snprintf. While decoding JSON can be done in firmware with sscanf, if the JSON is coming from a webhook I usually try to reorganize the JSON into a comma separated string using mustache templates so that it’s easier to parse out the data I want in the firmware.

https://docs.particle.io/reference/device-cloud/webhooks/#variable-substitution

Also, when designing mustache templates this site made by @rickkas7 is super helpful:
https://rickkas7.github.io/mustache/

Hi, I use this with some python script and parse successfully with json module

#include "Particle.h"

SYSTEM_THREAD(ENABLED);

unsigned long interval = 0;

String status = "{}" ; 
String status_template = "{\"power\":<power>,\"brightness\":<brightness>,\"hue\":<hue>,\"saturation\":<saturation>,\"id\":<id>,\"name\":<name>,\"mode\":<mode>}" ;


struct state_t{
  bool power = false;
  uint8_t brightness = 254;
  uint16_t hue = 20000;
  uint8_t saturation  = 254;
} state;

struct device_t{
  const char* id = "okow8gFKURexsc7Ifqp2i";
  const char* name = "Office - Wall Sconce";
  const char* mode = "other";
  struct state_t state;
} device;


void setup() {
    
 SYSTEM_MODE(AUTOMATIC);
 Particle.variable("status", status) ;

}



void loop() {

if (millis() - interval > 1000) {    
    
    status = String(status_template); // copying ;
    status.replace("<power>",String(device.state.power));
    status.replace("<brightness>",String(device.state.brightness));
    status.replace("<hue>",String(device.state.hue));
    status.replace("<saturation>" ,String(device.state.saturation));
    
    status.replace("<id>",device.id);
    status.replace("<name>",device.name);
    status.replace("<mode>",device.mode);
   
    
    
    interval = millis();
  } 

}

results:
json

1 Like