A story when a Python user passes a JSON file

Fringe81 Advent Calendar 2019, 12th day article.

This time, I will write a small story that people who have only touched Python (like me) will need in practice. As the title suggests, it is a story when passing data as a JSON file to a front engineer after squeezing the data with Python.

Data preparation

For example, I want to create a screen for introducing myself to new employees! At that time, suppose you are asked to have data in the following form (each data is like a CSV file, so it is assumed that they are individual).

interface Member {
    id: string;
    name: string;
    profile: {
        age: number;
        height: number;
        weight: number:
    };
    likes: {
        name: string;
        reason: string;
    }[];
}[];

I think that newcomers who can only use Python can organize this with pandas and output it. First, prepare the data.

import pandas as pd

#The following data assumes that there is a separate file and it matches the state read by pandas
df1 = pd.DataFrame(
    {
        "id": ["01", "02", "03"],
        "name": ["Mr. A", "Mr. B", "Mr. C"]
    }
)
df2 = pd.DataFrame(
    {
        "id": ["01", "02", "03"],
        "age": [22, 24, 27],
        "height": [177, 171, 167],
        "weight": [64, 57, 62]
    }
)
df3 = pd.DataFrame(
    {
        "id": ["01", "02", "03"],
        "name": ["Sushi / golf", "Baseball / camera", "Ducks, villagers, warriors"],
        "reason": ["Because it's delicious and it's refreshing", "Because it's exciting and I want to keep it in my memory", "Because it's cute, it's kind, it's cool"]
    }
)

The contents of the data look like this.

df1
id name
0 01 Mr. A
1 02 Mr. B
2 03 Mr. C
df2
id age height weight
0 01 22 177 64
1 02 24 171 57
2 03 27 167 62
df3
id name reason
0 01 Sushi / Golf Because it's delicious and it's refreshing
1 02 Baseball / Camera Because it's exciting and I want to keep it in my memory
2 03 Duck / Villager / Warrior Because it's cute, because it's kind, because it's cool

Data shaping

Next, we will format the data. For the time being, shape it and combine it into one data frame.

#df2 shaping
# "profile"Enter the dictionary summary in the column
df2["profile"] = df2.apply(lambda row: {"age": row["age"], "height": row["height"], "weight": row["weight"]}, axis=1)
df2 = df2[["id", "profile"]]

#df3 shaping
#Divide the text separated by "・" and include it in the list type
# "likes"Enter the dictionary summary in the column
df3["name"] = df3["name"].str.split("・")
df3["reason"] = df3["reason"].str.split("・")
df3["likes"] = df3.apply(lambda row: [{"name": name, "reason": reason} for name, reason in zip(row["name"], row["reason"])], axis=1)
df3 = df3[["id", "likes"]]

#Data combination
df = df1.merge(df2, on="id").merge(df3, on="id")

The contents are like this.

df
Mr
id name profile likes
0 01 Mr. A {'age': 22, 'height': 177, 'weight': 64} [{'name':'Sushi',' reason':'Because it's delicious'}, {'name':'...
1 02 Mr. B {'age': 24, 'height': 171, 'weight': 57} [{'name':'baseball','reason':'because it gets excited'}, {'name': ...
2 03 Mr. C {'age': 27, 'height': 167, 'weight': 62}

Data output

Finally, write the data to a JSON file.

import json

#Create a dict for output
#orient is set so that the nested structure in the list can be saved as it is (arguments here are important)
output = df.to_dict(orient="records")

#Open the save destination file and enter the data.
with open("introduction.json", "w") as f:
    #indent is set so that the contents of the file are easy to see
    # ensure_ascii is set to prevent garbled Japanese characters
    json.dump(output, f, indent=4, ensure_ascii=False)

By the way, the contents should look like this.

output
[{'id': '01',

'name':'Mr. A', 'profile': {'age': 22, 'height': 177, 'weight': 64}, 'likes': [{'name':'sushi','reason':'because it's delicious'}, {'name':'Golf',' reason':'Because I'm crazy'}]}, {'id': '02', 'name':'Mr. B', 'profile': {'age': 24, 'height': 171, 'weight': 57}, 'likes': [{'name':'baseball','reason':'because it gets excited'}, {'name':'camera','reason':'because I want to remember'}]}, {'id': '03', 'name':'C-san', 'profile': {'age': 27, 'height': 167, 'weight': 62}, 'likes': [{'name':'duck','reason':'because it's cute'}, {'name':'villager',' reason':'because it's kind'}, {'name':'Warrior',' reason':'Because it's cool'}]}]

that's all. Apart from this, it may be easier to put together only with the dict type, but this time I wrote a method to convert what was read in the pandas data frame as it is.

Finally

Thank you for reading until the end. Next time is the designer mirinrin! Stay tuned for Fringe81 Advent Calendar 2019.

Recommended Posts

A story when a Python user passes a JSON file
CRLF becomes LF when reading a Python file
How to create a JSON file in Python
Parse a JSON string written to a file in Python
Python script to create a JSON file from a CSV file
I made a script in Python to convert a text file for JSON (for vscode user snippet)
Create a binary file in Python
Precautions when creating a Python generator
When writing a program in Python
[Python, Selenium, PhantomJS] A story when scraping a website with lazy load
Creating a simple PowerPoint file with Python
A story I was addicted to when inserting from Python to a PostgreSQL table
Create a JSON object mapper in Python
Python #JSON
Create a deb file from a python package
[GPS] Create a kml file in Python
A story about Python pop and append
The story of blackjack A processing (python)
I made a configuration file with Python
A story made by a person who has no knowledge of Python or Json
Output timing is incorrect when standard (error) output is converted to a file in Python
A story that I was addicted to when I made SFTP communication with python
Error when installing a module with Python pip
A addictive story when using tensorflow on Android
How to read a CSV file with Python 2/3
Create a GIF file using Pillow in Python
Read a file containing garbled lines in Python
[Python3] A story stuck with time zone conversion
Python variadic memorandum when inheriting a defined class
How to generate a Python object from JSON
A story stuck with handling Python binary data
Python Note: When assigning a value to a string
I tried reading a CSV file using Python
A memo when creating a python environment with miniconda
Export Python3 version OpenCV KeyPoint to a file
Create a MIDI file in Python using pretty_midi
A story about modifying Python and adding functions
Read line by line from a file with Python
I want to write to a file with Python
Open a file dialog with a python GUI (tkinter.filedialog)
Script python file
A story that was convenient when I tried using the python ip address module
A story that got stuck when trying to upgrade the Python version on GCE
Python file processing
When writing to a csv file with python, a story that I made a mistake and did not meet the delivery date
[Grasshopper] When creating a data tree on Python script
Make a copy of a Google Drive file from Python
I tried running python etc. from a bat file
A memorandum when writing experimental code ~ Logging in python
What to do when gdal_merge creates a huge file
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
Problems when creating a csv-json conversion tool with python
A story about trying a (Golang +) Python monorepo with Bazel
How to convert JSON file to CSV file with Python Pandas
Things to note when initializing a list in Python
What's in that variable (when running a Python script)
SoC FPGA: A small story when using on Linux
Troublesome story when using Python3 with VScode on ubuntu
Read json file with Python, format it, and output json
Use communicate () when receiving output in a Python subprocess