0

Im trying to parse some weather json data in my email. But I cant quite fiqure out the syntax. With this code I'm trying to get the main.temp value printed. Below is my code so far, but it dosnt work :-(

%%[ 
var @Json 
set @Json = '{
"weather": [
    {
        "id": 800,
        "main": "Clear",
        "description": "clear sky",
        "icon": "01n"
    }
],
"main": {
    "temp": 275.66,
    "pressure": 1022,
    "humidity": 80,
    "temp_min": 272.15,
    "temp_max": 278.15
},
"sys": {
    "type": 1,
    "id": 5245,
    "message": 0.0065,
    "country": "DK",
    "sunrise": 1459485462,
    "sunset": 1459532996
 }
}'
]%%
{{.dataobject JsonVar type=variable source=@Json maxrows=20}}
   {{.data}} 
      {"target":"@Json"} 
   {{/data}}
{{/dataobject}}
{{#jsonvar}}
   {{#main}}
      {{temp}}
   {{/main}}
{{/jsonvar}}
7
  • 5
    Previously discussed here and here Apr 1, 2016 at 20:23
  • I have read through both but still I can't see where I'm wrong :-(
    – Bidstrup
    Apr 1, 2016 at 20:24
  • what's the error? Apr 1, 2016 at 20:27
  • 1
    I tried about 20 variations of how to reference the nested JSON object. I'll keep working on it and vote to re-open your question if I can find a solution. I would not be surprised if the platform doesn't support it. Apr 2, 2016 at 13:20
  • 3
    I figured it out. Voting to reopen since you presented a different scenario than the previous JSON parsing questions. Apr 4, 2016 at 13:55

2 Answers 2

1

You have to define another dataSource for the sub-object:

%%[
var @Json
set @Json = '{
   "weather":[
      {
         "id":800,
         "main":"Clear",
         "description":"clear sky",
         "icon":"01n"
      }
   ],
   "main":{
      "temp":"275.66",
      "pressure":1022,
      "humidity":80,
      "temp_min":272.15,
      "temp_max":278.15
   },
   "sys":{
      "type":1,
      "id":5245,
      "message":0.0065,
      "country":"DK",
      "sunrise":1459485462,
      "sunset":1459532996
   }
}'

]%%
{{.dataobject JsonVar type=variable maxrows=20}}
   {{.data}}
      {"target":"@Json"}
   {{/data}}
{{/dataobject}}
<tt>
@Json: %%=v(@Json)=%%
{{#each JsonVar}}

  {{.datasource JsonMain type=nested maxRows = 10}}
    {{.data}}
              { "target" : "JsonVar.main" }
    {{/data}}
    <br><br>JsonMain.temp: {{JsonMain.temp}}
    <br>JsonMain.pressure: {{JsonMain.pressure}}
    <br>JsonMain.humidity: {{JsonMain.humidity}}
    <br>JsonMain.temp_min: {{JsonMain.temp_min}}
    <br>JsonMain.temp_max: {{JsonMain.temp_max}}
  {{/datasource}}
{{/each}}
</tt>

Output:

@Json: { "weather":[ { "id":800, "main":"Clear", "description":"clear sky", "icon":"01n" } ], "main":{ "temp":"275.66", "pressure":1022, "humidity":80, "temp_min":272.15, "temp_max":278.15 }, "sys":{ "type":1, "id":5245, "message":0.0065, "country":"DK", "sunrise":1459485462, "sunset":1459532996 } }

JsonMain.temp: 275.66
JsonMain.pressure: 1022
JsonMain.humidity: 80
JsonMain.temp_min: 272.15
JsonMain.temp_max: 278.15
1

I found a solution using server side javascript :

%%[ @JSON,@ListID,lname1
set @JSON = HttpGET('https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA')

]%%
%%=v(@JSON)=%%


<br>
<br>
<br>

<script runat="server">
    Platform.Load("Core", "1")
    // get JSON from ampscript
    var jsonObj = Variable.GetValue("@JSON");
    var obj = Platform.Function.ParseJSON(jsonObj);
    var Adresse = obj.results[0].formatted_address ;
    var val = obj.results[0].address_components[0].long_name ;
    Variable.SetValue("@ListID",Adresse);
    Variable.SetValue("@lname1",val);
   // var evaluatedJSON = eval("(" + address_components + ")");
  // %%=v(@JSON)=%%

</script>
Adresse : %%=v(@ListID)=%%
<br>
<br>
longName 1 : %%=v(@lname1)=%%

<br>
<br>
<br>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .