0

I'm working with the Readline module in NodeJS and would like to parse the content of what the user wrote as code. Meaning if someone writes:

{
    name: "David",
    age: 34
}

I should be able to JSON.stringify(content) and get:

{
    "name": "David",
    "age": "34"
}

How can I convert a string in to actual code, so it can be interpreted as a JavaScript object, thus be converted in to JSON using JSON.stringify()?

6
  • Can you make them type valid JSON so you do not have to eval the string? evaling untrusted code can be unsafe.... Commented Dec 29, 2016 at 15:41
  • I'm building a Command Line Tool, so unless the user wants to brake his own machine he or she is welcome to do so :P Commented Dec 29, 2016 at 15:44
  • Still seems easier to just require valid JSON. Commented Dec 29, 2016 at 15:46
  • The point of the tool is to convert JS Object to JSON, you know so you don't have to do it by hand if you have big and complicated objects. Computers FTW ;) Commented Dec 29, 2016 at 15:47
  • So maybe a library? github.com/freethenation/durable-json-lint or regular expression: stackoverflow.com/questions/18280279/… I have been know to do the new Function hack in the browser to convert objects... Just offering up other ideas. Commented Dec 29, 2016 at 15:49

2 Answers 2

1

It's not entirely clear what you're asking, but, would JSON.parse() help you here? You'll want to wrap it in a try catch in case the input is not valid JSON.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Sign up to request clarification or add additional context in comments.

3 Comments

o_O bit confused ;) seams pretty clear what I'm asking, in the end there is a questions (I can be wrong hear). Anyway, JSON.parse() is used to convert JSON in to a JavaScript object. My problem is that what I'm typing is formatted as a JavaScript object. But process.stdin gives me back ASCII and not JavaScript. Not sure how else to put it :( Hope this now makes more sense.
Ok, I think that clears it up. If your input is a string, JSON.parse() will convert it to a JavaScript object. What I see here, though is that your input is not valid JSON (Missing quotes around attribute names). As you stated, JSON.stringify() takes a JavaScript object as input and outputs a string. Given that your input is not JSON in this case, it won't work as you expect.
Tru, that was a typo. Fixed. But I think I found the solution. I'll post it in a moment.
1

The trick to make this work is to use the VM module in a undocumented way as seen below.

let vm = require('vm');

let script = new vm.Script('x = ' + full_content);

let sandbox = script.runInThisContext();

converted = JSON.stringify(sandbox);

Basically you have to create a variable that will hold your string (JavaScript), which will be then converted in to proper JavaScript code thanks to .runInThisContext().

In this case the x variable will "disappear" and you don't have to think about that. But if you were to follow the NodeJS documentation example, and use .runInNewContext() then the x variable wouldn't go away, a you would have your object (in my case at least) assigned to the x variable.

Hope this helps :)

4 Comments

Thanks 2h of starting at the screen and trying to get out of the box :D ps. if you think this is a good answer a + would be nice ;)
And the person types (function(){ x=0; while(true) x++;})() does it kill the VM?
And their machine will get worm, good for long and dark winter nights ;)
You can set a timeout to avoid an infinite loop. There are several ways to handle errors using VM.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.