How to convert json response to CDT

Hello, I have API that returns this data: 

{"coord":{"lon":-3.7,"lat":40.42},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"base":"stations","main":{"temp":297.85,"feels_like":296.53,"temp_min":296.15,"temp_max":299.15,"pressure":1026,"humidity":44},"visibility":10000,"wind":{"speed":2.6,"deg":120},"clouds":{"all":20},"dt":1590499496,"sys":{"type":1,"id":6443,"country":"ES","sunrise":1590468593,"sunset":1590521653},"timezone":7200,"id":3117735,"name":"Madrid","cod":200}

what are all the steps for converting each element of this JSON and save it to a CDT

Can you help me please.

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    Once you get the dictionary going, you can use a helper rule you write to accept the dictionary, and use type() function to output your CDT.

    You would literally just write:

    type!yourCDTName(

    lon: ri!input.lon,

    lat: ri!input.lat,

    ...

    )

    Every field has to be typed twice per line for every field, assuming your CDT and the JSON have exactly the same names for every field.  It will be tedious, so very good if your CDT structure is very solid and clearly not changing in the future.  I'd hate for you to have to go through that tedium twice.

Reply
  • 0
    Certified Lead Developer

    Once you get the dictionary going, you can use a helper rule you write to accept the dictionary, and use type() function to output your CDT.

    You would literally just write:

    type!yourCDTName(

    lon: ri!input.lon,

    lat: ri!input.lat,

    ...

    )

    Every field has to be typed twice per line for every field, assuming your CDT and the JSON have exactly the same names for every field.  It will be tedious, so very good if your CDT structure is very solid and clearly not changing in the future.  I'd hate for you to have to go through that tedium twice.

Children
  • If the names are exactly the same in your CDT as the JSON, you can also cast them rather than having to re-write each parameter name. The only downside is you might have to create several nested CDTs to match the data structure. Once you do that though, you could just do this to cast to the correct type:

    a!localVariables(
      local!result: rule!YourIntegrationRule(),
      cast(
        type!YourTypeName,
        a!fromJson(local!result)
      )
    )