0

I have a string which is made on run-time depends on user inputs. When user enters some searching criteria the string is creating based on that input and the string is actually an complex objects. When i am trying to JSON.parse() and pass it to the mongoDB find() method to find data in a database and that gives me an error and that is fine. But my object are not a simple key-values pairs actually my objects are key and values are other object to in this case ithink the JSON.parse() method is failing to convert it to a actual JSON Object. I tried some custom implementations as well but i failed alot...

Dynamically Created String

"{ "name": { $regex: name, $options: "i" }, "code": { $regex: code, $options: "i" }, "phoneNumber": { $regex: phone, $options: "i" }, "nic": { $regex: cnic, $options: "i" }"

When i try to JSON.parse() it i am getting an error UNEXPECTED token error at position $....

Error Image

enter image description here

Help will be appreciated... Thanks in advance

1 Answer 1

2

In JSON all names of objects and their properties must be enclosed in " double-quote characters. Your string has names like $regex and $options without the double-quote characters. Put in those characters and you should be good to go.

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

2 Comments

i tried this but agian in am getting in error...i am posted image to my question.
You need another } at the end of your JSON string. Balance your curly braces.

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.