2

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.

1
  • Have you tried commander? I use it quite a lot and it handles that case perfectly. Commented Jan 11, 2016 at 13:26

1 Answer 1

2

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\"}]
Sign up to request clarification or add additional context in comments.

1 Comment

be ware, add single quote might not compatible in Windows system

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.