SoFunction
Updated on 2024-11-14

.to_json to json on a line-by-line basis

Recently, we need to convert the csv file into DataFrame and display it in the form of json to the frontend, so we need to use the to_json method of Dataframe.

The to_json method defaults to column names as keys and column contents as values in the form of {col1:[v11,v21,v31...],col2:[v12,v22,v32],...}, but sometimes we need to convert it to json on a row-by-row basis in the form of [row1:{col1:v11,col2:v12,col3:v13...},row2:{col1:v21,col2:v22,col3:v23...}],row2:{col1:v21,col2:v22,col3:v23...}],row2:{col1:v12,col3:v13...},row2:{col1:v21,col2:v22,col3:v23...} :v11,col2:v12,col3:v13...},row2:{col1:v21,col2:v22,col3:v23...}]

By looking up the official website we can see that the to_json method has a parameter orient, and its parameter description is as follows:

orient : string 
Series 
default is ‘index' 
allowed values are: {‘split','records','index'} 
DataFrame 
default is ‘columns' 
allowed values are: {‘split','records','index','columns','values'} 
The format of the JSON string 
split : dict like {index -> [index], columns -> [columns], data -> [values]} 
records : list like [{column -> value}, … , {column -> value}] 
index : dict like {index -> {column -> value}} 
columns : dict like {column -> {index -> value}} 
values : just the values array 
table : dict like {‘schema': {schema}, ‘data': {data}} describing the data, and the data component is like orient='records'. 
Changed in version 0.20.0

The general idea is:

If it is Series to json, the default orientation is 'index', and the optional parameters for orientation are {'split','records','index'}

If it is DataFrame to json, the default orientation is 'columns', and the optional parameters for orientation are {'split', 'records', 'index', 'columns', 'values'}

The format of the json is as follows

split, style {index -> [index], columns -> [columns], data -> [values]}

records, style [{column -> value}, ... , {column -> value}]

index with style {index -> {column -> value}}

columns, style {index -> {column -> value}}

values, array style

table, styled as {'schema': {schema}, 'data': {data}}, similar to records

Take a look at the demo given on the official website

df = ([['a', 'b'], ['c', 'd']],
  index=['row 1', 'row 2'],
  columns=['col 1', 'col 2'])
###########
split
###########
df.to_json(orient='split')
>'{"columns":["col 1","col 2"],
 "index":["row 1","row 2"],
 "data":[["a","b"],["c","d"]]}'
###########
index
###########
df.to_json(orient='index')
>'{"row 1":{"col 1":"a","col 2":"b"},"row 2":{"col 1":"c","col 2":"d"}}'
###########
records
###########
df.to_json(orient='index')
>'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'
###########
table
###########
df.to_json(orient='table')
>'{"schema": {"fields": [{"name": "index", "type": "string"},
  {"name": "col 1", "type": "string"},
  {"name": "col 2", "type": "string"}],
 "primaryKey": "index",
 "pandas_version": "0.20.0"},
 "data": [{"index": "row 1", "col 1": "a", "col 2": "b"},
 {"index": "row 2", "col 1": "c", "col 2": "d"}]}'

Mainly refer to the official website API:/pandas-docs/stable/generated/.to_json.html

Above this .to_json by line to json is all I have to share with you, I hope to give you a reference, and I hope you support me more.