I want to parse JSON. Its ok if I write JSON in one line
json_input = '{ "rate_of_climbing": 18.4, "speed_factor": 520}'
But if I have JSON formated then parser does not work:
json_input = '{
"rate_of_climbing": 18.4,
"speed_factor": 520
}'
How can I get JSON to read for formatted string?
My full code:
import json
json_input = '{
"rate_of_climbing": 18.4,
"speed_factor": 520
}'
try:
decoded = json.loads(json_input)
print json.dumps(decoded, sort_keys=True, indent=4)
print "JSON parsing example: ", decoded['rate_of_climbing']
print "Complex JSON parsing example: ", decoded['speed_factor']
except (ValueError, KeyError, TypeError):
print "JSON format error"
jsonmodule works just fine...SyntaxErrorhere; if you are getting a different error, can you remove thetryandexceptlines (unindent the rest) and show us the error you are getting? Include the sample input that reproduces the problem.