Home > Back-end >  How to get string from json python?
How to get string from json python?

Time:06-10

I have this json:

{'status': '1', 'message': 'OK', 
'result': {'LastBlock': '14934113', 'SafeGasPrice': '119', 'ProposeGasPrice': '119', 'FastGasPrice': '119', 
'suggestBaseFee': '118.174590189', 'gasUsedRatio': '0.025821954106323,0.354559890356924,0.173050329701518,0.340210087647076,0.0269005272339711'}}

How I can get strings from 'result'? I want to get 'SafeGasPrice'

CodePudding user response:

Want to work with Json object?
Use json inbuilt library in python

import json

# x is a json string where loads method convert json string into python dictionary
json.loads(x)

Read more on W3 school Json libarary python

CodePudding user response:

import json
data = json.loads('{"status": "1", "message": "OK", "result": {"LastBlock": "14934113", "SafeGasPrice": "119", "ProposeGasPrice": "119", "FastGasPrice": "119", "suggestBaseFee": "118.174590189", "gasUsedRatio": "0.025821954106323,0.354559890356924,0.173050329701518,0.340210087647076,0.0269005272339711"}}')
SafeGasPrice = data["result"]["SafeGasPrice"]
print(SafeGasPrice)

CodePudding user response:

I first get the values from result: gas_unit_price = gas_unit_price.json()["result"] Second I get the string I need: gas_unit_price = gas_unit_price["FastGasPrice"]

  • Related