One String inside Another

Colleagues,

I'm trying to create a Json String. In the code snippet below, apikey and object are Strings. But I can't get the JSON String to understand that these two objects are Strings and not just text.

String objeto = image.c_str();
String apikey = "bcadba31ad9ea04a8a3";
      
String json = "{\r\n\"ApiKey\":apikey,\r\n\"SampleImages\":{\r\n\"sample1\":objeto,\r\n\"sample2\":" "\r\n}\r\n}";

The result of this is:

{
"ApiKey":apikey,
"SampleImages":{
"sample1":objeto,
"sample2":
}
}

But instead of apikey and object I need their respective values.

Alguém sabe como tem que ficar uma String JSON ?

Thanks

String concatenation:

For simplicity I removed the \r\n. I had tried this. But it gives stray error \

String json = "{\r\n\"ApiKey\":" + apikey + ",\"SampleImages\":{\"sample1\":" + objeto + ","sample2\":" "}}";

O erro completo:

error: stray '\' in program

String json = "{\r\n\"ApiKey\":" + apikey + ",\"SampleImages\":{\"sample1\":" + objeto + ","sample2\":" "}}";
 

AJSON:56:105: error: unable to find string literal operator 'operator""sample2' with 'const char [5]', 'unsigned int' arguments
 String json = "{\r\n\"ApiKey\":" + apikey + ",\"SampleImages\":{\"sample1\":" + objeto + ","sample2\":" "}}";
                                                                                                         ^
exit status 1
stray '\' in program

You can use raw string literals.

+ ","sample2\":" "}}";
               ^ ^      // delete that pair of double quotes

Thank you all.

I think I understand, at least half of it already works.

Special thanks to Delta_G

It looks cool, there's just one detail missing: I need the apikey and object values ​​to be in quotation marks. When I print the String on the serial monitor, these two values ​​are being called and displayed correctly but they are not in quotes.

How to make ?

String json = "{\r\n\"ApiKey\":" + apikey + ",\r\n\"SampleImages\":{\r\n\"sample1\":" + objeto + "\r\n}\r\n}";

If you use a raw string you don't need to escape anything.

I think there are several possibilities. You could use sprintf() with an %s at the variable position. Or concatenate two raw strings and the variable string. That should work String too, because raw strings are only a special form of string literals. ( I cannot check at the moment and never used it with String ).

Here's how it looks doing all the heavy lifting in snprintf and then at the end creating the String from the result.

void setup() {
   
   char buf[256];

   Serial.begin(115200);
   delay(5000);
   
   snprintf( 
      buf, 
      sizeof buf, 
      R"(
{
  "ApiKey": "%s",
  "SampleImages": {
    "sample1": "%s",
    "sample2": " "
  }
}
)",
      "this is your api key",
      "this is the value of sample1"
   );
   String str(buf);
   Serial.println(str);
}

void loop() {
}

Now I was able to test and it works with concatenating Strings too (no sprintf used):
I made image.c_str() to a string literal:

void setup() {
    Serial.begin(115200);
    String objeto = "image.c_str()";
    String apikey = "bcadba31ad9ea04a8a3";

    String json = R"({
"ApiKey":")" + apikey + R"(",
"SampleImages":{
"sample1":")" + objeto + R"(",
"sample2":" "
}
})";
    Serial.println(json);
}

void loop() {
    // put your main code here, to run repeatedly:

}

Result:

{
"ApiKey":"bcadba31ad9ea04a8a3",
"SampleImages":{
"sample1":"image.c_str()",
"sample2":" "
}
}

[EDIT] Sorry, forgot the quotes around your variables :wink: - now it should be correct.

Thanks for these additional code snippets.

It will certainly help many people who have this same question.

Health and peace.