[Python] Use JSON with Python

Introduction

I used to handle json data in Python, but I will introduce it because it has an affinity with the Python standard dict type.

What is json

Here It will be very helpful. Thank you very much. JavaScript Object Notation A notation for expressing data. Similar to JavaScript syntax, but treated independently of JavaScript. Functions that can handle JSON in programming languages other than JavaScript are prepared.

Since this article focuses on using JSON, detailed explanations will be omitted. The format is as follows. It feels like value is linked to key.

{
  "key":value,
  "key":value,
  "key":value
}

The key and value set is the same as the Python dict.

Python json module

There is a json module in Python. It mainly converts dict⇔json.

Use json.dumps to convert dict to json. The type of dict is of course dict, but after conversion it will be str type.

import json

sample_dict = {"Japan":"Tokyo","UK":"London","USA":"Washington, D.C."}

print(type(sample_dict))
print(sample_dict)

#output
# <class 'dict'>
# {'Japan': 'Tokyo', 'UK': 'London', 'USA': 'Washington, D.C.'}

#conversion
sample_json = json.dumps(sample_dict)

print(type(sample_json))
print(sample_json)

#output
# <class 'str'>
# {"Japan": "Tokyo", "UK": "London", "USA": "Washington, D.C."}

Convert from pandas to json

You can also convert from pandas DataFrame or pandas Series to json. Personally, I'm grateful that I use pandas a lot. Use to_json () to convert pandasDataFrame (Series) to json.

import pandas as pd

df_sample = pd.DataFrame([[1,1,1],[2,3,4],[8,7,6]],columns=["x","y","z"],index=["A","B","C"])
json_df = df_sample.to_json()

print(df_sample)
print(json_df)

#output
   x  y  z
A  1  1  1
B  2  3  4
C  8  7  6

{"x":{"A":1,"B":2,"C":8},"y":{"A":1,"B":3,"C":7},"z":{"A":1,"B":4,"C":6}}

load json

Use json.loads to load json data. Use sample_json from earlier.


dict_json = json.loads(sample_json)

print(type(dict_json))
print(dict_json)

#output
# <class 'dict'>
# {'Japan': 'Tokyo', 'UK': 'London', 'USA': 'Washington, D.C.'}

Of course dict_json is a Python dict

print(dict_json['Japan'])

#output
# Tokyo

You can access it in this way.

in conclusion

Since json can be used not only in python but also in other languages, it is convenient for communication between languages. I've only explained the minimum here, but please try it!

What I thought

I tried using it myself, and since I wrote it from the perspective of something like that, I thought that all the articles could not be touched on comprehensively. For the time being, I will continue to write about what I have used, and if I need to review it, I will update the article.

Recommended Posts

[Python] Use JSON with Python
Use mecab with Python3
Use DynamoDB with Python
Use Python 3.8 with Anaconda
Use python with docker
Use Trello API with python
POST json with Python3 script
Use Twitter API with Python
Use TUN / TAP with Python
Python #JSON
Use subsonic API with python3
Format json with Vim (with python)
Read json data with python
Python: How to use async with
Use PointGrey camera with Python (PyCapture2)
Use vl53l0x with Raspberry Pi (python)
[Python] Use Basic/Digest authentication with Flask
Use NAIF SPICE TOOLKIT with Python
JSON encoding and decoding with python
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
How to use FTP with Python
Use Windows 10 speech synthesis with Python
Use OpenCV with Python 3 in Window
Use PostgreSQL with Lambda (Python + psycopg2)
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Use smbus with python3 under pyenv environment
Use DeepL with python (for dissertation translation)
Use Amazon Simple Notification Service with Python
Convert Excel data to JSON with python
[Python] Use string data with scikit-learn SVM
[Introduction to Python] Let's use foreach with Python
Use PIL and Pillow with Cygwin Python
Use cryptography library cryptography with Docker Python image
Reading and writing JSON files with Python
Use Application Insights with Python 3 (including bottles)
Use C ++ functions from python with pybind11
Use selenium phantomjs webdriver with python unittest
Until you can use opencv with python
Use Python and MeCab with Azure Functions
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Use thingsspeak from python
Scraping with Python (preparation)
Use mecab-ipadic-neologd with igo-python