LoginSignup
26
23

More than 3 years have passed since last update.

json を pretty print するPythonワンライナー。日本語をエスケープしない。

Last updated at Posted at 2014-12-06

「ensure_ascii=False」がミソ。

Terminal
$ echo '{"one":1, "two":2, "日本語":"あああ"}' | python -c 'import sys,json;print(json.dumps(json.loads(sys.stdin.read()),indent=4,ensure_ascii=False))'

{
    "日本語": "あああ", 
    "two": 2, 
    "one": 1
}

本当は以下のように python -mtool.json ですませたいけど、日本語部分がエスケープされてしまう。

Terminal
$ echo '{"one":1, "two":2, "日本語":"あああ"}' | python -mjson.tool                                                                                    
{
    "one": 1,
    "two": 2,
    "\u65e5\u672c\u8a9e": "\u3042\u3042\u3042"
}
26
23
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
26
23