JSON.parse returns string instead of object JSON.parse returns string instead of object json json

JSON.parse returns string instead of object


This seems to be fairly consistent with over-stringified strings. For example I loaded a text file using FileReader.readAsText that came with \n and \r which rendered in the console, so I did - (JSON.stringify(reader.result)).replace(/(?:\\[rn])+/g, '') first to see the symbols, then to get rid of them. Taking that and running JSON.parse() on it converts it to a non-escaped string, so running JSON.parse() again creates an object.

If you do not stringify your string, it will convert to an object and often it is not necessary but if you have no control over the obtained value, then running JSON.parse() twice will do the trick.


As others have stated in the comments it seems that this issue has been solved. If you are receiving a response from the server as a "stringified object" then you can turn it into a normal object with JSON.parse() like so:

var stringResponse = '{"action":"login","args":["nosuccess"]}';var objResponse = JSON.parse(stringResponse);console.log(objResponse.args);

You can also try out the above code here.

As for why the server is returning a string when you really wanted an object, that really comes down to your backend code, what library you are using, and the transport protocol. If you just want your front-end code to work, use JSON.parse. If you want to edit how the backend responds, please provide more information.


The checked response is correct in that it seems to be an issue of over-stringified strings. I came across it from using AWS Amplify AWSJSON type in my project. The solution that worked for me was to iterate multiple (twice) to get an object.

Wrote a simple JS function that when used to parse will return an object. There isn't really error checking, but a starting point.

    export function jsonParser(blob) {       let parsed = JSON.parse(blob);       if (typeof parsed === 'string') parsed = jsonParser(parsed);       return parsed;    }