0

I'm trying to extract data and present it in a readable format (testing this out with a public API)

<script runat="server">
 Platform.Load("Core","1.1.5");

  var url="https://api.nasa.gov/planetary/apod?api_key=7Apb3QN7qVsXPuPDcSZBKAPf8twca03C5w4Oxuyc";
  var response;
  var json;
  var picurl;
  var explanation;

  
  try {
    response=HTTP.Get(url);
    str=Stringify(response);
    
    json=Platform.Function.ParseJSON(str);
    explanation=json.explanation;
    

    Write("Respone: "+Stringify(json)+"<br><br>");
    Write("Explanation: "+Stringify(explanation));

  } catch(error) {
    Write(Stringify(error));
  }
     
 
</script>

So the part that isn't working is the "explanation" variable. The only other workaround I can use to render it is if I use SetValue to create an AMPScript variable, but I'd preferably like to keep it in SSJS code just to streamline the logic when reading the code.

4

So the issue is that the returned JSON has another step in the path before explanation which is why your code is returning null, as at the 'top level' there is no 'explanation' key.

Here is the returned JSON:

{
  "Status": 0,
  "Content": "{\"date\":\"2020-12-14\",\"explanation\":\"The streak across the sky is a capsule returning from an asteroid. It returned earlier this month from the near-Earth asteroid 162173 Ryugu carrying small rocks and dust from its surface. The canister was released by its mothership, Japan's Hayabusa2, a mission that visited Ryugu in 2018, harvested a surface sample in 2019, and zoomed back past Earth. The jettisoned return capsule deployed a parachute and landed in rural Australia. A similar mission, NASA's OSIRIS- REx, recently captured rocks and dust from a similar asteroid, Bennu, and is scheduled to return its surface sample to Earth in 2023. Analyses of compounds from these asteroids holds promise to give humanity new insights about the early Solar System and new clues about how water and organic matter came to be on Earth. Experts Debate: How will humanity first discover extraterrestrial life?\",\"media_type\":\"video\",\"service_version\":\"v1\",\"title\":\"Capsule Returns from Asteroid Ryugu\",\"url\":\"https://www.youtube.com/embed/NuLuCeawQSo?rel=0\"}\n"
}

Which as you may notice, by the escape characters, is an object that contains an Object String inside of it.

The only two keys in this object are 'Status' and 'Content'. As the explanation part you want is inside the Content key, you will need to use the following code to get the Content value.

var content = json.Content

Now as a note, although this is a string Object, it needs to be Parsed (like you did previously to get your json var) to be used.

var cObj = Platform.Function.ParseJSON(content);

Which will give the following Object:

{
  "date": "2020-12-14",
  "explanation": "The streak across the sky is a capsule returning from an asteroid. It returned earlier this month from the near-Earth asteroid 162173 Ryugu carrying small rocks and dust from its surface. The canister was released by its mothership, Japan's Hayabusa2, a mission that visited Ryugu in 2018, harvested a surface sample in 2019, and zoomed back past Earth. The jettisoned return capsule deployed a parachute and landed in rural Australia. A similar mission, NASA's OSIRIS- REx, recently captured rocks and dust from a similar asteroid, Bennu, and is scheduled to return its surface sample to Earth in 2023. Analyses of compounds from these asteroids holds promise to give humanity new insights about the early Solar System and new clues about how water and organic matter came to be on Earth. Experts Debate: How will humanity first discover extraterrestrial life?",
  "media_type": "video",
  "service_version": "v1",
  "title": "Capsule Returns from Asteroid Ryugu",
  "url": "https://www.youtube.com/embed/NuLuCeawQSo?rel=0"
}

Which you can now use to get your 'explanation' key:

var expl = cObj.explanation;

Which would output:

The streak across the sky is a capsule returning from an asteroid. It returned earlier this month from the near-Earth asteroid 162173 Ryugu carrying small rocks and dust from its surface. The canister was released by its mothership, Japan's Hayabusa2, a mission that visited Ryugu in 2018, harvested a surface sample in 2019, and zoomed back past Earth. The jettisoned return capsule deployed a parachute and landed in rural Australia. A similar mission, NASA's OSIRIS- REx, recently captured rocks and dust from a similar asteroid, Bennu, and is scheduled to return its surface sample to Earth in 2023. Analyses of compounds from these asteroids holds promise to give humanity new insights about the early Solar System and new clues about how water and organic matter came to be on Earth. Experts Debate: How will humanity first discover extraterrestrial life?

The final code would be:

<script runat="server">
 Platform.Load("Core","1.1.5");

  var url="https://api.nasa.gov/planetary/apod?api_key=7Apb3QN7qVsXPuPDcSZBKAPf8twca03C5w4Oxuyc";
  var response;
  var json;
  var picurl;
  var explanation;

  
  try {
    response=HTTP.Get(url);
    str=Stringify(response);
    
    json=Platform.Function.ParseJSON(str);
    var content = json.Content;
    var cObj = Platform.Function.ParseJSON(content);
    explanation=cObj.explanation;
    

    Write("Respone: " + Stringify(json)+"<br><br>");
    Write("Explanation: " + explanation);

  } catch(error) {
    Write(Stringify(error));
  }
     
 
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.