Skip to content
Advertisement

Read JSON Lines file as String in Python

I have a JSON Lines file that I would like to read as a string in Python. The file contains multiple JSON objects in this format:

JavaScript

I tried the following code in Python but it returned an error. I was able to load the file as a list of dictionaries using lines = [] but apprently it doesn’t work for a string. How can I read the whole file as a string?

JavaScript

Advertisement

Answer

The best way to read a JSON Lines document as a string would be to use the read() function as follow:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement