+ Start a Discussion
hsupriyahsupriya 

Parsing JSON. Urgent help!

My JSON output looks as follows:

 

{\n \"Name\" : \"mary mercy\",

\n \"Email\":\"mmercy@gmail.com\",

\n \"Phone\" :\"(xxx) xxx-xxx\"\n}"

 

I want to be able to be parse these individual values Name, Email and Phone and display them on the VF page.

 

I tried using deserialize, but it gave me the fatal error saying, 

Malformed JSON: Expected '[' at the beginning of List/Set

 

 

Please help. Thanks

Best Answer chosen by Admin (Salesforce Developers) 
hsupriyahsupriya

Hello VBS,

 

You are right. The issue was with the way JSON was being received. 

So, in my class(Databse.com), from where I was sending JSON to salesforce, I returned the Salesforce object instead of returning the JSON string and tha resolved my issue.

Thanks anyway!

All Answers

joshbirkjoshbirk

I find http://jsonlint.com/ incredibly useful for trying to tweak JSON.  It looks like it has something to do with your escapes, as:

 

{
    "Name": "marymercy",
    "Email": "mmercy@gmail.com",
    "Phone": "(xxx)xxx-xxx"
}

 Validates fine. What's the situation that you need to try and escape this out?

 

 

hsupriyahsupriya

Hello Josh,

 

Thanks for the reply.

So, I have integrated Databse.com and Salesforce.com and this is the response I am getting from Databse.com. I need to separate all the fields from JSON into separate strings and display them on the associated VF page. 

 

joshbirkjoshbirk

So you are calling a DB.com REST API via a Visualforce page on Salesforce?

hsupriyahsupriya

yes, something similar. 

JSON parsing has been a nightmare for me always. The site you mentioned is quite useful. If at all I could get rid of new line and escape characters from the response, I will manage to get the valid JSON.

But was wondering, if there is any workaround. May be I should serialize the JSON at Database.com itself and then send to salesforce?

vbsvbs
Can you post the class built for de-serialization in Salesforce? Is the JSON received in the same format as posted above? I would expect the escape characters to not be present for the quotes...
hsupriyahsupriya

Hello VBS,

 

You are right. The issue was with the way JSON was being received. 

So, in my class(Databse.com), from where I was sending JSON to salesforce, I returned the Salesforce object instead of returning the JSON string and tha resolved my issue.

Thanks anyway!

This was selected as the best answer