Reputation: 8114
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
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