Asked  7 Months ago    Answers:  5   Viewed   32 times

How should I parse JSON using Node.js? Is there some module which will validate and parse JSON securely?

 Answers

94

You can simply use JSON.parse.

The definition of the JSON object is part of the ECMAScript 5 specification. node.js is built on Google Chrome's V8 engine, which adheres to ECMA standard. Therefore, node.js also has a global object JSON[docs].

Note - JSON.parse can tie up the current thread because it is a synchronous method. So if you are planning to parse big JSON objects use a streaming json parser.

Tuesday, June 1, 2021
 
Bere
answered 7 Months ago
84

That response is a Map, with a single element with key '212315952136472'. There's no 'data' key in the Map. If you want to loop through all entries, use something like this:

JSONObject userJson = JSON.parse(jsonResponse)
userJson.each { id, data -> println data.link }

If you know it's a single-element Map then you can directly access the link:

def data = userJson.values().iterator().next()
String link = data.link

And if you knew the id (e.g. if you used it to make the request) then you can access the value more concisely:

String id = '212315952136472'
...
String link = userJson[id].link
Tuesday, June 29, 2021
 
Manmay
answered 6 Months ago
51

The first argument passed to pgsql function json_populate_recordsetshould be a row type. If you want to use the json array to populate the existing table anoop you can simply pass the table anoop as the row type like this:

insert into anoop
select * from json_populate_recordset(null::anoop, 
        '[{"id":67272,"name":"EE_Quick_Changes_J_UTP.xlsx"},
          {"id":67273,"name":"16167.txt"},
          {"id":67274,"name":"EE_12_09_2013_Bcum_Searchall.png"}]');

Here the null is the default value to insert into table columns not set in the json passed.

If you don't have an existing table, you need to create a row type to hold your json data (ie. column names and their types) and pass it as the first parameter, like this anoop_type:

create TYPE anoop_type AS (id int, name varchar(100));
select * from json_populate_recordset(null :: anoop_type, 
        '[...]') --same as above
Sunday, August 15, 2021
 
Sisyphus
answered 4 Months ago
54

You can write to csv (comma-separated values) text file without any additional library. This extension open in Excel by default.

var fs = require('fs');
var file = fs.createWriteStream('file.csv', {'flags': 'w', autoClose: true});
var result = '';
for (var hashkey in myObject) 
    result += hashkey + ';' + myObject[hashkey].keyA + ';' + myObject[hashkey].keyB + 'n';
file.write(result); 
Monday, August 30, 2021
 
Joel
answered 3 Months ago
71

It is a Headers object. It has e.g. get and forEach methods. For example:

getDownload = async (url) => {
    const response = await fetch(url);
    console.log(response.headers.get('content-type'));
    return {
        name: response.headers.get('Content-Disposition'),
        length: response.headers.get('content-length')
    }
}

Note the case insensitivity.

Sunday, October 10, 2021
 
asator
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share