admin管理员组

文章数量:1304893

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

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"
                }
            ]
        }
    ]

}

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

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"
                }
            ]
        }
    ]

}
Share Improve this question edited Aug 5, 2017 at 10:25 steliosbl 8,9214 gold badges32 silver badges57 bronze badges asked Aug 5, 2017 at 7:46 Om ChaturvediOm Chaturvedi 1031 gold badge2 silver badges10 bronze badges 10
  • What is the error, what doesn't work? You've only got half a question. – ste2425 Commented Aug 5, 2017 at 7:48
  • 1 JSON.parse... – Pranav C Balan Commented Aug 5, 2017 at 7:48
  • Please add code runs converting. – sfrehse Commented Aug 5, 2017 at 7:48
  • Error in console log --Uncaught SyntaxError: Invalid or unexpected token – Om Chaturvedi Commented Aug 5, 2017 at 7:53
  • 1 @OmChaturvedi I just tried this in chrome dev tools looks fine tbh imgur./a/LslsH – adiga Commented Aug 5, 2017 at 7:59
 |  Show 5 more ments

3 Answers 3

Reset to default 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));

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

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

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

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

Just using

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

本文标签: javascriptJSONparse is not working for converting JSON string to JSON objectStack Overflow