0

I have this json data that I post to Djangp

[[LAT2:1.3178775, LON1:103.7608174, LON2:103.7733836, LAT1:1.3104325, YPIXELS:378, XPIXELS:400, MINZ:40, XKNOTS:26, YKNOTS:24, MAXZ:80], [node:51, z:63.462589, y:1.312762, x:103.766148]] 

Getting error: Expecting value: line 1 column 3 (char 2)

My codes:

jsonStr = request.body.decode(encoding='UTF-8') jsonObj = json.loads(jsonStr) 
0

1 Answer 1

1

Your string isn't valid json. key:value pairs need to be encompassed in curly brackets, and keys must be quoted. For example: {"key" : "value"}

At minimum you're going to have to transform your string into this format:

[{"LAT2":1.3178775, "LON1":103.7608174, "LON2":103.7733836, "LAT1":1.3104325, "YPIXELS":378, "XPIXELS":400, "MINZ":40, "XKNOTS":26, "YKNOTS":24, "MAXZ":80}, {"node":51, "z":63.462589, "y":1.312762, "x":103.766148}]'

Here's a way to do it without regular expressions, if you know the keys in the string will be consistent:

 # Bracket the {key : value} structures: input = '[' + input[1:-1].replace('[', '{').replace(']', '}') + ']' # Wrap the keys in double quotes: keys = ('LAT2', 'LON1', 'LON2', 'LAT1', 'YPIXELS', 'XPIXELS', 'XKNOTS', 'YKNOTS', 'MINZ', 'MAXZ', 'node', 'z', 'y', 'x') for key in keys: input = input.replace(key, '"' + key + '"') 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.