Python json Specific Value Extraction

Asked 2 years ago, Updated 2 years ago, 54 views

{
    "test": [
        {
            "customer": "test1",
            "type": [
                "windows"
            ],
            "contact": [{
                "name": "a",
                "email": "@gmail.com",
                "phone": null ,
                "remarks": null
            }]
        },
        {
            "customer": "test2",
            "type": [
                "android"
            ],
            "contact": [{
                "name": "b",
                "email": "@gmail.com",
                "phone": "010-0000-0000",
                "remarks": null
            }]
        },
        {
            "customer": "test3",
            "type": [
                "android"
            ],
            "contact": [{
                "name": "a",
                "email": "@naver.com",
                "phone": null,
                "remarks": null
            }]
        }
    ]
}
import json

with open('test.json', 'rt', encoding='UTF8') as f:
    content = json.load(f)
email_list = [ x for x in content['omnidoc']['contact']['email'].keys()]

I want to get only the email data of the json file from Python as a list, what should I do? And I want to remove the duplication.

There's this error here.

TypeError: list indices must be integers or slices, not str

python3 json list

2022-09-21 23:11

1 Answers

Please keep that in mind.

import json
from pprint import pprint

s = '''{
    "test": [
        {
            "customer": "test1",
            "type": [
                "windows"
            ],
            "contact": [{
                "name": "a",
                "email": "@gmail.com",
                "phone": null ,
                "remarks": null
            }]
        },
        {
            "customer": "test2",
            "type": [
                "android"
            ],
            "contact": [{
                "name": "b",
                "email": "@gmail.com",
                "phone": "010-0000-0000",
                "remarks": null
            }]
        },
        {
            "customer": "test3",
            "type": [
                "android"
            ],
            "contact": [{
                "name": "a",
                "email": "@naver.com",
                "phone": null,
                "remarks": null
            }]
        }
    ]
}'''

d = json.loads(s)
print('------')

pprint(d)
print('------')

for e in d['test']:
    pprint(e['contact'])
print('------')

for i, e in enumerate(d['test']):
    print(i, e['contact'][0]['email'])
print('------')

emails = { e['contact'][0]['email'] for e in d['test']}
print(emails)
print('------')

Results.

------
{'test': [{'contact': [{'email': '@gmail.com',
                        'name': 'a',
                        'phone': None,
                        'remarks': None}],
           'customer': 'test1',
           'type': ['windows']},
          {'contact': [{'email': '@gmail.com',
                        'name': 'b',
                        'phone': '010-0000-0000',
                        'remarks': None}],
           'customer': 'test2',
           'type': ['android']},
          {'contact': [{'email': '@naver.com',
                        'name': 'a',
                        'phone': None,
                        'remarks': None}],
           'customer': 'test3',
           'type': ['android']}]}
------
[{'email': '@gmail.com', 'name': 'a', 'phone': None, 'remarks': None}]
[{'email': '@gmail.com',
  'name': 'b',
  'phone': '010-0000-0000',
  'remarks': None}]
[{'email': '@naver.com', 'name': 'a', 'phone': None, 'remarks': None}]
------
0 @gmail.com
1 @gmail.com
2 @naver.com
------
{'@gmail.com', '@naver.com'}
------


2022-09-21 23:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.