Cannot get the example Deserialize Json object to work

I'm using this code from the JSON example: the purpose is to extract only the "config"

Serial.println("Test JSON");
char input[] = "{\"config\":{\"user\":\"toto\"}}";
Serial.printf("demo input is: %s \n",input);
StaticJsonDocument<256> doc1, doc2;
DeserializationError error = deserializeJson(doc1, input);
if (error) {
  Serial.print("Failed to parse json config with error:");
  Serial.println(error.c_str());
}
Serial.printf("\n doc1 = \n");
serializeJsonPretty(doc1,Serial);
DeserializationError error2 = deserializeJson(doc2, doc1["config"]);
if (error2) {
  Serial.print("Failed to parse json config with error:");
  Serial.println(error2.c_str());
}
Serial.printf("\n doc2 = \n");
serializeJsonPretty(doc2,Serial);

The deseralizarionError error2 return "emptyinput".
Here is the complete output:

Test JSON
demo input is: {"config":{"user":"toto"}}
 doc1 =
{
  "config": {
    "user": "toto"
  }
}
Failed to parse doc config with error:EmptyInput
 doc2 =
null

Could someone help on this ?

I don't think you can deserialize an object that has already been deserialized. I'm not sure how you would make the contents of 'config' a string containing JSON rather than JSON. Perhaps your JSON should be:

"{\"config\": \"{\\\"user\\\": \\\"toto\\\"}\"}"

Hopefully, this would be deserialized as:
{"config":"{\"user\": \"toto\"}"}
And that would make doc1["config"] the string:
{"user": "toto"}
which could then be deserialized.

try something like

#include <ArduinoJson.h>
char input[] = "{\"config\":{\"user\":\"toto\"}}";
StaticJsonDocument<32> doc;


void setup() {
  Serial.begin(115200);
  DeserializationError error = deserializeJson(doc, input);
  if (error) {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
    return;
  }

  serializeJsonPretty(doc["config"], Serial); // {"user": "toto"}

  const char* config_user = doc["config"]["user"]; // "toto"
  Serial.println(config_user);
}


void loop() {}

to get a feel for the extraction

My final purpose was to use the content of "config" and make it a json config file for my board (saved locally on LittleFS). My initial idea was to deserialize down to an struct and then to add the items of the structure to a StaticJsonDocument on wich I can later do a SerializeJsonPretty to the config.json file. Thank to the code given by J-M-L Jackson, I finally understood that ,amazingly enough , my idea is a brain's torture to perform a simple thing: if I do after the first deserialization to doc1 and SerializeJsonPretty (doc1["config"],configfile); it works just as expected.

File configFile = LittleFS.open("/config.json", "w");
      if (!configFile)
      {
        Serial.println("Failed to open config file");
      }
      serializeJsonPretty(doc1["config"], configFile);
      configFile.close();

Thank you for your help !

glad I helped you out of your torture chamber ! :slight_smile:

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