I have json data in a string, and I would like to extract the data within the "data" tag and return the data in a json format.
"data": { "id": 00001, "approved": true, "created_at": "2022-12-21T12:59:43Z", "credit": 0, "debit": 30, "description": "Deposit 30", "payment_currency_id": 1012, "player_id": 10001, "transaction_id": 00001 }
I'm trying to remove the "data" tag and just return its contents i.e
{ "id": 00001, "approved": true, "created_at": "2022-12-21T12:59:43Z", "credit": 0, "debit": 30, "description": "Deposit 30", "payment_currency_id": 1012, "player_id": 10001, "transaction_id": 00001 }
I tried the below but it changes the format of the json string
data = json.loads(payload)
df = pd.json_normalize(data['data'])
CodePudding user response:
00001
is not valid JSON.
Using pandas
for this task is overhead. Simply load the json string into an object, get data
and convert back to json:
import json
s = '''{"data": { "id": "00001", "approved": true, "created_at": "2022-12-21T12:59:43Z", "credit": 0, "debit": 30, "description": "Deposit 30", "payment_currency_id": 1012, "player_id": 10001, "transaction_id": "00001" }}'''
only_data = json.dumps(json.loads(s)['data'])
print(only_data)
Output:
{"id": "00001", "approved": true, "created_at": "2022-12-21T12:59:43Z", "credit": 0, "debit": 30, "description": "Deposit 30", "payment_currency_id": 1012, "player_id": 10001, "transaction_id": "00001"}