1
1

More than 1 year has passed since last update.

Pythonでjson内の文字置換をする

Last updated at Posted at 2022-10-11

まえおき

開発環境のJSONファイルを本番環境用の文字列に置き換えるため、Pythonを使用してyamlファイルから設定出来るように実装を行いました。

準備・コード実行

yamlをpythonで扱えるようにライブラリをインストールします。

pip install pyyaml

以下のyamlファイルを作成します。

replace.yaml
replace:
    - index: 番号
      before: 置換前文字
      after:  置換後文字

コードは以下になります。
実行後、指定したtest.jsonの内容が置き換えられます。

replace_json.py
import yaml

with open(r'YAMLファイルパス', encoding='utf-8') as readYaml:
    yaml = yaml.load(readYaml, Loader=yaml.Loader)
 
with open(r'JSONファイルパス',"rt", encoding='utf-8') as readParameter:
    parameter = readParameter.read()

with open(r'JSONファイルパス',"wt", encoding='utf-8') as writeParameter:
    print(parameter.encode('utf-8'))

    for item in yaml['replace']:
        print(item['index'])
        print(item['before'])
        print(item['after'])
        parameter = parameter.replace(str(item['before']),str(item['after']))
    writeParameter.write(parameter)

JSONジェネレーターからテスト用のJsonファイルを作成します。

test.json
 {
    "_id": "6341211ace889273f2944ecd",
    "index": 0,
    "guid": "382e6b22-7061-4a54-a7c7-866c08fc9004",
    "isActive": true,
    "balance": "$2,822.82",
    "picture": "http://placehold.it/32x32",
    "age": 28,
    "eyeColor": "green",
    "name": "Moran Hahn",
    "gender": "male",
    "company": "ZAYA",
    "email": "moranhahn@zaya.com",
    "phone": "+1 (816) 565-2323",
    "address": "142 Stockholm Street, Holtville, Illinois, 5283",
    "about": "Et fugiat qui ad minim ut ex est officia dolore. Ipsum sunt ex ad laborum sunt consequat nostrud nostrud enim. Pariatur nostrud irure eu sunt adipisicing incididunt enim sit nostrud anim reprehenderit. Duis tempor qui esse id aliquip dolor. Elit commodo nulla et anim sunt ea cillum ut pariatur aliqua ipsum esse.\r\n",
    "registered": "2019-02-27T11:33:52 -09:00",
    "latitude": -60.572008,
    "longitude": 153.823886
 }

テスト用の置き換えyamlルールは以下にで作成します。

replace.yaml
replace:
    - index: 1
      before: http://placehold.it/32x32
      after:  http://xxxxxx/xxxxxx
      
    - index: 2
      before: moranhahn@zaya.com
      after:  xxxxxx@xxxx.xxx

    - index: 3
      before: -60.572008
      after:  -0.0

実行ログ
image.png

置き換え後のtest.jsonファイルです。

test.json
 {
    "_id": "6341211ace889273f2944ecd",
    "index": 0,
    "guid": "382e6b22-7061-4a54-a7c7-866c08fc9004",
    "isActive": true,
    "balance": "$2,822.82",
    "picture": "http://xxxxxx/xxxxxx",
    "age": 28,
    "eyeColor": "green",
    "name": "Moran Hahn",
    "gender": "male",
    "company": "ZAYA",
    "email": "xxxxxx@xxxx.xxx",
    "phone": "+1 (816) 565-2323",
    "address": "142 Stockholm Street, Holtville, Illinois, 5283",
    "about": "Et fugiat qui ad minim ut ex est officia dolore. Ipsum sunt ex ad laborum sunt consequat nostrud nostrud enim. Pariatur nostrud irure eu sunt adipisicing incididunt enim sit nostrud anim reprehenderit. Duis tempor qui esse id aliquip dolor. Elit commodo nulla et anim sunt ea cillum ut pariatur aliqua ipsum esse.\r\n",
    "registered": "2019-02-27T11:33:52 -09:00",
    "latitude": -0.0,
    "longitude": 153.823886
 }

上手く置換出来ていることを確認。

これで以上になります。
見て下さってありがとうございました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1