0
var filter1 = " { where:{ businessName:{ $like:'%A'} ,'userId': { $gt: 0} }, limit: 1 }"

I want to make pass it like this JSON

  var filter =  { where:{ businessName:{ $like:'%A'} ,'userId': { $gt: 0} }, limit: 1 } 

I did this to make it like JSON example.

JSON.parse(filter)

But it throws the following exception:

SyntaxError: Unexpected token w in JSON at position 3

1
  • 2
    Well your string is no JSON. Commented Nov 29, 2016 at 7:18

2 Answers 2

2

Your JSON is not valid. Use "" with all keys and for values, except numbers and bools

var filter1 = '{ "where": { "businessName" :{ "$like":"%A"} ,"userId": { "$gt": 0} }, "limit": 1 }'

var filter1 = '{ "where": { "businessName" :{ "$like":"%A"} ,"userId": { "$gt": 0} }, "limit": 1 }';

var filter = JSON.parse(filter1);

console.log(filter);

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

Comments

1

It's not valid JSON string. It's more a JavaScript object literal wrapped in quotes.

In JSON standard all keys should be wrapped in quotes, so here is how your JSON would look like:

"{"where":{"businessName":{"$like":"%A"},"userId":{"$gt":0}},"limit":1}"

Since your string is simply a JavaScript object wrapped in quotes, you can arrive at the correct JSON string by simply removing quotes:

var filter1 = { where:{ businessName:{ $like:'%A'} ,'userId': { $gt: 0} }, limit: 1 }

and running it through JSON.stringify:

JSON.stringify(filter1);

4 Comments

my query engine was unable to recognize it using stringfy
@danielad, sorry, didn't understand your comment
yeah , i have to use that json as piece of where query and using stringfy the where query is not recognized.
@danielad, if you have correct JSON string "{"where":{"businessName":{"$like":"%A"},"userId":{"$gt":0}},"limit":1}" and you want to have an object, then you'd have to use JSON.parse. If you have simple JavaScript object and you want to have it in JSON, use JSON.stringify

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.