Arduinojson generation using variables

JsonObject& root = jsonBuffer.createObject(); root["data"] = RawJson(F("{\"token\":jsontest1,\"mac\":46002300146,\"temp\":24,\"time\":1516383700}"));

need help in generating json from variables. i am using arduinojson library where the
mac,temp and time have to be variables.

If you want to build a raw JSON string, snprintf() is your friend.
But if you are using the AdruinoJson library, why not build the JSON step by step?

1 Like

yes i solved it by doing step by step. the variables were reading in String format,
converted it to Char array using syntax
String testString = Time.now();

char testChar[20];
testString.toCharArray(testChar, 20);

then printing it by
JsonObject& root = jsonBuffer.createObject();
root[“temperature”]=testString;

 root.printTo(Serial);

You can avoid the intermediate variable when you either of these

  root["temperature"] = testString.c_str();
  // or 
  root["temperature"] = (const char*)testString;
1 Like

it worked . thank you :slight_smile: