Harkirat Saluja
Harkirat Saluja

Reputation: 8114

How to read a json string passed as command line parameter in node.js?

I am trying to run a script in node.js by passing a json string as a command line argument and then parsing this json string.

node ab.js  [{"url": "http://example"}]

Now in my script i do the following:-

var str=process.argv[2]

However when i try to print the variable str i am getting following output and not the whole json string.

[{url

Can some help me on this? I wanted the entire string to be stored in str variable.

Upvotes: 2

Views: 4137

Answers (1)

Paul
Paul

Reputation: 141877

You should quote your string so that it becomes a single argument:

node ab.js '[{"url": "http://example"}]'

Alternatively you could escape the special characters that you want your shell to ignore:

node ab.js [{\"url\":\ \"http://example\"}]

Upvotes: 2

Related Questions