I have 100 JSON files need to merge to 1 JSON file. basically I want to put [] around 100 files and append them all into 1 file.
Each file has the same structure as follow:
[
{
"id": "1",
"title": "Student",
"children": [
{
"ID": "111",
"Name": "John",
"Pattern": "DA0"
},
{
"ID": "222",
"Name": "Tom",
"Pattern": "DA0"
}
]
}
]
I have the following code to achieve this but there is an error for JSON encode? Please have a look:
import glob
import json
read_files = glob.glob("*.json")
output_list = []
with open(read_files, 'w', encoding='utf-8') as jsonf:
for f in read_files:
with open(f, "rb") as infile:
output_list.append(json.load(infile))
all_items = []
for json_file in output_list:
all_items += json_file['items']
textfile_merged = open('merged.json', 'w')
json.dump({ "items": all_items }, textfile_merged)
textfile_merged.close()
The error message:
Traceback (most recent call last):
File "combine.py", line 10, in <module>
with open(read_files, 'w', encoding='utf-8') as jsonf:
TypeError: expected str, bytes or os.PathLike object, not list
The glob.glob("*.json) returns a list of path names per the python documentation. So your code with open(read_files, 'w', encoding='utf-8') as jsonf: will not work properly.
Try something like:
import glob
import json
read_files = glob.glob("*.json")
output_list = []
for f in read_files:
with open(f, "rb") as infile:
output_list.append(json.load(infile))
# rest of your code
Related
I have a JSON file with 10000 data entries like below in a file.
{
"1":{
"name":"0",
"description":"",
"image":""
},
"2":{
"name":"1",
"description":"",
"image":""
},
...
}
I need to write each entry in this object into its own file.
For example, the output of each file looks like this:
1.json
{
"name": "",
"description": "",
"image": ""
}
I have the following code, but I'm not sure how to proceed from here. Can anyone help with this?
import json
with open('sample.json', 'r') as openfile:
# Reading from json file
json_object = json.load(openfile)
You can use a for loop to iterate over all the fields in the outer object, and then create a new file for each inner object:
import json
with open('sample.json', 'r') as input_file:
json_object = json.load(input_file)
for key, value in json_object.items():
with open(f'{key}.json', 'w') as output_file:
json.dump(value, output_file)
I have written a python code to convert csv file into json file. But the output is not the same as I desired. please look and suggest modifications.
Below is the expected json file.
[
{
"id": "1",
"MobileNo": "923002546363"
},
{
"id": "2",
"MobileNo": "923343676143"
}
]
below is the code that I have written in python.
import csv, json
def csv_to_json(csvFilePath, jsonFilePath):
jsonArray = []
#read csv file
with open(csvFilePath, encoding='utf-8') as csvf:
#load csv file data using csv library's dictionary reader
csvReader = csv.DictReader(csvf)
#convert each csv row into python dict
for row in csvReader:
#add this python dict to json array
jsonArray.append(row)
#convert python jsonArray to JSON String and write to file
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonString = json.dumps(jsonArray, indent=4)
jsonf.write(jsonString)
csvFilePath = r'my_csv_data.csv'
jsonFilePath = r'data.json'
csv_to_json(csvFilePath, jsonFilePath)
As your post doesn't provide current output, I just created a csv file to run your code:
id,MobileNo
1,923002546363
2,923343676143
3,214134367614
And works just fine:
[
{
"id": "1",
"MobileNo": "923002546363"
},
{
"id": "2",
"MobileNo": "923343676143"
},
{
"id": "3",
"MobileNo": "214134367614"
}
]
Check if your csv file isn't corrupted. And if possible, edit your post with current output and your csv file.
I have two files: json and text files.
I would like to replace one of the dictionary value with the value which in the text file.
Let us say, in the text file file.text, I have the following lists. [11, 15, 10].
In the json file, I have the following dictionary.
"aa": {
"bb": [
"25",
"40",
"05"
],
"cc": [
"20"
]
}
I would like to overwrite the cc value with the text file above.
file.json
"aa": {
"bb": [
"25",
"40",
"05"
],
"cc": [
"11", "15", "10"
]
}
I have tried something in Python.
def replace(text_file, json_file):
tex_file_path = 'C:/Documents/file.txt'
with open(os.path.join(tex_file_path, text_file), 'r') as f:
read_text= f.read()
json_file_path = 'C:/Documents/file.json'
with open(os.path.join(json_file_path, json_file), 'r') as f:
read_json = json.load(f)
text_to_be_replaced = read_json.get('aa')
for value in text_to_be_replaced.items():
for element in value:
# statment
I was wondering if someone can really help with this.
Although you've named it .text, the contents of the file appear to be JSON, so you can use json.load() as well. Then convert the integers in the list to strings and insert it into the desired place in the JSON file.
There's no need to loop over the dictionary items. Just address the specific element you want to replace.
def replace(text_file, json_file):
tex_file_path = 'C:/Documents'
with open(os.path.join(tex_file_path, text_file), 'r') as f:
read_text= json.load(f)
read_text = list(map(str, read_text))
json_file_path = 'C:/Documents'
with open(os.path.join(json_file_path, json_file), 'r') as f:
read_json = json.load(f)
read_json["aa"]["cc"] = read_text
with open(os.path.join(json_file_path, json_file), 'w') as f:
json.dump(read_json, f)
Also, your XXX_path variables should just be directories, the filename comes from the function parameter.
Here's a simple example using a StringIO to demonstrate reading / writing from a file-like object:
import json
from io import StringIO
json_file_obj = StringIO("""
{"aa": {
"bb": [
"25",
"40",
"05"
],
"cc": [
"20"
]
}
}
""")
text_file_obj = StringIO("[11, 15, 10]")
def replace(src_file_obj: StringIO, repl_file_obj: StringIO):
# Load file contents into a Python object
data = json.load(src_file_obj)
# Read in txt file contents
new_cc_value = json.load(repl_file_obj)
# But now result will be a list of int, here we want a list of string
new_cc_value = list(map(str, new_cc_value))
# Replace desired value
data['aa']['cc'] = new_cc_value
# Now we write to our file-like object, `src_file_obj`
# This is to demonstrate replacing the original file contents
src_file_obj = StringIO()
json.dump(data, src_file_obj)
# Seek to the start of the file
src_file_obj.seek(0)
return src_file_obj
json_file_obj = replace(json_file_obj, text_file_obj)
print(json_file_obj.read())
Output:
{"aa": {"bb": ["25", "40", "05"], "cc": ["11", "15", "10"]}}
Hint - If you want to write the output to an actual file, you can replace these lines below:
src_file_obj = StringIO()
json.dump(data, src_file_obj)
src_file_obj.seek(0)
With these lines:
with open("file_name.txt", 'w') as out_file:
json.dump(data, out_file)
code :
userid1='u123'
userid2='u124'
ids= (userid1,userid2)
fake = Faker('en_US')
for ind in ids:
for idx in range(1):
sms = {
"id": ind ,
"name": fake.name(),
"email": fake.email(),
"gender": "MALE",
}
f_name = '{}.json'.format(ind)
with open(f_name, 'w') as fp:
#Save the dictionary
json.dump(sms, fp, indent=4)
print(sms)
file1 = filename.json ( how to get the *ind* value here i.e., userid)
fd1=open("filename.json")
json_content1 = fd1.read()
fd1.close()
how to open file that has been saved f_name = '{}.json'.format(ind) here . without mentioning the file name manually. file names are saved using ind. so how to use ind here and open the file
this code can help you to get data from json file: you can get any filed from the json data by typing data["name-of-filed"]:
import json
userid1='json_file1'
ids= [userid1]
for ind in ids:
f_name = '{}.json'.format(ind)
with open(f_name, 'r') as outfile:
data = json.loads(outfile.read())
print(data["name"])
print(data)
here is an exemple :
file.json :
{
"name": "Ghassen",
"apiVersion": "v1"
}
output :
Ghassen
{'name': 'Ghassen', 'apiVersion': 'v1'}
json file
[{"Attachment": [
{
"Page:": [
{
"Path": "a\\b\\c.pdf", #field to be extracted
"PageID": 1
}
]
}
],
"ID": 13221}]
I tried the following but getting the TypeError: list indices must be integers, not str
with open(file) as f:
d = json.load(f)
print(d[0]['Attachment']['Page']['Path'])
d[0]['Attachment'] is a list, so is d[0]['Attachment'][0]['Page:'].
with open(file) as f:
d = json.load(f)
print(d[0]['Attachment'][0]['Page:'][0]['Path'])
will do the job.