0

I have big json file, about 500mb, like next:

[
    {"title": "title1 here", "text": ["text1 here"]},
    {"title": "title2 here", "text": ["text2 here"]},
    {...
]

I'd like to make changes in titles and text. How can I do it? I decided to read titles and texts as strings and after save it to json format again. It's ok with reading and making changes:

f = open('test.json')
data = json.load(f)

for i in range(len(data)):
    #print(data[i]['title'])
    changedTitle = data[i]['title'].split()
    changedText = data[i]['text']
    shuffle(changedTitle)
    shuffle(changedText)
    changedTitle = " ".join(changedTitle)
    changedText = " ".join(changedText) 
    print(changedTitle)
    print(changedText)

So I got changedTitle and changedText in loop and have no idea how to construct json file from it. Please advice. Thanks.

I decided to read titles and texts as strings and after save it to json format again.

0

1 Answer 1

0

Solution:

Reassign the changed title and text back into the data at the same index you read it from. For example:

for i in range(len(data)):
    #print(data[i]['title'])

    # Read from data at specific index
    changedTitle = data[i]['title'].split()
    changedText = data[i]['text']

    # Change data values
    changedTitle = shuffle(changedTitle)
    changedText = shuffle(changedText)

    # Reassign changed values back to original indices
    data[i]['title'] = " ".join(changedTitle)
    data[i]['text'] = " ".join(changedText) 

and then to save back to a json file

with open('your_file_name.json', 'w') as f:
    json.dump(data, f)
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, that's what you do, but you forgot to save it back to the file.
Anything else you need?
No, that's fine now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.