1

I'm sure this is a dumb question, I just can't get it to work and I can't figure out why.

I have this JSON:

[{"prop1":"250","prop2":"1"},{"prop1":"500","prop2":"2"},{"prop1":"750","prop2":"3"}]

I'm trying to target a specific property value, in this case I want to return the value "250", but I don't seem to succeed. I'm testing it on a cloudpage.

<script language="javascript" runat="server">
Platform.Load("Core","1");
  
  var response = HTTP.Get("MyUrlToTheJson");
  var obj = Platform.Function.ParseJSON(response.content);
  Write(obj[0].prop1);
</script>

But it never returns any value. How do I return the value "250" from my JSON?

4
  • 1
    You probably need to print the result to see whether you parsed the JSON successfully. Try to print the obj variable first
    – Duc Le
    Commented Jan 5, 2023 at 8:18
  • I did that before I tested this. But your comment made me do it again, and this time I realized my error. I keep forgetting the importance of capitalization. I simply needed a capital "C" in Content. var obj = Platform.Function.ParseJSON(response.Content); Commented Jan 5, 2023 at 9:20
  • Great. So the issue is solved now?
    – Duc Le
    Commented Jan 5, 2023 at 9:34
  • Yes, will post answer Commented Jan 5, 2023 at 10:40

2 Answers 2

1

I simply needed a capital "C" in Content. Thanks for making me realize, Duc Le.

<script language="javascript" runat="server">
Platform.Load("Core","1");
  
  var response = HTTP.Get("MyUrlToTheJson");
  var obj = Platform.Function.ParseJSON(response.Content);
  Write(obj[0].prop1);
</script>
0

Here's how I approach finding JSON paths in the context of SFMC with SSJS:

<script language="javascript" runat="server">

  Platform.Load("Core","1");
  
  var response = HTTP.Get("https://httpbin.org/json");
  var obj = Platform.Function.ParseJSON(response.Content);
  Write(Stringify(obj));
  
  // paste results into jsonpathfinder.com
  // click Beautify
  // select the desired path, and note the path returned
  
  Write("<br>author:" + obj.slideshow.author);

</script>

Try it yourself: https://mcsnippets.herokuapp.com/s/APBrmuSk

You must log in to answer this question.

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