Python Forum
extract specific data from a group of json-files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extract specific data from a group of json-files
#2
First you should know the data structure of the json file.
You can investigate it, if you open the file with Python and use json.loads() on the open file.

import json

with open('your_data0001.json') as fd:
    data = json.loafs(fd)
Usually json data is a dictionary with subdictionaries and lists.
To show the keys, you can use list(data.keys()).
If you find the right key, you can dig deeper.

For example if you have the key 'metadata', accessing it, is very easy:
data['metadata']
The value of 'metadata' could be a list or a dict or something else (int, float, str).


After you know the structure, you can write a transformer function for it.
It should transform the json data into the form you want to have.

This is just an example and do not have to fit on your data.
def transformer(mapping_from_json):
    """
    A generator which takes a mapping (dict)
    and yields name, age, active
    """
    for items in mapping_from_json['results']['metadata']:
        # if metadata is a list
        for element in items:
            name = element.get('name', 'NO NAME')
            age = element.get('age', 0)
            active = element.get('active', False)
            yield (name, age, active)
I used the generator, because the logic is easier to understand. Always if a yield is in a function, then it's returning a generator, if you call the function. Iterating over the generator, yields the elements.

To consume it, just use something, which takes iterables or use a for loop.
list(transformer(my_dict))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: extract specific data from a group of json-files - by DeaD_EyE - Dec-05-2019, 09:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why can't it extract the data from .txt well? Melcu54 4 1,895 Dec-12-2024, 07:36 PM
Last Post: Melcu54
  Write json data to csv Olive 6 1,511 Oct-22-2024, 06:59 AM
Last Post: Olive
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,202 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  Python script to extract data from API to database melpys 0 1,101 Aug-12-2024, 05:53 PM
Last Post: melpys
  Trying to generating multiple json files using python script dzgn989 4 2,656 May-10-2024, 03:09 PM
Last Post: deanhystad
  encrypt data in json file help jacksfrustration 1 2,427 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 4,096 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  data validation with specific regular expression shaheen07 0 934 Jan-12-2024, 07:56 AM
Last Post: shaheen07
Question Need help for a python script to extract information from a list of files lephunghien 6 2,761 Jun-12-2023, 05:40 PM
Last Post: snippsat
  script to calculate data in csv-files ledgreve 0 2,491 May-19-2023, 07:24 AM
Last Post: ledgreve

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020