Convert String to JSON Object Using JavaScript

May 14, 2018

1 min read

Published in

Convert String to JSON Object using Javascript is an essential task if you are working heavily on JavaScript-based applications. Developer faces many issues when they begin working with JSON and JavaScript in the beginning stage and this kind of solution is very handy. JSON.parse() can be used to convert text to JSON.

Convert String to JSON Object using JavaScript

Here is the code which does that.

1
var jsonObj = JSON.parse(jsonstring);

JSON.parse() does this trick. It parses a JSON text and converts to JavaScript object.

Example 1:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var jsontext = '{"firstname":"James","surname":"Bond","mobile":["007-700-007","001-007-007-0007"]}';  

var contact = JSON.parse(jsontext);  

console.log(contact.firstname + " " + contact.surname);  

console.log(contact.mobile[1]);  

// Output:  
// James Bond  
// 001-007-007-0007

I use these tools to validate JSON online.

JSON Formatter

JSON Validator

Related Articles:

Validate JSON String Using JavaScript

Sharing is caring!