Skip to content

Instantly share code, notes, and snippets.

@RussellLuo
Last active May 12, 2017 06:27
Show Gist options
  • Select an option

  • Save RussellLuo/23b5116ed3593bfdb36265df4930ab2b to your computer and use it in GitHub Desktop.

Select an option

Save RussellLuo/23b5116ed3593bfdb36265df4930ab2b to your computer and use it in GitHub Desktop.

Revisions

  1. RussellLuo revised this gist May 12, 2017. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion pretty_print_json.sh
    Original file line number Diff line number Diff line change
    @@ -1 +1,9 @@
    echo '{"hello":"你好"}' | python -c 'import json, sys; print(json.dumps(json.loads(sys.stdin.read()), ensure_ascii=False, indent=2).encode("utf-8"))'
    # Method 1
    # Pros: really short
    # Cons: unicode will be escaped
    echo '{"hello":"你好"}' | python -mjson.tool

    # Method 2
    # Pros: avoid unicode escapes, output string is always UTF-8 encoded
    # Cons: a little long
    echo '{"hello":"你好"}' | python -c 'import json, sys; print(json.dumps(json.load(sys.stdin), ensure_ascii=False, indent=2).encode("utf-8"))'
  2. RussellLuo renamed this gist May 12, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pretty_json.sh → pretty_print_json.sh
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    echo '{"hello":"你好"}' | python -c 'import json, sys; print(json.dumps(json.loads(sys.stdin.read()), ensure_ascii=False, indent=2))'
    echo '{"hello":"你好"}' | python -c 'import json, sys; print(json.dumps(json.loads(sys.stdin.read()), ensure_ascii=False, indent=2).encode("utf-8"))'
  3. RussellLuo created this gist May 12, 2017.
    1 change: 1 addition & 0 deletions pretty_json.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    echo '{"hello":"你好"}' | python -c 'import json, sys; print(json.dumps(json.loads(sys.stdin.read()), ensure_ascii=False, indent=2))'