SOLVED

Re: JSON Parser

Go to solution
Harish_Gupta6
Level 8

JSON Parser

Hi,

I am sending a Json file using Post request to Marketo. Here is the JSON:

{

"action":"createOrUpdate",

"lookupField":"email",

"input":[

{

"requestedCampaignType":"orderConfirmation",

"totalProductPurchased":"2",

"shippingAddress":"48 B",

"shippingCity":"Jammu",

"shippingState":"Jammu and Kashmir",

"shippingCountry":"India",

"sampleJson" : [

{

"ProductID" : "1",

"ProductName" : "Mi Mix 2 (Black, 128 GB)",

"ProductPrice" : "$ 299.98",

"Quantity" : "1",

"ProductColor":"Midnight Black"

},

{

"ProductID" : "2",

"ProductName" : "Mi Mix 3 (Black, 256 GB)",

"ProductPrice" : "$ 399.98",

"Quantity" : "1",

"ProductColor":"Midnight Black"

}

]

}

]

}

The field "sampleJson" is a textarea type field. When i am checking this data in Marketo under "sampleJson" file, it's coming like:

[{

ProductID=1, ProductName=AC (Black, 128 GB), Product Price=$ 599.98, Quantity=1, ProductColor=Midnight Dark}, {ProductID=2, ProductName=Cooler (Black, 256 GB), Product Price=$ 399.98, Quantity=1, ProductColor=Dark Black

}]

Can anyone help me with the reason why Marketo is changing the format of nested json and what is the correct way to keep the format similar in Marketo like I am sending in my post request.

Also, my Velocity script is successfully parsing the JSON when i am storing it like:

[

{

"ProductID" : "1",

"ProductName" : "Mi LED",

"ProductPrice" : "$ 299.98",

"Quantity" : "1",

"ProductColor":"Dark Black"

},

{

"ProductID" : "2",

"ProductName" : "LG Cooler",

"ProductPrice" : "$ 399.98",

"Quantity" : "1",

"ProductColor":"Dark Red"

}

]

but If I run my Velocity script on the format which Marketo is changing then Marketo is showing the error and script is not running.

Please suggest.Sanford Whiteman

Harish Gupta
1 ACCEPTED SOLUTION

Accepted Solutions
Mark_Price
Level 7

Re: JSON Parser

^^ Came here to say this.

Harish Gupta did you just add the escape slashes yourself? If an escape function was run, it would have escaped the line breaks (which aren't needed).

If you're new to escaping in this way, you can use an online tool such as:

https://codebeautify.org/json-escape-unescape

View solution in original post

9 REPLIES 9
Jim_Thao7
Level 9

Re: JSON Parser

Because the results you shared were so different from the post, I just want to ask, are you sure you're validating against the correct record in Marketo?

SanfordWhiteman
Level 10 - Community Moderator

Re: JSON Parser

Echoing Jim, there's something wrong with your testing setup.

Marketo doesn't change the contents of Textarea fields like that. They're opaque blocks of data.

The unwanted data you've shown is exactly what you'd see in Velocity *after* successful parsing of a JSON-like string into an ArrayList of HashMaps (it's the Velocity serialization format).

Mark_Price
Level 7

Re: JSON Parser

Hi Harish,

It seems like you have a JSON object as the value for "sampleJSON" in the API call. Perhaps Marketo is formatting the data writing to the text area.

The last time I did this setup, I want to say I passed an escaped string as the field value in the API call:

"sampleJSON" : "[{\"recommendation1\":{\"resultCount\":\"3\",\"product1url\":\"https://website.com/product-1\"}}]"

hope you get it sorted!

SanfordWhiteman
Level 10 - Community Moderator

Re: JSON Parser

Excellent point, Mark. In this case it's that the parser is too forgiving (same thing happens with webhooks in certain cases). It should kick back an error immediately.

The value being passed is not actually an embedded JSON string but additional nested levels of the overall JSON payload. Therefore it gets stringified using the Velocity (i.e. Java) serialization format.

Harish_Gupta6
Level 8

Re: JSON Parser

Hi Mark,

Thanks for the reply.

I modified the JSON file as per your suggestion but I am getting "Invalid Json" error. Below is the JSON:

{

"action":"createOrUpdate",

"lookupField":"email",

"input":[

{

"requestedCampaignType":"orderConfirmation",

"email":"harishg@techaspect.com",

"firstName":"Post",

"lastName":"Man",

"sampleJson": "[

{

\"ProductID\" : \" 1 \",

\"ProductName \" : \" Mi Mix 2 (Black, 128 GB) \",

\"ProductPrice\" : \" $ 299.98 \",

\"Quantity\" : \"1\",

\"ProductColor\":\"Midnight Black\"

},

{

\"ProductID\" : \"2\",

\"ProductName\" : \"Mi Mix 3 (Black, 128 GB)\",

\"ProductPrice\" : \"$ 399.98\",

\"Quantity\" : \"1\",

\"ProductColor\":\"Midnight Dark\"

}

]"

}

]

}

Please suggest.

Thanks

Harish Gupta
SanfordWhiteman
Level 10 - Community Moderator

Re: JSON Parser

Line breaks are not permitted within string properties. They must be escaped. Same rule in JavaScript itself.

Mark_Price
Level 7

Re: JSON Parser

^^ Came here to say this.

Harish Gupta did you just add the escape slashes yourself? If an escape function was run, it would have escaped the line breaks (which aren't needed).

If you're new to escaping in this way, you can use an online tool such as:

https://codebeautify.org/json-escape-unescape

Harish_Gupta6
Level 8

Re: JSON Parser

Thanks, Mark. It worked.

Harish Gupta
Mark_Price
Level 7

Re: JSON Parser

welcome to the land of the JSON parsers.

glad it's working.