Convert JSON String to JSON Object Javascript

In this article, we will see converting JSON strings to JSON objects in javascript. You can use the javascript JSON.parse() method to convert a JSON string into a JSON object. JSON is used to exchange data between server and web. JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

So, let's see string to JSON javascript, json.parse(), parse JSON object, convert string javascript, JSON string to a JSON object, how to convert JSON string in javascript, convert string to object javascript.

JSON has built on two types:

  • A collection of key and value pairs.
  • An ordered list of values.
Example 1:

we received a JSON string from the web server.

'{"name":"maria", "age":24, "city":"France", "gender":"male"}'

Now we can convert strings to json object using json.parse() method.

var user = JSON.parse('{"name":"maria", "age":24, "city":"France", "gender":"male"}');

 

 

Example 2:

 Now we are converting JSON text to Javascript Objects like the below example.

<script>
const txt = '{"name":"maria", "age":24, "city":"France", "gender":"male"}'
const obj = JSON.parse(txt);
console.log("Name: "+obj.name);
console.log("Age: "+obj.age);
</script>

 

Example 3:

Now, In this example convert Array as JSON using json.parse() method in javascript. So, the JSON array will be parsed into a javascript array.

<script>
const text = '[ "maria", "24", "France", "male" ]';
const parseArr = JSON.parse(text);
console.log(parseArr[0]);
console.log(parseArr[1]);
</script>

 


You might also like :

RECOMMENDED POSTS

FEATURE POSTS