Home > Net >  How to extract value of a key from an object of arrays in python
How to extract value of a key from an object of arrays in python

Time:04-24

This might be a real noob question.

Thanks for helping.

I am receiving a json payload with the below structure

 {
    "response": true,
    "error": null,
    "msg": null,
    "data": {
        "slots": {
            "04/26/2022": [
                {
                    "0": 1650958200000,
                    "1": 19800,
                    "3": "1:00 PM",
                    "4": "26/Apr/2022 13:00 IST"
                },
                {
                    "0": 1650960000000,
                    "1": 19800,
                    "3": "1:30 PM",
                    "4": "26/Apr/2022 13:30 IST"
                },
                {
                    "0": 1650963600000,
                    "1": 19800,
                    "3": "2:30 PM",
                    "4": "26/Apr/2022 14:30 IST"
                },
                {
                    "0": 1650965400000,
                    "1": 19800,
                    "3": "3:00 PM",
                    "4": "26/Apr/2022 15:00 IST"
                },
                {
                    "0": 1650967200000,
                    "1": 19800,
                    "3": "3:30 PM",
                    "4": "26/Apr/2022 15:30 IST"
                }
            ],
            "04/24/2022": [],
            "04/25/2022": [],
            "04/23/2022": []
        }
    }
}

I want to create a new object with an array of the values of "0". Output should be

{
 "slots": [
          1650958200000,
          1650960000000,
          1650963600000,
          1650965400000,
          1650967200000
          ]
}

How would i go about doing this?

CodePudding user response:

Here's a straight-forward approach:

slots = payload["data"]["slots"]  # Assumes payload has been converted from JSON to Python

slots_obj = {"slots": [d["0"] for _, lst in slots.items() for d in lst]}

Output:

{'slots': [1650958200000,
           1650960000000,
           1650963600000,
           1650965400000,
           1650967200000,
           16539137693178]}

If you haven't already, use json.loads to convert the JSON to a Python dictionary:

import json

payload = json.loads(payload)

The solution above uses what's known as a list comprehension. It is equivalent to a for loop, which may be easier to understand:

timestamps = []  # Initialize an empty list to accumulate the Unix timestamps

# Iterate over each date, array pair in the slots dictionary using slots.items()
for _, slot_array in slots.items():
    # Iterate over each inner dictionary
    for slot_dict in slot_array:
        # Grab the value at the "0" and append it to the timestamps accumulator
        timestamps.append(slot_dict["0"])

# Finally create the new object, a dictionary with one key, "slots"
slots_obj = {"slots": timestamps}

CodePudding user response:

Using the json module as in ddejohn's approach will be more efficient than the approach shown here. However, I thought it would be useful for you to see other ways of obtaining your desired output.

If the above is a string, say, s, you can use findall() from the re module to extract those numbers.

import re

p = re.compile(r'(?<="0": )\d (?=,)')
d = {'slots': [int(x) for x in p.findall(s)]}

print(d)

Output

{'slots': [1650958200000,
           1650960000000,
           1650963600000,
           1650965400000,
           1650967200000]}

The regular expression r'(?<="0": )\d (?=,)' matches one or more digits that are preceded by "0": and that are followed by a ,. findall(s) returns all (nonoverlapping) matches of this expression in the string s as a list of strings. In particuar, p.findall(s) returns the list

['1650958200000',
 '1650960000000',
 '1650963600000',
 '1650965400000',
 '1650967200000']

[int(x) for x in p.findall(s)] creates a new list formed by taking each element of the list returned by p.findall(s) and converting it into an integer.

Timings


Using the json module as in ddejohn's approach will be more efficient.

# ddejohn
6.14 µs ± 41 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

# oda
14.6 µs ± 76.1 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

CodePudding user response:

I did the next program, i hope that will be useful to you.

But you should have on mind that you will probably lose important information

The explanation of how It's works is in the program

import json

dicc={}

# We open the file
with open('data.json', 'r') as f:
    data = json.JSONDecoder().decode(f.read())

    # We get the data
    slots=data['data']['slots']

    # We travel to the slots and we get the key
    for slot in slots:

        # We get the data and put them in the dictionary
        for key in slots[slot]:

            # If slots key is not created, we create it
            if "slots" not in dicc:

                # We create the list that will contain the data
                dicc["slots"]=[]

            #We add the data to the list in the dictionary
            dicc["slots"].append(key['0'])

new_json=json.dumps(dicc)
  • Related