How to Merge Json Objects Containing Arrays Using Python

How to Combine JSON Objects From Different Arrays With Python

Use pandas:

import pandas as pd
df = pd.DataFrame([x for k,v in data.items() for x in v])
print(df)

Output:

    id       keyid    name symbol
0 000 4273666087 Raptor RPT
1 111 1818114564 Duck DUK
2 222 8032408148 Hawk HWK
3 333 0362766431 Goose GOO

You can do it in Regular Python but it will be a big hassle, so I support using pandas which can do it in one line.

As you want nested loops:

import pandas as pd
l = []
for k,v in data.items():
for x in v:
l.append(x)
df = pd.DataFrame(l)
print(df)

How to merge two json arrays in Python

Let say you receive something like resp1 = '{"data": [...]}' and store it in a dictionary d. Assuming you do that using json package as follows:

d = json.loads(resp1)

For the next batch of data, you should store it in a temporal dictionary td, extract the field "data_next" and append it into the original dictionary:

td = json.loads(respN)
d["data"].append(td["data_next"])

Merge two json object in python

In json module, dumps convert python object to a string, and loads convert a string into python object. So in your original codes, you just try to concat two json-string. Try to code like this:

import json

from collections import defaultdict


def merge_dict(d1, d2):
dd = defaultdict(list)

for d in (d1, d2):
for key, value in d.items():
if isinstance(value, list):
dd[key].extend(value)
else:
dd[key].append(value)
return dict(dd)


if __name__ == '__main__':
json_str1 = json.dumps({"a": [1, 2]})
json_str2 = json.dumps({"a": [3, 4]})

dct1 = json.loads(json_str1)
dct2 = json.loads(json_str2)
combined_dct = merge_dict(dct1, dct2)

json_str3 = json.dumps(combined_dct)

# {"a": [1, 2, 3, 4]}
print(json_str3)

How to merge 2 JSON array files?

You could use the loads function of the json module that Python provides out of the box in order to read and process the json structures inside these .txt files and then append them to a final list in order to use it any way you want:

import json

result = []
textFiles = ['db_1.txt', 'db_2.txt']
for textFile in textFiles:
with open(textFile, 'r') as file_1:
data = json.loads(file_1.read())
result.extend(data)

print(result)

This will print:

[{'title': 'Title1', 'price': 21.37}, {'title': 'Title2', 'price': 32.1}, {'title': 'Title3', 'price': 221.67}, {'title': 'Title4', 'price': 121.37}, {'title': 'Title5', 'price': 232.1}]

Merge JSON data with Python

You're looking for extend, not append.

thejson1["data"].extend(thejson2["data"])

append takes the single argument and insert it to the end. While extend extends the list by adding all the individual values in the argument list to the end.

# example:
a=[1, 2, 3]

b = a[:].append([4, 5])
# b = [1, 2, 3, [4, 5]]

c = a[:].extend([4, 5])
# c = [1, 2, 3, 4, 5]


Related Topics



Leave a reply



Submit