How to deserealize json with nested json?

Hello

my arduino can receive the following jsons:

{"release":"{"quantity":"50"}"}
or
{"diet":"{[{"schedule":"08:00","grams":50},{"schedule":"12:00","grams":50}]}"}

how can i check if i got a json with the command "release" or "diet". Let's say I received the "release" command, how can I retrieve the quantity information (50)?

My code:


      //deserealize JSON
      DeserializationError error = deserializeJson(doc, F("{\"release\":{\"quantity\":\"50\"}}"));
        
      
      if (error) {
        Console.print(F("deserializeJson() failed: "));
        Console.println(error.f_str());

      } else {
        const char* release= doc["release"];
        if (release) {
          Console.println("ok, we received a "release" command");
          return;
        }

thanks

1 Like

Hi @mateusguilherme
Which version of the ArduinoJson library are you using?
5.0 or 6.0?

With ArduinoJson 5, you invoked the JSON parser by calling JsonBuffer::parseObject() or JsonBuffer::parseArray().

With ArduinoJson 6, you call the function deserializeJson() and pass the JsonDocument and the input as arguments.

RV mineirim

hi.. I'm using version 6.18.4

Hi @mateusguilherme
using tags /> post your complet code.

RV mineirin

The deserialization tutorial describes how to find out what is in a Json object.

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