1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python JSONデータをソートする

Last updated at Posted at 2022-01-07

はじめに

VSCodeのsettings.jsonを編集していて、設定項目が散らばっていたので、ソートさせるために書きました。
調べたところ、簡単な方法がなく、とりあえずPythonで書きました。

もっと簡単な方法があるよ、Pythonだとこう書いたほうがいいよ。ということがあったら教えていただけましたら幸いです。

Code

from collections import OrderedDict
import json

with open('mydata.json', encoding='utf-8') as file:
    data = json.load(file)
    ordereddictdata = OrderedDict(sorted(data.items(), key=lambda x: x[0]))
    json.dump(ordereddictdata, open('mydata_ordered.json', 'w', encoding='utf-8'), ensure_ascii=False, indent=4)
1
1
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?