Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Node.js, how do I turn a string to a json? [duplicate]

For example, a HTTP REST API just returned me a JSON, but of course it's a string right now. How can I turn it into a JSON?

like image 678
TIMEX Avatar asked Apr 27 '11 09:04

TIMEX


People also ask

How convert JSON object to string in node JS?

Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

How does JSON fetch data in node JS?

To load the data from customer. json file, we will use fs. readFile , passing it the path to our file, an optional encoding type, and a callback to receive the file data. If the file is successfully read, the contents will be passed to the callback.

How do I save JSON data in node JS?

To save the JSON object to a file, we stringify the json object jsonObj and write it to a file using Node FS's writeFile() function.


2 Answers

You need to use this function.

JSON.parse(yourJsonString);

And it will return the object / array that was contained within the string.

like image 107
Olical Avatar answered Oct 14 '22 05:10

Olical


use the JSON function >

JSON.parse(theString)
like image 34
neebz Avatar answered Oct 14 '22 05:10

neebz