0

I'm trying to merge two json files each of them contain an array of objects, here is an example of how the data looks line in each file:

data in file 1:

[{"tag": "MODL", "entity": "QX50"}, {"tag": "MODL", "entity": "QX60"}, {"tag": "MODL", "entity": "QX70"}]

data in file 2:

[{"tag": "MANF","entity": "لكزس"},{"tag": "MANF","entity": "جيب"},{"tag": "MANF","entity": "هونداي"}]

so I tried to merge them by this code:

import json

f1data = f2data = "" 

with open('merged_file_models_arabic.json') as f1: 
  f1data = f1.read() 
with open('MANF_arabic.json') as f2: 
  f2data = f2.read() 

f1data += "\n"
f1data += f2data
with open ('merged_MANF_MODL.json', 'a') as f3: 
  f3.write(f1data)

but the result in 'merged_MANF_MODL.json' file becomes two arrays, and this is not what I want, I want the two arrays in each file merged into one array in 'merged_MANF_MODL.json' file.

Expected result:

[{"tag": "MODL", "entity": "QX50"}, {"tag": "MODL", "entity": "QX60"}, {"tag": "MODL", "entity": "QX70"}, {"tag": "MANF","entity": "لكزس"},{"tag": "MANF","entity": "جيب"},{"tag": "MANF","entity": "هونداي"}]

2 Answers 2

1

Try this instead:

import json

f1data = f2data = "" 

with open('merged_file_models_arabic.json') as f1: 
  f1data = json.load(f1) 
with open('MANF_arabic.json') as f2: 
  f2data = json.load(f2) 

f1data.extend( f2data )   # merge

with open ('merged_MANF_MODL.json', 'a') as f3: 
  json.dump(f1data, f3)
Sign up to request clarification or add additional context in comments.

1 Comment

From your code f1data = f1.read() is set to string object. and f1data.extend( f2data ) Here 'str' object has no attribute 'extend'
0

You can try something like this

import json
import io

with open("file1.json") as f1:
    data = json.load(f1)

with open("file2.json") as f2:
    data2 = json.load(f2)

data.extend(data2)

>>> data
[{'tag': 'MODL', 'entity': 'QX50'}, {'tag': 'MODL', 'entity': 'QX60'}, 
 {'tag': 'MODL', 'entity': 'QX70'}, {'tag': 'MANF', 'entity': 'لكزس'}, 
 {'tag': 'MANF', 'entity': 'جيب'}, {'tag': 'MANF', 'entity': 'هونداي'}]

Now you can append or write this single list data into the 3rd JSON file

with io.open('merged_MANF_MODL.json', 'w', encoding='utf8') as f3: 
   #json.dump(data, f3, ensure_ascii=False)
   data = json.dumps(data, ensure_ascii=False)
   f3.write(unicode(data))

Reason for ensure_ascii=False. As your list consists of non-ascii characters (Arabic). If ensure_ascii is false, these characters will be output as-is.

7 Comments

I tried your code but this error showed: TypeError: expected a string or other character buffer object
@Nour_em I have updated the code, please try it now.
I tried the updated code and this error showed: UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-6: ordinal not in range(128)
@Nour_em Can you share the full traceback here
Traceback (most recent call last): File "merged_MANF_MODL.py", line 26, in <module> json.dump(data, f3, ensure_ascii=False) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 190, in dump fp.write(chunk) UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-6: ordinal not in range(128)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.