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
Beautifulsoup Findall() Given Multiple Classes
Pandas To_Csv: Suppress Scientific Notation in CSV File When Writing Pandas to Csv
How to Print a String Multiple Times
How to Copy a File to a Remote Server in Python Using Scp or Ssh
Pandas - Drop Last Column of Dataframe
Conversion of String to Upper Case Without Inbuilt Methods
What Is the Correct Way to Make My Pyqt Application Quit When Killed from the Console (Ctrl-C)
Pandas: Update Column Values from Another Column If Criteria
How to Change the Foreground or Background Colour of a Tkinter Button on MAC Os X
Cv2 Image Error: Error: (-215:Assertion Failed) !Ssize.Empty() in Function 'Cv::Resize'
Python Anaconda - How to Safely Uninstall
Count Frequency of Words in a List and Sort by Frequency
How to Format an Integer to a Two Digit Hex
Finding Non-Numeric Rows in Dataframe in Pandas
Jsondecodeerror: Expecting Value: Line 1 Column 1 (Char 0)
Spliting a Row to Multiple Row Pyspark
Getting the Id of the Last Record Inserted for Postgresql Serial Key With Python