Corey673
Corey673

Reputation: 23

Create Json Object with ArduinoJson 6

So im trying to create and json with 4 of the same objects in it but cant seem to get it to work using ArduinoJson 6, version 5 looks easier to use with .createObject but curious why they removed it in 6 cause there must still be a way to do it. Is there a way to do it or do i have to downgrade to version 5?

What i want it to look like:

{
   "Spot":[
      {
         "LocationId":"673d855c-9f66-4e49-8b2c-737e829d880c",
         "SpotId":"1",
         "Occupied":"Empty"
      },
      {
         "LocationId":"673d855c-9f66-4e49-8b2c-737e829d880c",
         "SpotId":"2",
         "Occupied":"Empty"
      },
      {
         "LocationId":"673d855c-9f66-4e49-8b2c-737e829d880c",
         "SpotId":"3",
         "Occupied":"Empty"
      },
      {
         "LocationId":"673d855c-9f66-4e49-8b2c-737e829d880c",
         "SpotId":"4",
         "Occupied":"Empty"
      }
   ]
}

My Code:

  char json_string[256];
  StaticJsonDocument<256> carparkResponse;

  JsonObject spot1 = carparkResponse.createNestedObject("Spot");
   spot1["LocationId"] = LOCATION_ID;
   spot1["SpotId"] = "1";
   spot1["Occupied"] = occupied1.isOccupied();
   
  JsonObject spot2 = spot1.createNestedObject();
  spot2["LocationId"] = LOCATION_ID;
  spot2["SpotId"] = "2";
  spot2["Occupied"] = occupied2.isOccupied();
  
  JsonObject spot3 = spot2.createNestedObject();
  spot3["LocationId"] = LOCATION_ID;
  spot3["SpotId"] = "3";
  spot3["Occupied"] = occupied3.isOccupied();
  
  JsonObject spot4 = spot3.createNestedObject();
  spot4["LocationId"] = LOCATION_ID;
  spot4["SpotId"] = "4";
  spot4["Occupied"] = occupied4.isOccupied();
  

  serializeJson(carparkResponse, json_string);
  Serial.println(json_string);

Upvotes: 0

Views: 970

Answers (1)

Corey673
Corey673

Reputation: 23

So I found the Json builder that fixed my issue in case anyone else has to make any json's https://arduinojson.org/v6/assistant/#/step1

Upvotes: 1

Related Questions