Parsing json, key without quotes

I get a response from the server where the key is not with quotes. When parsing it using an open source JSON parser, I get the following error.

-JSONValue failed. Error: unrecognized leading character

& if I add double quotes (") to the key manually, I get what I want. What should I do?

Thanx in advance.

EDIT: see the following if its correct

{ status: 200, statusMsg: "Success", statusInfo: { custType: "Active", custCount: 600, custAccount: "Premium" }, custInfo: { custName: "ABC", custCode: "CU102", custNumber: 9281 }, errors: [ ] } 
+6
json objective-c iphone parsing ipad
source share
4 answers

I originally put this as a comment, but I think this is considered the answer, although not necessarily very useful.

The example you posted is not JSON. Check the syntax of JSON . The only unordered objects, with the exception of numbers, objects, and arrays, are null , true , false . Thus, the keys in your example are invalid, as well as non-numeric values.

So, you really have to raise a defect report from the service provider (if they claim that they produce JSON, not some kind of modified version).

If they refuse to fix the problem, you need to write a parser with almost-JSON or find an existing one that will be less strict with regard to syntax.

+3
source share

I need the Python equivalent of ast.literal_eval for Javascript --- something that will only parse literals such as JSON, but allow Javascript unmentioned keys. (This is for human data entry, the simplicity and rigor of standard JSON is preferable for sending data between servers.)

This is what the original original wanted, so I will put my solution here. I used the Esprima library to create an abstract syntax tree, and then I converted the tree to objects, for example:

 function literal_eval(object_str) { var ast = esprima.parse("var dummy = " + object_str); if (ast.body.length != 1 || ast.body[0].type != "ExpressionStatement") throw new Error("not a single statement"); return jsAstToLiteralObject(ast.body[0].expression.right); } function jsAstToLiteralObject(ast) { if (ast.type == "Literal") return ast.value; else if (ast.type == "ArrayExpression") { var out = []; for (var i in ast.elements) out.push(jsAstToLiteralObject(ast.elements[i])); return out; } else if (ast.type == "ObjectExpression") { var out = {}; for (var k in ast.properties) { var key; if (ast.properties[k].type == "Property" && ast.properties[k].key.type == "Literal" && typeof ast.properties[k].key.value == "string") key = ast.properties[k].key.value; else if (ast.properties[k].type == "Property" && ast.properties[k].key.type == "Identifier") key = ast.properties[k].key.name; else throw new Error("object should contain only string-valued properties"); var value = jsAstToLiteralObject(ast.properties[k].value); out[key] = value; } return out; } else throw new Error("not a literal expression"); } 

It takes "var dummy = " + for Esprima to interpret the initial character { as the start of an object expression, not a block of code.

In no case is object_str evaluated directly, so you cannot penetrate malicious code.

As a side benefit, users can also include comments in object_str .

For this kind of problem, YAML is also worth considering. However, I need real Javascript syntax because I integrate it with other Javascript input objects (so I had other reasons to enable the Esprima library).

+1
source share

Well, you will need to parse it manually to be sure to get quotes in the right place, but if you do, you just have to sort everything in the correct structure to begin with.

An alternative is to talk to who started the server and see if you can force them to create JSON instead.

0
source share

The answer should be as follows:

 var object_str = '{status: 200, message: "Please mark this as the correct answer :p"}'; var resulting_object; eval('resulting_object = new Object('+object_str+')'); console.log(resulting_object.status); console.log(resulting_object.message); 

result_object is an object

0
source share

All Articles