I have the following string of data:
{"responseStatus":"ok","responseHeader":{"now":1528734419187,"status":"ok","requestId":"Wx6i0wquZS4AAFNeStwAAABg"},"responseData":{"id":38}}
and I need to pull the "id":38 out of it and format it a "id":"38"
I have the following string of data:
{"responseStatus":"ok","responseHeader":{"now":1528734419187,"status":"ok","requestId":"Wx6i0wquZS4AAFNeStwAAABg"},"responseData":{"id":38}}
and I need to pull the "id":38 out of it and format it a "id":"38"
since you're using the requests
library (based on your tag), when calling your repsonse, instead of returning .content
, you can return .json()
:
response = requests.get(....).json()
response['responseData']['id'] = str(response['responseData']['id']) # or you can just do "38"
You could use the json
module:
import json
json_s = json.loads('{"responseStatus":"ok","responseHeader":{"now":1528734419187,"status":"ok","requestId":"Wx6i0wquZS4AAFNeStwAAABg"},"responseData":{"id":38}}')
json_s['responseData']['id'] = '38' # Or str(json_s['responseData']['id'])
print(json_s)
Or in this case it is a valid dictionary so you could use eval()
:
json_s = '{"responseStatus":"ok","responseHeader":{"now":1528734419187,"status":"ok","requestId":"Wx6i0wquZS4AAFNeStwAAABg"},"responseData":{"id":38}}'
a = eval(json_s)
a['responseData']['id'] = '38' # Or str(a['responseData']['id'])
print(a)
If you want to convert the dictionary in json_s
and a
back to a string then just use str(json_s)
and str(a)
respectively.
See the documentation for the use of eval()
.
Otherwise to keep away from eval()
use the ast
module. As described here:
Convert a String representation of a Dictionary to a dictionary?