string in json format

Hi!

I'm trying to create a String in the json format like this:

String json = {"91234": {"status": "NotOk"}};

where "91234" is a string variable.

So it would be:

String STRING_VARIABLE = "91234";

String json = {STRING_VARIABLE: {"status": "NotOk"}};

but I am not able to concatenate that into a variable.

can anybody help me?

we would probably not recommend the String class if you can do otherwise but with a String that could look like this

String STRING_VARIABLE = "91234";
String json = "{\"" + STRING_VARIABLE + "\": {\"status\": \"NotOk\"}}";

void setup() {
  Serial.begin(115200);
  Serial.println(STRING_VARIABLE);
  Serial.println(json);
}

void loop() {}

Serial Monitor (@ 115200 bauds) will show

[color=purple]
91234
{"91234": {"status": "NotOk"}}
[/color]

note that you can’t directly have double quotes in a string, you need to ‘escape’ it (see escape sequences) that’s why you can see " appearing the the String definition where double quotes are needed

Thank you, @J-M-L

It worked :slight_smile:

:slight_smile: cool