Parse a JSON string written to a file in Python

I checked how to parse the JSON string written to the file with Python, so make a note.

Method (json.load ())

For such a file

data.json


{
    "data":{
        "a": "Hello",
        "b": 123
    },
    "c": true
}

JSON parsing can be done by using ** json.load () ** for the data acquired by ʻopen ()`.

import json

raw = open('data.json', 'r')
type(raw)
# <class '_io.TextIOWrapper'>

parsed = json.load(raw)
type(parsed)
# <class 'dict'>

parsed
# {'data': {'a': 'Hello', 'b': 123}, 'c': True}

print(json.dumps(parsed, ensure_ascii=False, indent=4))
#{
#    "data":{
#        "a": "Hello",
#        "b": 123
#    },
#    "c": true
#}

Bad way (json.loads)

-- json.loads cannot parse TextIOWrapper type data.

json.loads(raw)
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#   File "/usr/lib64/python3.6/json/__init__.py", line 348, in loads
#     'not {!r}'.format(s.__class__.__name__))
# TypeError: the JSON object must be str, bytes or bytearray, not 'TextIOWrapper'

that's all

Recommended Posts

Parse a JSON string written to a file in Python
How to create a JSON file in Python
How to embed a variable in a python string
Parse JSON file to object
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
[Python] How to expand variables in a character string
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
Python script to create a JSON file from a CSV file
[Introduction to Python] How to parse JSON
Create a binary file in Python
6 ways to string objects in Python
Create a random string in Python
Change the standard output destination to a file in Python
How to import a file anywhere you like in Python
I made a script in Python to convert a text file for JSON (for vscode user snippet)
How to make a string into an array or an array into a string in Python
[Introduction to Python] How to output a character string in a Print statement
Convert psd file to png in Python
Create a JSON object mapper in Python
How to get a string from a command line argument in python
[Python] How to invert a character string
Timezone specification when converting a string to datetime type in python
How to get a stacktrace in python
Python vba to create a date string for creating a file name
[GPS] Create a kml file in Python
Generate a class from a string in Python
Get the formula in an excel file as a string in Python
What to do if there is a decimal in python json .dumps
Create a shortcut to run a Python file in VScode on your terminal
Note: [Python3] Convert datetime to a string in any format you like
I tried to develop a Formatter that outputs Python logs in JSON
I want to color a part of an Excel string in Python
How to write a string when there are multiple lines in python
I made a program to check the size of a file in Python
How to read a CSV file with Python 2/3
Create a GIF file using Pillow in Python
3 ways to parse time strings in python [Note]
Try to calculate a statistical problem in Python
How to clear tuples in a list (Python)
To execute a Python enumerate function in JavaScript
Introduction to effectiveness verification Chapter 3 written in Python
Read a file containing garbled lines in Python
I want to create a window in Python
From file to graph drawing in Python. Elementary elementary
How to generate a Python object from JSON
A clever way to time processing in Python
Steps to develop a web application in Python
Python Note: When assigning a value to a string
To add a module to python put in Julialang
How to notify a Discord channel in Python
Export Python3 version OpenCV KeyPoint to a file
Create a datetime object from a string in Python (Python 3.3)
[Python] How to draw a histogram in Matplotlib
Create a MIDI file in Python using pretty_midi
Put out a shortened URL string in Python
I want to write to a file with Python
How to read a file in a different directory
Insert an object inside a string in Python
Introduction to Effectiveness Verification Chapter 2 Written in Python