[Answer]-Django Decode Json error

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 + '"')
👤frank

Leave a comment