How to convert JSON string to Object

Server always return an object in string format because when we store JSON object to server we are preferring to covert JSON into string format and then send to backend.

So, When receiving object from server that object is always in string format.

Now time to convert string to JSON object.

Backend returned string looks like below. Make sure string is in JSON format, otherwise you will get syntax error.

'{"name":"Nirmal", "email": "admin@owlcoders.com"}'

To convert JSON use JSON.parese() javascript function

see example below:

const backend_data = '{"name":"Nirmal", "email": "admin@owlcoders.com"}';
const json_obj = JSON.parse(backend_data);

Now json_obj is a perfect JSON object.

Leave a Reply

Your email address will not be published. Required fields are marked *