A
A
Andrew2022-03-25 14:32:22
Python
Andrew, 2022-03-25 14:32:22

How to fix error when saving file to json?

There is a list of dictionaries [{}, {}, {}, {}...] (in the .json file)
It is necessary to find dictionaries with the same keys and remove duplicates. I do it like this:

with open('file.json') as f:
    data = json.load(f)
clean_data = [{x['title']: x for x in data}.values()]

When trying to save clean_data back to a .json file:
with open("clean_file.json", "w") as file:
    json.dump(file, clean_data)

I am getting an error:
TypeError: Object of type TextIOWrapper is not JSON serializable

How to identify/correct the cause?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2022-03-25
@Rsa97

Read the documentation for the json.dump function and check the order of the parameters.

L
LXSTVAYNE, 2022-03-25
@lxstvayne

The order in which parameters are passed is messed up.

with open("clean_file.json", "w") as file:
    json.dump(clean_data, file)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question