Generate nested JSON from Form component

I want to generate below nested Json from user inputs. Below is the expected json output.

{
   "student":{
      "id":1,
      "name":"John Smith",
      "address":{
         "city":{
            "neaby town":{
               "pin":493746
            }
         },
         "coordinates":{
            "latitude":37.7749,
            "longitude":-122.4194
         }
      }
   }
}

Question: How do i generate this nested Json using Form component? Whats a better/recommended approach?

Thanks

I'd go with a JS Code block that lets you define the JSON and put the various variables you've collected in your form into the appropriate objects:

// Define variables - these will actually come from your form
var id = 1;                  //  form.id
var name = "John Smith";     //  form.name
var pin = 493746;            //  etc.
var latitude = 37.7749;
var longitude = -122.4194;

// Create the JSON object
var desiredJSON= {
  "student": {
    "id": id,
    "name": name,
    "address": {
      "city": {
        "nearby_town": {
          "pin": pin
        }
      },
      "coordinates": {
        "latitude": latitude,
        "longitude": longitude
      }
    }
  }
};

return desiredJSON

Gives the following
image

1 Like