0

Hello I have the following JSON returned from my server side:

{"command":"SELECT","rowCount":1,"oid":null,"rows":[{"username":"xxxx"}],"fields":[{"name":"username","tableID":34722,"columnID":3,"dataTypeID":1043,"dataTypeSize":-1,"dataTypeModifier":204,"format":"text"}],"_parsers":[null]}

I'm parsing that json in javascript like this :

 var ParsedJSONResponse = $.parseJSON(JSONResponseFromServerSide);

The variable is "ParsedJSONResponse" always null, my JSON is valid I checked it in JSONLint so what is going on please?

2
  • You do know that if you put the contentType:JSON that jQuery will parse it for you and ultimately return a a JSON object. Commented Sep 19, 2013 at 17:07
  • looks like the value is already a object not a json string so there is no need to parse it again Commented Sep 19, 2013 at 17:07

2 Answers 2

3

You should parse the String

var stringJson = '{"command":"SELECT","rowCount":1,"oid":null,"rows":[{"username":"xxxx"}],"fields":[{"name":"username","tableID":34722,"columnID":3,"dataTypeID":1043,"dataTypeSize":-1,"dataTypeModifier":204,"format":"text"}],"_parsers":[null]}';  
var ParsedJSONResponse = $.parseJSON(stringJson);  

You just have a Json (JSONResponseFromServerSide) and no reason to parse it.

Parsing the Json object returns null.

$.parseJSON({}); // returns `null`  
Sign up to request clarification or add additional context in comments.

Comments

0

Your code is correct check out this JSFiddle.

var JSONResponseFromServerSide = '{"command":"SELECT","rowCount":1,"oid":null,"rows":[{"username":"xxxx"}],"fields":[{"name":"username","tableID":34722,"columnID":3,"dataTypeID":1043,"dataTypeSize":-1,"dataTypeModifier":204,"format":"text"}],"_parsers":[null]}'; //it's your json string
$.parseJSON(JSONResponseFromServerSide); //nothing wrong

If JSONResponseFromServerSide is already a JavaScript object, than you don't have to do parseJSON

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.