4.3. To JSON

  • File paths works also with DATAs

4.3.1. SetUp

>>> import pandas as pd
>>>
>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)
>>>
>>>
>>> data = pd.DataFrame([
...     {'firstname': 'Alice', 'lastname': 'Apricot', 'age': 30, 'lastlogin': pd.Timestamp('2000-01-01'), 'is_active': True},
...     {'firstname': 'Bob', 'lastname': 'Blackthorn', 'age': 31, 'lastlogin': pd.Timestamp('2000-01-02'), 'is_active': True},
...     {'firstname': 'Carol', 'lastname': 'Corn', 'age': 32, 'lastlogin': pd.Timestamp('2000-01-03'), 'is_active': False},
...     {'firstname': 'Dave', 'lastname': 'Durian', 'age': 33, 'lastlogin': pd.Timestamp('2000-01-04'), 'is_active': False},
...     {'firstname': 'Eve', 'lastname': 'Elderberry', 'age': 34, 'lastlogin': pd.Timestamp('2000-01-05'), 'is_active': True},
...     {'firstname': 'Mallory', 'lastname': 'Melon', 'age': 15, 'lastlogin': pd.NaT, 'is_active': None},
... ])
>>>
>>> data
  firstname    lastname  age  lastlogin is_active
0     Alice     Apricot   30 2000-01-01      True
1       Bob  Blackthorn   31 2000-01-02      True
2     Carol        Corn   32 2000-01-03     False
3      Dave      Durian   33 2000-01-04     False
4       Eve  Elderberry   34 2000-01-05      True
5   Mallory       Melon   15        NaT      None

4.3.2. Example

>>> data.to_json('/tmp/myfile.json', date_format='iso')
$ cat /tmp/myfile.json
{"firstname":{"0":"Alice","1":"Bob","2":"Carol","3":"Dave","4":"Eve","5":"Mallory"},"lastname":{"0":"Apricot","1":"Blackthorn","2":"Corn","3":"Durian","4":"Elderberry","5":"Melon"},"age":{"0":30,"1":31,"2":32,"3":33,"4":34,"5":15},"lastlogin":{"0":"2000-01-01T00:00:00.000","1":"2000-01-02T00:00:00.000","2":"2000-01-03T00:00:00.000","3":"2000-01-04T00:00:00.000","4":"2000-01-05T00:00:00.000","5":null},"is_active":{"0":true,"1":true,"2":false,"3":false,"4":true,"5":null}}

4.3.3. As Records

>>> data.to_json(
...     '/tmp/myfile.json',
...     indent=2,
...     date_format='iso',
...     orient='records'
... )
$ cat /tmp/myfile.json
[
  {
    "firstname":"Alice",
    "lastname":"Apricot",
    "age":30,
    "lastlogin":"2000-01-01T00:00:00.000",
    "is_active":true
  },
  {
    "firstname":"Bob",
    "lastname":"Blackthorn",
    "age":31,
    "lastlogin":"2000-01-02T00:00:00.000",
    "is_active":true
  },
  {
    "firstname":"Carol",
    "lastname":"Corn",
    "age":32,
    "lastlogin":"2000-01-03T00:00:00.000",
    "is_active":false
  },
  {
    "firstname":"Dave",
    "lastname":"Durian",
    "age":33,
    "lastlogin":"2000-01-04T00:00:00.000",
    "is_active":false
  },
  {
    "firstname":"Eve",
    "lastname":"Elderberry",
    "age":34,
    "lastlogin":"2000-01-05T00:00:00.000",
    "is_active":true
  },
  {
    "firstname":"Mallory",
    "lastname":"Melon",
    "age":15,
    "lastlogin":null,
    "is_active":null
  }
]

4.3.4. As Columns

>>> data.to_json(
...     '/tmp/myfile.json',
...     indent=2,
...     date_format='iso',
... )
$ cat /tmp/myfile.json
{
  "firstname":{
    "0":"Alice",
    "1":"Bob",
    "2":"Carol",
    "3":"Dave",
    "4":"Eve",
    "5":"Mallory"
  },
  "lastname":{
    "0":"Apricot",
    "1":"Blackthorn",
    "2":"Corn",
    "3":"Durian",
    "4":"Elderberry",
    "5":"Melon"
  },
  "age":{
    "0":30,
    "1":31,
    "2":32,
    "3":33,
    "4":34,
    "5":15
  },
  "lastlogin":{
    "0":"2000-01-01T00:00:00.000",
    "1":"2000-01-02T00:00:00.000",
    "2":"2000-01-03T00:00:00.000",
    "3":"2000-01-04T00:00:00.000",
    "4":"2000-01-05T00:00:00.000",
    "5":null
  },
  "is_active":{
    "0":true,
    "1":true,
    "2":false,
    "3":false,
    "4":true,
    "5":null
  }
}

4.3.5. JSON Lines

>>> data.to_json(
...     '/tmp/myfile.json',
...     date_format='iso',
...     orient='records',
...     lines=True,
... )
$ cat /tmp/myfile.json
{"firstname":"Alice","lastname":"Apricot","age":30,"lastlogin":"2000-01-01T00:00:00.000","is_active":true}
{"firstname":"Bob","lastname":"Blackthorn","age":31,"lastlogin":"2000-01-02T00:00:00.000","is_active":true}
{"firstname":"Carol","lastname":"Corn","age":32,"lastlogin":"2000-01-03T00:00:00.000","is_active":false}
{"firstname":"Dave","lastname":"Durian","age":33,"lastlogin":"2000-01-04T00:00:00.000","is_active":false}
{"firstname":"Eve","lastname":"Elderberry","age":34,"lastlogin":"2000-01-05T00:00:00.000","is_active":true}
{"firstname":"Mallory","lastname":"Melon","age":15,"lastlogin":null,"is_active":null}

4.3.6. Assignments

# %% About
# - Name: Pandas To JSON
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Export data from `data` to file `FILE`
# 2. Data has to be in JSON format
# 3. Run doctests - all must succeed

# %% Polish
# 1. Wyeksportuj dane z `data` do pliku `FILE`
# 2. Dane mają być w formacie JSON
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# {"firstname":{"0":"Alice","1":"Bob","2":"Carol","3":"Dave","4":"Eve","5":"Mallory"},"lastname":{"0":"Apricot","1":"Blackthorn","2":"Corn","3":"Durian","4":"Elderberry","5":"Melon"},"age":{"0":30,"1":31,"2":32,"3":33,"4":34,"5":15},"email":{"0":"alice@example.com","1":"bob@example.com","2":"carol@example.com","3":"dave@example.org","4":"eve@example.org","5":"mallory@example.net"},"lastlogin":{"0":"2000-01-01","1":"2000-01-02","2":"2000-01-03","3":"2000-01-04","4":"2000-01-05","5":null},"is_active":{"0":true,"1":true,"2":true,"3":true,"4":true,"5":false},"groups":{"0":"users;staff","1":"users;staff","2":"users","3":"users","4":"users;staff;admins","5":null}}

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> from os import remove
>>> result = open(FILE).read()

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is str, \
'Variable `result` has an invalid type; expected: `str`.'

>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)

>>> from pprint import pprint
>>> print(result)  # doctest: +NORMALIZE_WHITESPACE
{"firstname":{"0":"Alice","1":"Bob","2":"Carol","3":"Dave","4":"Eve","5":"Mallory"},"lastname":{"0":"Apricot","1":"Blackthorn","2":"Corn","3":"Durian","4":"Elderberry","5":"Melon"},"age":{"0":30,"1":31,"2":32,"3":33,"4":34,"5":15},"email":{"0":"alice@example.com","1":"bob@example.com","2":"carol@example.com","3":"dave@example.org","4":"eve@example.org","5":"mallory@example.net"},"lastlogin":{"0":"2000-01-01","1":"2000-01-02","2":"2000-01-03","3":"2000-01-04","4":"2000-01-05","5":null},"is_active":{"0":true,"1":true,"2":true,"3":true,"4":true,"5":false},"groups":{"0":"users;staff","1":"users;staff","2":"users","3":"users","4":"users;staff;admins","5":null}}

>>> remove(FILE)
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import pandas as pd

# %% Types

# %% Data
DATA = 'https://python3.info/_static/example.json'
FILE = r'_temporary.json'

data = pd.read_json(DATA)

# %% Result