Skip to content Skip to sidebar Skip to footer

Search For Combinations In JSON Nested Object

I have a large JSON object. A piece of it is: data = [ { 'make': 'dacia', 'model': 'x', 'version': 'A', 'typ': 'sedan', 'infos': [ {'id': 1, 'name': 's

Solution 1:

Okay, so first you need to get all of the actual "infos" from your json data like so:

infos = [
    [i["name"] for i in d["infos"]] if isinstance(d["infos"], list) else d["infos"]
    for d in data
]

This will give you something like below which we will use later:

[['steering wheel problems', 'ABS errors'], ['throttle problems', 'broken handbreak', 'missing seatbelts']]

Now, to get all of the combinations, we first need to process this by flattening the infos array and weeding out duplicates:

unique_infos = [x for l in infos for x in l]

To get all of the combinations:

infos_combo = itertools.chain.from_iterable(
    itertools.combinations(unique_infos, r) for r in range(len(unique_infos) + 1)
)

which will yield:

()
('steering wheel problems',)
('ABS errors',)
('throttle problems',)
('broken handbreak',)
('missing seatbelts',)
('steering wheel problems', 'ABS errors')
('steering wheel problems', 'throttle problems')
('steering wheel problems', 'broken handbreak')
...
# truncated code too long
...
('steering wheel problems', 'throttle problems', 'broken handbreak', 'missing seatbelts')
('ABS errors', 'throttle problems', 'broken handbreak', 'missing seatbelts')
('steering wheel problems', 'ABS errors', 'throttle problems', 'broken handbreak', 'missing seatbelts')

After that, it's a matter of doing a count for every combination we have from the original infos list:

occurences = {}
for combo in infos_combo:
    occurences[combo] = infos.count(list(combo))

print(occurences)

The full code:

import itertools
import sys

data = [
    {
        "make": "dacia",
        "model": "x",
        "version": "A",
        "typ": "sedan",
        "infos": [
            {"id": 1, "name": "steering wheel problems"},
            {"id": 32, "name": "ABS errors"},
        ],
    },
    {
        "make": "nissan",
        "model": "z",
        "version": "B",
        "typ": "coupe",
        "infos": [
            {"id": 3, "name": "throttle problems"},
            {"id": 56, "name": "broken handbreak"},
            {"id": 11, "name": "missing seatbelts"},
        ],
    },
]

infos = [
    [i["name"] for i in d["infos"]] if isinstance(d["infos"], list) else d["infos"]
    for d in data
]

unique_infos = [x for l in infos for x in l]

infos_combo = itertools.chain.from_iterable(
    itertools.combinations(unique_infos, r) for r in range(len(unique_infos) + 1)
)

occurences = {}
for combo in infos_combo:
    occurences[combo] = infos.count(list(combo))

print(occurences)

Post a Comment for "Search For Combinations In JSON Nested Object"