{Python}JSONファイル読込

 

import json


with open('a.json', 'w', encoding='utf-8', newline='') as f:

  f.write('[\n')
  f.write('    {\n')
  f.write('        "instanceid": "i-22222222222222222",\n')
  f.write('        "Tags": "work2"\n')
  f.write('    },\n')
  f.write('    {\n')
  f.write('        "instanceid": "i-33333333333333333",\n')
  f.write('        "Tags": "work3"\n')
  f.write('    },\n')
  f.write('    {\n')
  f.write('        "instanceid": "i-11111111111111111",\n')
  f.write('        "Tags": "work1"\n')
  f.write('    }\n')
  f.write(']\n')


with open('a.json', 'r', encoding='utf-8', newline='') as f:
  print( f.read() )
  
with open('a.json', 'r', encoding='utf-8', newline='') as f:
  json_string = json.load(f)

json_string

for j in json_string:
  print(j)
  for k,v in j.items():
    print(k, v)


json_string[0]["Tags"] = ""

with open('b.json','w', encoding='utf-8',newline='') as f:
  json.dump(json_string, f, indent=2)

with open('b.json', 'r', encoding='utf-8', newline='') as f:
  print( f.read() )