2 years ago

#70483

test-img

Deepak

Can we pass an JSON to node js script from command line.?

I am trying to pass a JSON to Node js script from command line. I have tried "JSON.parse" but it is giving error "Unexpected token ' in JSON at position 1".

I have tried below things but no luck. Could someone please help. Here is my 'index.js'.


var args = process.argv;
var json = args[2];
console.log("JSON ::", json, "Type of JSON ::", typeof json);
console.log(
"JSON stringify ::",
JSON.stringify(json),
"Type of Stringify JSON ::",
typeof JSON.stringify(json)
);

From command line, I called the script with


node index.js "{'foo':'ball'}"

Here is the output :


JSON :: {'foo':'ball'} Type of JSON :: string
JSON stringify :: "{'foo':'ball'}" Type of Stringify JSON :: string

Type is showing as 'string'

node.js

command-line

command-line-interface

command-line-arguments

0 Answers

Your Answer

Accepted video resources