SOLVED

Parse the JSON String using Velocity Script

Go to solution
Karthik_Logan1
Level 2

Parse the JSON String using Velocity Script

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"
                    }
                ]
            }
        ]
    }
}

Karthik_Logan1_0-1724250004080.png

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)))))

Karthik_Logan1_1-1724250211463.png

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 

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Parse the JSON String using Velocity Script


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.

View solution in original post

5 REPLIES 5
SanfordWhiteman
Level 10 - Community Moderator

Re: Parse the JSON String using Velocity Script


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.

Karthik_Logan1
Level 2

Re: Parse the JSON String using Velocity Script

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 - Community Moderator

Re: Parse the JSON String using Velocity Script


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.

Karthik_Logan1
Level 2

Re: Parse the JSON String using Velocity Script

Apologies for the delayed response, Sanford. Yes, slots should include the product name, product price, product image, and product URL. The JSON provided are samples.

SanfordWhiteman
Level 10 - Community Moderator

Re: Parse the JSON String using Velocity Script


The JSON provided are samples.

Well, it’s good if sample JSON has the same shape as the real JSON. 🙂