• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

jsonlフォーマット

date: 2022-05-24 excerpt: jsonlフォーマットの使い方

tag: jsonjsonl


jsonlフォーマットの使い方

概要

  • JSON Linesというフォーマットの1つ
  • column情報を含んだcsvのようなデータ
    • {"key1": "val1", "key2": "val2", ...}
  • 拡張子はjsonl
  • 文字コードはUTF8
  • ラインセパレータは\n

具体的なユースケース

APIの戻り値

  • GCPの多くがjsonl形式になっており、統一性のために求められることがある

具体例

{
  "result": [
    {"key1": "val2", "key2": "val2", ...},
    {"key1": "val2", "key2": "val2", ...}
  ]
}

BigQueryへのアップロードデータ

  • BigQueryへデータをアップロードする際のファイル形式としての利用

pandasの関数でjsonl形式で出力する

import pandas as pd

df = pd.DataFrame()
df["A"] = [1, 2, 3]
df["B"] = ["a", "b", "c"]

data = df.to_json(orient="records", lines=True, force_ascii=False)

print(data)
"""
{"A":1,"B":"a"}
{"A":2,"B":"b"}
{"A":3,"B":"c"}

"""

参考

  • JSON Lines/Documentation for the JSON Lines text file format


jsonjsonl Share Tweet