-4

I have below data , how can I convert user data to json in python?

results= [
            {
                "created_at": 1632712032050,
                "user": "{\"id\": \"12\", \"name\": \"test\"}"
            },
            {
                "created_at": 1632712032050,
                "user": "{\"id\": \"13\", \"name\": \"test\"}"
            }
         ]

expected output:

results= [
            {
                "created_at": 1632712032050,
                "user": {"id": "12", "name": "test"}
            },
            {
                "created_at": 1632712032050,
                "user": {"id": "13", "name": "test"}
            }
         ]
test
  • 133
  • 1
  • 8

1 Answers1

0

Use the json module of python

import json

results= [
            {
                "created_at": 1632712032050,
                "user": "{\"id\": \"12\", \"name\": \"test\"}"
            }
         ]

print(results) #in string format

results[0]['user'] = json.loads(results[0]['user'])

print(results) #after parsing its now json dictionary/object
Zain Ul Abidin
  • 2,467
  • 1
  • 17
  • 29