【Python】Python ~ 基本編 / JSONあれこれ ~

■ はじめに

https://dk521123.hatenablog.com/entry/2019/10/19/104805

の続き。

JSONに関して扱うことになったので、メモ。

目次

【1】JSONインスタンスをコピーする
【2】JSONをマージする
【3】JSONオブジェクトをフィルタリングする
【4】SELECTした結果をJSON化
 1)PostgreSQLの場合

【1】JSONインスタンスをコピーする

<json>.copy() を使う

サンプル

import json

input_dict = {"name": "Smith", "birth_day": "2002-12-21", "gender": "man"}
clone_dict = input_dict.copy()
clone_dict["name"] = "Mike"

# {'name': 'Smith', 'birth_day': '2002-12-21', 'gender': 'man'}
print(input_dict)
# {'name': 'Mike', 'birth_day': '2002-12-21', 'gender': 'man'}
print(clone_dict)

【2】JSONをマージする

<json1>.update(<json2>) を使う

※ キーが重複する場合

⇒ 引数 <json2> で上書きされる

サンプル

import json

input1_dict = {"name": "Smith", "birth_day": "1967-08-11", "gender": "man"}
input2_dict = {"birth_day": "2012-12-23", "occupation": "IT", "note": "Hello world"}
input1_dict.update(input2_dict)

# {'name': 'Smith', 'birth_day': '2012-12-23', 'gender': 'man', 'occupation': 'IT', 'note': 'Hello world'}
print(input1_dict)

【3】JSONオブジェクトをフィルタリングする

* lambda / filter を使うといいかも。

https://stackoverflow.com/questions/40827356/find-a-value-in-json-using-python/40827383

サンプル

例1:lambda / filterでフィルタリング

user_list = [
  {
    "id": "X0001",
    "name": "Mike",
    "birth_date": None
  },
  {
    "id": "X0002",
    "name": "Tom",
    "birth_date": "2022-01-01"
  },
  {
    "id": "X0003",
    "name": "Paul",
    "birth_date": "1988-12-12"
  }
]

results = list(filter(lambda x: x["id"] == "X0003", user_list))

# [{'id': 'X0003', 'name': 'Paul', 'birth_date': '1988-12-12'}]
print(results)

例2:汎用的に使えるように関数化

def find(target_list: list, filter_dict: dict):
  return_values = target_list
  for key, value in filter_dict.items():
    return_values = list(filter(lambda x: x[key] == value, return_values))
  return return_values

user_list = [
  {
    "id": "X0001",
    "name": "Mike",
    "birth_date": None
  },
  {
    "id": "X0002",
    "name": "Tom",
    "birth_date": "2022-01-01"
  },
  {
    "id": "X0003",
    "name": "Paul",
    "birth_date": "1988-12-12"
  },
  {
    "id": "X0004",
    "name": "Mike",
    "birth_date": "1988-12-12"
  },
  {
    "id": "X0005",
    "name": "Paul",
    "birth_date": "1988-12-12"
  },
]

results = find(user_list, {"name": "Mike", "birth_date": "1988-12-12"})

# [{'id': 'X0004', 'name': 'Mike', 'birth_date': '1988-12-12'}]
print(results)

【4】SELECTした結果をJSON

1)PostgreSQLの場合

* 以下の関連記事を参照のこと

PythonPostgreSQL を使う ~ psycopg2編 ~
https://dk521123.hatenablog.com/entry/2020/05/06/141029

関連記事

Python ~ 基本編 / 文字列 ~
https://dk521123.hatenablog.com/entry/2019/10/12/075251
Python ~ 基本編 / JSON
https://dk521123.hatenablog.com/entry/2019/10/19/104805
Python ~ 基本編 / YAML
https://dk521123.hatenablog.com/entry/2019/10/16/225401
Python ~ 基本編 / ラムダ lambda ~
https://dk521123.hatenablog.com/entry/2019/09/23/000000
Python ~ 基本編 / map ~
https://dk521123.hatenablog.com/entry/2021/03/29/000000
Python ~ 基本編 / 辞書 ~
https://dk521123.hatenablog.com/entry/2019/10/27/100014
Python ~ 基本編 / 辞書・あれこれ ~
https://dk521123.hatenablog.com/entry/2020/10/11/000000
PythonPostgreSQL を使う ~ psycopg2編 ~
https://dk521123.hatenablog.com/entry/2020/05/06/141029
Pandas ~ 基本編 / JSON編 ~
https://dk521123.hatenablog.com/entry/2022/02/16/000000
Python ~ ndjson を扱う ~
https://dk521123.hatenablog.com/entry/2022/08/30/224248