Vinyl Warmth
Vinyl Warmth

Reputation: 2506

How can I parse a JavaScript file to an object with Node

I need to parse a JavaScript file that exists on the Web with Node.

The file looks as follows:

var obj = {"data": [{"property1":40}]}

Is there any way I can download the file as a string, and parse it so it to an object? i.e. so that after I have parsed the file I can do:

var data = obj.data;

Upvotes: 2

Views: 647

Answers (1)

Darth
Darth

Reputation: 1651

You can use eval for it, but be careful - you can also execute unwanted code

const fileContent = 'var obj = {"data": [{"property1":40}]}';
eval(fileContent);
var data = obj.data;
console.log(data);

Upvotes: 2

Related Questions