Skip to content
the poor coder

Converting JSON to JavaScript Object

Poor Man
Published
Reading time
2 min read
On this page

Converting JSON to JavaScript Object#

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is used to transmit data between a server and a web application, as an alternative to XML.

Converting a JSON string to a JavaScript object can be done in many ways:

Using JSON.parse() method:#

The most popular way to convert JSON to a JavaScript object is to use the built-in JSON.parse() method. This method takes a JSON string as input and returns a JavaScript object.

Text
const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
const obj = JSON.parse(jsonString);
console.log(obj); // Output: {name: "John", age: 30, city: "New York"}

Using Function Constructor:#

You can also convert JSON to JavaScript object using the Function constructor. This technique involves creating a new Function object with the input JSON string as its body and then invoking the function using the eval() method. This approach is not recommended because it can be dangerous if the input string is not trusted.

Text
const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
const obj = new Function('return ' + jsonString)();
console.log(obj); // Output: {name: "John", age: 30, city: "New York"}

Using jQuery:#

If you are using jQuery in your web application, you can use the $.parseJSON() method to convert JSON to JavaScript object.

Text
const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
const obj = $.parseJSON(jsonString);
console.log(obj); // Output: {name: "John", age: 30, city: "New York"}

Using JSON.parse() with try-catch block:#

If you are not sure if the input string is a valid JSON string, you can use the try-catch block to handle any errors that may occur during the conversion process.

Text
const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
let obj;
try {
  obj = JSON.parse(jsonString);
} catch (error) {
  console.error(error);
}
console.log(obj); // Output: {name: "John", age: 30, city: "New York"}

By using one of these techniques, you can easily convert JSON string to JavaScript object. Always be careful while working with JSON data as it can be manipulated easily.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC