I am trying to modify my JSON file in which I need to replace the file_name with the base path as structured below:
data = {"images": [
{
"id": 1,
"file_name": "Folder1/Folder2/Folder3/Folder4/1110.jpg",
"height": 3024,
"width": 4032
},
{
"id": 2,
"file_name": "Folder1/Folder2/Folder3/Folder4/1111.jpg",
"height": 3024,
"width": 4032
},
{
"id": 3,
"file_name": "Folder1/Folder2/Folder3/Folder4/1112.jpg",
"height": 3024,
"width": 4032
}
]}
Here is my code which I tried
data = json.load(open(".\example.json"))
First I have check the full file_name with below code
for img in data['images']:
print(img["file_name"])
Below is the Output for this cell
Folder1/Folder2/Folder3/Folder4/1110.jpg
Folder1/Folder2/Folder3/Folder4/1110.jpg
Folder1/Folder2/Folder3/Folder4/1110.jpg
Then I modified it to get the base path as below
for feature in data['images']:
feature['file_name'] = os.path.basename(feature["file_name"])
print(feature['file_name'])
This is the output:
1110.jpg
1111.jpg
1112.jpg
When I am trying to dump this into a JSON file but I am only getting one file_name.
data = {1110.jpg}
I would like to get the output as below. Please help me with your inputs
data = {"images": [
{
"id": 1,
"file_name": "1110.jpg",
"height": 3024,
"width": 4032
},
{
"id": 2,
"file_name": "1111.jpg",
"height": 3024,
"width": 4032
},
{
"id": 3,
"file_name": "1112.jpg",
"height": 3024,
"width": 4032
}
]}