JSON encoding and decoding with python

Handle associative arrays in JSON

Note that I have more opportunities to use it recently, such as encoding an associative array into JSON and sending it via TCP or UDP, or reading a configuration file written in JSON.

Encode

If the data of the associative array is frequently rewritten and you want to send the contents of each array by communication, JSON is convenient.

For example, suppose you want to get some 3D coordinates and store them in an associative array.

>>> import json
>>> list = {'x': 100, 'y': 200, 'z': 300}
>>> json.dumps(list)
'{"x": 100, "y": 200, "z": 300}'

Since this example is simple data, there is no need to bother to make it an associative array, but it is very easy to retrieve data with a key when there are various types of data or when you do not remember the order one by one.

Even when sending JSON-encoded data somewhere via some kind of communication, if you tell the key, the recipient can easily retrieve the data. So, JSON decoding.

Decode

Encode the list you used earlier, assign it to a variable, and try decoding it.

>>> import json
>>> list = {'x': 100, 'y': 200, 'z': 300}
>>> enc = json.dumps(list)
>>> print enc
'{"x": 100, "y": 200, "z": 300}'
>>> dec = json.loads(enc)
>>> print dec
{u'y': 200, u'x': 100, u'z': 300}
>>> print dec['x']
100

Like this. After decoding, the order of the elements inside is out of order. Anyway, when referring to it, it is called by the key, so the order does not matter.

Recommended Posts

JSON encoding and decoding with python
Reading and writing JSON files with Python
Read JSON with Python and output as CSV
[Python3] Read and write with datetime isoformat with json
Programming with Python and Tkinter
Encryption and decryption with Python
POST json with Python3 script
python with pyenv and venv
Format json with Vim (with python)
Works with Python and R
Read json data with python
Parse and visualize JSON (Web application ⑤ with Python + Flask)
Read json file with Python, format it, and output json
Communicate with FX-5204PS with Python and PyUSB
Shining life with Python and OpenCV
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Easily write JSON and Python dataclass conversions with quicktype and dacite
Scraping with Python, Selenium and Chromedriver
Scraping with Python and Beautiful Soup
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Reading and writing NetCDF with Python
JSON encoding / decoding of custom objects
Normalization method (encoding) and reversion (decoding)
I played with PyQt5 and Python3
Python #JSON
Python encoding
Python logging and dump to json
Reading and writing CSV with Python
Multiple integrals with Python and Sympy
Sample of HTTP GET and JSON parsing with python of pepper
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
Sugoroku game and addition game with python
FM modulation and demodulation with Python
[Python3] Save the mean and covariance matrix in json with pandas
Communicate between Elixir and Python with gRPC
Data pipeline construction with Python and Luigi
Monitor Mojo outages with Python and Skype
FM modulation and demodulation with Python Part 3
[Automation] Manipulate mouse and keyboard with Python
Passwordless authentication with RDS and IAM (Python)
Python installation and package management with pip
Using Python and MeCab with Azure Databricks
POST variously with Python and receive with Flask
Capturing images with Pupil, python and OpenCV
Convert Excel data to JSON with python
Fractal to make and play with Python
Use PIL and Pillow with Cygwin Python
Create and decrypt Caesar cipher with python
CentOS 6.4 with Python 2.7.3 with Apache with mod_wsgi and Django
Dealing with "years and months" in Python
I installed and used Numba with Python3.5
Read and write JSON files in Python
Tweet analysis with Python, Mecab and CaboCha
Linking python and JavaScript with jupyter notebook
Traffic monitoring with Kibana, ElasticSearch and Python
FM modulation and demodulation with Python Part 2