Python での YAML と JSON の相互変換

https://pyyaml.org/
https://pypi.org/project/PyYAML/
を使う。
pip install pyyaml

相互変換の媒介として辞書として扱う

YAMLJSON
sample.yaml

title: サンプル
item:
- id: 10
  name: Red
  value:
  - 111
  - 222
- id: 20
  name: Blue
  phone:
  - 333
  - 444

sample.yaml を読み込んで JSON としてダンプ

# -*- coding: utf-8 -*-
import yaml
import json

with open('sample.yaml', encoding='utf8' ) as file:
    sample_dict = yaml.safe_load(file)
    js = json.dumps(sample_dict, ensure_ascii=False, indent=2)
    print(js)

ensure_ascii=False を指定しないと
日本語の文字列、 "サンプル" → "\u30b5\u30f3\u30d7\u30eb" になる
結果、、、

{
  "title": "サンプル",
  "item": [
    {
      "id": 10,
      "name": "Red",
      "value": [
        111,
        222
      ]
    },
    {
      "id": 20,
      "name": "Blue",
      "phone": [
        333,
        444
      ]
    }
  ]
}

JSONYAML
sample.json を読み込んで YAML としてダンプ

# -*- coding: utf-8 -*-
import yaml
import json

with open('sample.json', encoding='utf8') as file:
    sample_dict = json.load(file)
    ym = yaml.dump(sample_dict, sort_keys=False, allow_unicode=True)
    print(ym)

JSONから読込んだ辞書の順序を維持する為に、sort_keys=False を指定する
allow_unicode=True を指定しないと
日本語の文字列、 サンプル → \u30B5\u30F3\u30D7\u30EB になる
結果、、、

title: サンプル
item:
- id: 10
  name: Red
  value:
  - 111
  - 222
- id: 20
  name: Blue
  phone:
  - 333
  - 444