2

Need help to convert below JSON string into JSON object.Even string JSON is valid json (verified by https://jsonlint.com/).

JSON:

{
    "condition": "AND",
    "rules": [{
            "id": "amount",
            "operator": "greater_or_equal",
            "value": "900"
        },
        {
            "condition": "AND",
            "rules": [{
                    "id": "vendorname",
                    "operator": "equal",
                    "value": "US BANK NATIONAL ASSOCIATION"
                },

                {
                    "id": "vendorname",
                    "operator": "equal",
                    "value": "HANSEN SOLUTIONS  LLC"
                }
            ]
        }
    ]

}
10
  • What is the error, what doesn't work? You've only got half a question. Commented Aug 5, 2017 at 7:48
  • 1
    JSON.parse... Commented Aug 5, 2017 at 7:48
  • Please add code runs converting. Commented Aug 5, 2017 at 7:48
  • Error in console log --Uncaught SyntaxError: Invalid or unexpected token Commented Aug 5, 2017 at 7:53
  • 1
    @OmChaturvedi I just tried this in chrome dev tools looks fine tbh imgur.com/a/LslsH Commented Aug 5, 2017 at 7:59

3 Answers 3

4

Your JSON string is multiline. Multiline string should be stored using template literals, otherwise use string concatenation to represent your string.

The below exmaple uses template literals. It is used to represent multi line string.

var str = `{
	"condition": "AND",
	"rules": [{
			"id": "amount",
			"operator": "greater_or_equal",
			"value": "900"
		},
		{
			"condition": "AND",
			"rules": [{
					"id": "vendorname",
					"operator": "equal",
					"value": "US BANK NATIONAL ASSOCIATION"
				},

				{
					"id": "vendorname",
					"operator": "equal",
					"value": "HANSEN SOLUTIONS  LLC"
				}
			]
		}
	]
}`;

console.log(JSON.parse(str));

This is a single line string.

var str = '{"condition":"AND","rules":[{"id":"amount","operator":"greater_or_equal","value":"900"},{"condition":"AND","rules":[{"id":"vendorname","operator":"equal","value":"US BANK NATIONAL ASSOCIATION"},{"id":"vendorname","operator":"equal","value":"HANSEN SOLUTIONS  LLC"}]}]}';

console.log(JSON.parse(str));

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

2 Comments

Not sure why you were down voted. This is the likely issue. But also not really sure why OP is creating a string to convert to an object when he could just create the object in the first place.
Even I am not sure What is OP trying to do.
3

Need help to convert below JSON string into JSON object.Even string JSON is valid json (verified by https://jsonlint.com/).

JSON.parse(jsonString); Is a pure JavaScript approach so long as you can require a reasonably modern browser.

See also https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Update: Try JSON.parse(JSON.stringify(TheString))

5 Comments

Thanks for your input...I am using chrome browser
You are welcome. This should solve your problem: JSON.parse(jsonString);
@ Spacemoose , I am already using JSON.parse.See the issue title
He probably was down voted because the title states "JSON.parse is not working" and his submitted answer is basically "use JSON.parse". Huh?
try JSON.parse(JSON.stringify(yourString))
0

Just using

try {
      let obj = JSON.parse( string);
} catch( e) {
    // conversion fails
   console.error( e ) 
} 

1 Comment

However, let is not available on some Browser. But typescript should work. ☺️

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.