2

I would like to allow users to specify JavaScript object in a HTML textarea which I would then parse. But JSON looks too strict for me, requiring only " as quotes, and quotes everywhere. I would like that you could specify the object in ways you can specify it in JavaScript, and then parse it. How could I parse such string (in JavaScript itself)? Is there a library for it?

I know that I could use eval, but I would not like to allow custom code to run, just static object definition.

I am trying to provide a simple way for users to write MongoDB query.

3
  • You're looking for a "JavaScript language parser written in JavaScript". Consider including "parser generator" in your search terms. See also stackoverflow.com/questions/2554519/… Commented Dec 12, 2014 at 23:42
  • eval is a simple way. Depending on how exactly the input is used, it won't even be bad (from a security standpoint). Alternatives: Use a JS parser, walk the AST and extract the information you need. Or create your own parser that is a bit more relaxed than JSON (e.g. via pegjs.majda.cz or zaach.github.io/jison). Commented Dec 12, 2014 at 23:44
  • OK, those answers in the other question provide just answers how to parse whole JavaScript. I could now spend some time designing a grammar or making sure that only objects are read in the AST tree and then convert them manually to normal objects. But my question is: is there something which is already made for parsing only JavaScript objects. Commented Dec 13, 2014 at 5:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.