Home > Back-end >  How to match multiple json with multiple value in python
How to match multiple json with multiple value in python

Time:01-25

I have three json and wish to combine three of them to it respective value.

jsonlistA = { "header1": "Hello World1", "header2": "Hello World2", "header3": "Hello World3"}

jsonlistB = { "condition1": "halo1", "condition2": "halo2", "condition3": "halo3"}

jsonlistC = { "parameter1": "hi1", "parameter2": "hi2", "parameter3": "hi3"}

What i want to achieve

I want it to pair json list a,b,c with three value from three different list. For example, header1,condition1, parameter1 should be one list eg.

jsonCombineA = { "header1": "Hello World1", "condition1": "halo1","parameter1": "hi1"}

So when I print the json to string it should be HelloWorld1 halo1 hi1.

CodePudding user response:

I think you are searching for zip method

a = b = c = range(20)
list(zip(a, b, c))

in your case just to take values from dict eg.

a = jsonlistA.values()
...

CodePudding user response:

Here is a solution with zip

By using zip on the keys of the list, it return this

('header1', 'condition1', 'parameter1')
('header2', 'condition2', 'parameter2')
('header3', 'condition3', 'parameter3')

Now we can have the desired output with

jsonlistA = { "header1": "Hello World1", "header2": "Hello World2", "header3": "Hello World3"}
jsonlistB = { "condition1": "halo1", "condition2": "halo2", "condition3": "halo3"}
jsonlistC = { "parameter1": "hi1", "parameter2": "hi2", "parameter3": "hi3"}

for keys in zip(jsonlistA.keys(), jsonlistB.keys(), jsonlistC.keys()):
    print(f"{jsonlistA[keys[0]]} {jsonlistB[keys[1]]} {jsonlistC[keys[2]]}")

CodePudding user response:

You can zip dict.items objects of each dict and use a dict constructor in a list comprehension:

jsonCombineA, jsonCombineB, jsonCombineC = [dict(tpl) for tpl in zip(jsonlistA.items(), jsonlistB.items(), jsonlistC.items())]

Output:

print(jsonCombineA)  # {'header1': 'Hello World1', 'condition1': 'halo1', 'parameter1': 'hi1'}
print(jsonCombineB)  # {'header2': 'Hello World2', 'condition2': 'halo2', 'parameter2': 'hi2'}
print(jsonCombineC)  # {'header3': 'Hello World3', 'condition3': 'halo3', 'parameter3': 'hi3'}

CodePudding user response:

It will work no matter if you change the order of the data inside the dictionary:

import re

jsonlistA = {"header1": "Hello World1",
             "header2": "Hello World2",
             "header3": "Hello World3"}
jsonlistB = {"condition1": "halo1",
             "condition2": "halo2",
             "condition3": "halo3"}
jsonlistC = {"parameter1": "hi1",
             "parameter2": "hi2",
             "parameter3": "hi3"}

data = []
for i, j in jsonlistA.items():
    number = re.search(r'[\d] $', i)
    if number:
        number = number.group()
        key1, key2 = f"condition{number}", f"parameter{number}"
        data.append({i: j, key1: jsonlistB[key1], key2: jsonlistC[key2]})

[{'header1': 'Hello World1', 'condition1': 'halo1', 'parameter1': 'hi1'},
 {'header2': 'Hello World2', 'condition2': 'halo2', 'parameter2': 'hi2'},
 {'header3': 'Hello World3', 'condition3': 'halo3', 'parameter3': 'hi3'}]

CodePudding user response:

I think that's what you're looking for:

jsonlistA = {"header1": "Hello World1", "header2": "Hello World2", "header3": "Hello World3"}
jsonlistB = {"condition1": "halo1", "condition2": "halo2", "condition3": "halo3"}
jsonlistC = {"parameter1": "hi1", "parameter2": "hi2", "parameter3": "hi3"}

jsonCombine = []
for data in zip(jsonlistA.items(), jsonlistB.items(), jsonlistC.items()):
    jsonCombine.append({i[0]: i[1] for i in data})
jsonCombineA = jsonCombine[0]
jsonCombineB = jsonCombine[1]
jsonCombineC = jsonCombine[2]

By the way, these are not "JSON lists", they are called dict.

  •  Tags:  
  • Related