简体   繁体   中英

Convert String to JSON and prettify it in Node.js

I am receiving the following data from server:

{ "_id" : ObjectId("5f42eb030ce39b30b86a6519"), "username" : "test", "password" : "$2a$10$95H4m8QFoeeA0bF4klEb2OIQ8/SIz0raOJG" }

{ "_id" : ObjectId("5f42efa1b9f7e2321c55a9c1"), "username" : "admin", "password" : "$2a$10$G/gGYVVxO/RDuoDXIS0iSeFsKAOw9Cs8HlCa3U" }

I want to convert it to valid JSON object and prettify it for viewing. Some methods I tried:

let data = ({ "_id" : ObjectId("5f42eb030ce39b30b86a6519"), "username" : "test", "password" : "$2a$10$95H4m8QFoeeA0bF4klEb2OIQ8/SIz0raOJG" }

{ "_id" : ObjectId("5f42efa1b9f7e2321c55a9c1"), "username" : "admin", "password" : "$2a$10$G/gGYVVxO/RDuoDXIS0iSeFsKAOw9Cs8HlCa3U" })

1. data = data.trim().replace("} {", "}, {") //to put a comma (,) between tow {}.

2. data = JSON.stringify(data, null, 4); //It generates "backslash(\\)" after every word

3. data=JSON.parse(data) //It converts to String JSON but I want Object JSON.

After doing JSON.stringify((JSON.parse(data)), null, 4); this is the response I am getting:

 ["{ \"_id\" : ObjectId(\"5f42eb030ce39b30b86a6519\"), \"username\" : \"test\", \"password\" : \"$2a$10$95H4m8QFoeeA0bF4klEb2OIQ8/SIz0raOJG\" }, 
{ \"_id\" : ObjectId(\"5f42efa1b9f7e2321c55a9c1\"), \"username\" : \"admin\", \"password\" : \"$2a$10$G/gGYVVxO/RDuoDXIS0iSeFsKAOw9Cs8HlCa3U\" }"]

How can I convert it to valid JSON object and prettify it for viewing? Thank you in advance.

As an alternative you could use regex to escape the double quotes inside de ObjectId , and then fix the ObjectId . First insert a comma , between objects, the idea on your first list item was correct but maybe the separator was not a single space, or maybe was a line break , so it's better to use th regex /}\\s*{/ .

const resp = `{ "_id" : ObjectId("5f42eb030ce39b30b86a6519"), "username" : "test",
 "password" : "$2a$10$95H4m8QFoeeA0bF4klEb2OIQ8/SIz0raOJG" }
    { "_id" : ObjectId("5f42efa1b9f7e2321c55a9c1"), "username" : "admin", "password" :
 "$2a$10$G/gGYVVxO/RDuoDXIS0iSeFsKAOw9Cs8HlCa3U" }`;

fixMultObj = resp.replace(/}\s*{/, '}, {');
console.log(fixMultObj)
/*
{ "_id" : ObjectId("5f42eb030ce39b30b86a6519"), "username" : "test", "password" :
 "$2a$10$95H4m8QFoeeA0bF4klEb2OIQ8/SIz0raOJG" }, { "_id" :
ObjectId("5f42efa1b9f7e2321c55a9c1"), "username" : "admin", "password" :
 "$2a$10$G/gGYVVxO/RDuoDXIS0iSeFsKAOw9Cs8HlCa3U" }
*/

Now you can escape the double quotes surrounding the ObjectId hex value, and add the double quotes on the ObjectId itself.

fixQuotes = fixMultObj.replace(/ObjectId\("(\S+)"\)/g, '"ObjectId(\\"$1\\")"');
console.log(fixQuotes)
/*
{ "_id" : "ObjectId(\"5f42eb030ce39b30b86a6519\")", "username" : "test", "password" :
 "$2a$10$95H4m8QFoeeA0bF4klEb2OIQ8/SIz0raOJG" }, { "_id" :
"ObjectId(\"5f42efa1b9f7e2321c55a9c1\")", "username" : "admin","password" :
"$2a$10$G/gGYVVxO/RDuoDXIS0iSeFsKAOw9Cs8HlCa3U" }
*/

Make the array notation to be able to reach multiple objects.

makeArray = JSON.parse(`[${fixQuotes}]`)

console.log(makeArray)
/*
[ { _id: 'ObjectId("5f42eb030ce39b30b86a6519")',
    username: 'test',
    password: '$2a$10$95H4m8QFoeeA0bF4klEb2OIQ8/SIz0raOJG' },
  { _id: 'ObjectId("5f42efa1b9f7e2321c55a9c1")',
    username: 'admin',
    password: '$2a$10$G/gGYVVxO/RDuoDXIS0iSeFsKAOw9Cs8HlCa3U' } ]
*/

And get the desired values from the JSON object.

console.log(makeArray[1]._id)
// ObjectId("5f42efa1b9f7e2321c55a9c1")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM