JSON Parser | Community
Skip to main content
Harish_Gupta6
Level 6
February 21, 2019
Solved

JSON Parser

  • February 21, 2019
  • 3 replies
  • 7592 views

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

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 Mark_Price

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


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

3 replies

Jim_Thao7
Level 8
February 21, 2019

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
February 21, 2019

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

Level 6
February 21, 2019

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
February 21, 2019

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.