0

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
    }
]}
2
  • 2
    create a list and append all of the outputs there and then iterate throught the list and add items from it to the json file or do it directly. also could You provide a bit more code on how You dump those json files etc Commented Mar 31, 2021 at 12:30
  • Thank you for the quick response @Matiiss, Yes I missed the dump JSON file. Anyways the below answer has solved my query. Commented Mar 31, 2021 at 12:46

2 Answers 2

1

The code works as expected if you have resolved the variable name difference.

Further, I would also suggest to open the file using context manager with so that it gets closed automagically when the with block is exited.

import json, os
with open(".\example.json", 'r') as f:
    data = json.load(f)

for feature in data['images']:
    feature['file_name'] = os.path.basename(feature["file_name"])
    
print(data)

Output:

Data in json file:

{'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}]}

Formatted 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}]}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank You @Krishna, it worked exactly as per my expectations. As you mentioned this was the variable name difference issue. I corrected it.
Glad that the problem was resolved quickly!
1

Issue is in this line for feature in temp['images']:, please change it to for feature in data['images']

for feature in data['images']:
    feature['file_name'] = os.path.basename(feature["file_name"])
print(data)
[{'file_name': '1110.jpg', 'height': 3024, 'id': 1, 'width': 4032},
 {'file_name': '1111.jpg', 'height': 3024, 'id': 2, 'width': 4032},
 {'file_name': '1112.jpg', 'height': 3024, 'id': 3, 'width': 4032}]

1 Comment

Thank You @Sancith Jain.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.