mardi 5 mai 2015

How to pass VALID json on the command line to a Node.js application?

I'm attempting to pass, as a single command line argument, some valid JSON to my node application. However, my current attempt is failing because by the time the parameter is passed into my node application, the double quotes have been stripped. This makes the JSON invalid at best, but if there are spaces in any of the property values, it causes serious problems. This example node application will demonstrate the problem:

console.log(process.argv);

Run this with the following command-line arguments:

node test '{"name": "John Doe"}'

The output of this command is this:

[ 'C:\\Program Files\\nodejs\\node.exe',
'C:\\project\\test',
'{name:John',
'Doe}' ]

Is there anyway to prevent the double quotes from being stripped away?

Aucun commentaire:

Enregistrer un commentaire