Parse the JSON String using Velocity Script | Community
Skip to main content
Level 2
August 21, 2024
Solved

Parse the JSON String using Velocity Script

  • August 21, 2024
  • 1 reply
  • 2655 views

Hi Everyone,

 

Our Goal: Display recommended products from Adobe Target in Marketo emails.

 

I successfully sent a request to Adobe Target through a Marketo webhook and received a product recommendation JSON response. I need to display the recommended products in Marketo email using Velocity script.

{ "status": 200, "requestId": "74c07edd", "client": "ABC", "id": { "tntId": "1234aaa" }, "edgeHost": "mboxedge34.net", "execute": { "mboxes": [ { "index": 1, "name": "apitest", "options": [ { "content": { "recommendedItems": { "key": "key01", "slot01": "product01", "slot02": "product02", "slot03": "product03", "slot04": "product04" } }, "type": "json", "sourceType": "target" } ] } ] } }

I'm storing the response value in a Marketo custom text field, but there's a discrepancy between the actual response and the stored response - additional text like "Map" and "List" has been inserted.

Map(mboxes -> List(Map(index -> 1, name -> apitest, options -> List(Map(content -> Map(recommendedItems -> Map(slot03 -> product03, slot02 -> product02, slot04 -> product04, slot01 -> product01, key -> key01)), type -> json, sourceType -> target)))))

I need assistance with converting/parsing JSON and displaying all slot information in an email using Velocity script.

@sanfordwhiteman please share your valuable inputs here. 

 

Thanks,

Karthik 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

I'm storing the response value in a Marketo custom text field

Looks like you’re mapping the root property execute specifically.

 


I'm storing the response value in a Marketo custom text field, but there's a discrepancy between the actual response and the stored response - additional text like "Map" and "List" has been inserted.Map(mboxes -> List(Map(index -> 1, name -> apitest, options -> List(Map(content -> Map(recommendedItems -> Map(slot03 -> product03, slot02 -> product02, slot04 -> product04, slot01 -> product01, key -> key01)), type -> json, sourceType -> target)))))

Not so much a discrepancy. You’re trying to map a complex response property (with nested objects and arrays) to a single Marketo field. When you do that, Marketo stringifies the response — so far so good. But it stringifies it using Java representation, not JSON — which isn’t what you want.

 

What you want is to map a response property and have it turned into a long JSON string, which can then be parsed by Velocity. Unfortunately, there’s no native way to do that. You’d have to fetch the response via a kind of API gateway which stringifies the response on-the-fly. Very easy to build using AWS API Gateway, for example — though it’s easy for me to say that, having using it for several years!

 

Let’s take a little step back, though. I’m not familiar with this API, but are you 100% sure that you can’t map execute.mboxes[0].options[0].content.recommendedItems.slot01, execute.mboxes[0].options[0].content.recommendedItems.slot02, etc.? I understand this won’t scale if there are unlimited slots.

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
August 21, 2024

I'm storing the response value in a Marketo custom text field

Looks like you’re mapping the root property execute specifically.

 


I'm storing the response value in a Marketo custom text field, but there's a discrepancy between the actual response and the stored response - additional text like "Map" and "List" has been inserted.Map(mboxes -> List(Map(index -> 1, name -> apitest, options -> List(Map(content -> Map(recommendedItems -> Map(slot03 -> product03, slot02 -> product02, slot04 -> product04, slot01 -> product01, key -> key01)), type -> json, sourceType -> target)))))

Not so much a discrepancy. You’re trying to map a complex response property (with nested objects and arrays) to a single Marketo field. When you do that, Marketo stringifies the response — so far so good. But it stringifies it using Java representation, not JSON — which isn’t what you want.

 

What you want is to map a response property and have it turned into a long JSON string, which can then be parsed by Velocity. Unfortunately, there’s no native way to do that. You’d have to fetch the response via a kind of API gateway which stringifies the response on-the-fly. Very easy to build using AWS API Gateway, for example — though it’s easy for me to say that, having using it for several years!

 

Let’s take a little step back, though. I’m not familiar with this API, but are you 100% sure that you can’t map execute.mboxes[0].options[0].content.recommendedItems.slot01, execute.mboxes[0].options[0].content.recommendedItems.slot02, etc.? I understand this won’t scale if there are unlimited slots.

Level 2
August 21, 2024

Thanks for the quick response.

Definitely I will refer your API gateway Transforming Marketo-incompatible webhook response... - Marketing Nation

 

I assume that each slot must have product name, product price, product Image and product URL. 

 

As an alternative approach, I plan to write a custom service script using PHP or Python, host it on an AWS server, and then invoke the web service via a Webhook. I'm unsure if this will resolve our issue.

 

If there are only a few products, I will use execute.mboxes[0].options[0].content.recommendedItems.slot01 in this manner.

 

Thanks again.

SanfordWhiteman
Level 10
August 21, 2024

I assume that each slot must have product name, product price, product Image and product URL.

Does it, though? In your example it just seems like a string.