import json

json_string = None

with open("json_file.json") as f:
json_string = f.read()
try:
parsed_json = json.loads(json_string)
formatted_json = json.dumps(parsed_json, indent = 4,sort_keys=True)
with open("json_file.json","w") as f:
f.write(formatted_json)
except Exception as e:
print(repr(e))

Parameter indent  decides the number of spaces to use for indentation.

Parameter sort_keys decides whether the keys in formatted JSON should be in sorted order or not. Default value is False.

Bonus: You can pretty-print the small JSON string directly at command terminal without using python script file. Use below line of code. 
echo '{"first_name": "Anurag", "last_name": "rana"}' | python -m json.tool

最新文章

  1. 用idea写servlet文件
  2. Bootstrap 类解析
  3. css 中的position z-index em rem zoom 的基本用法
  4. 《Code Complete》ch.25 代码调整策略
  5. Linux解压 tar命令
  6. maven系列(3)-maven生命周期的介绍
  7. 如何在xcode下面同时安装cocos2d-iphone 和 cocos2d-x模板,其实是因为很喜欢C++的缘故,当时学习的是前者,现在自己摸着石头过河了就(cocos2d-x安装失败 出错)
  8. IPv4头部结构具体解释
  9. 有关Struts2a的ction直接使用response异步问题
  10. Bash函数
  11. [C#]使用GroupJoin将两个关联的集合进行分组
  12. 02.将SDK获取到的ECS主机信息入库
  13. [SCOI 2011]糖果
  14. ffdshow 源代码分析 9: 编解码器有关类的总结
  15. C#算法与数据结构之线性结构
  16. 个人博客搭建( wordpress )
  17. php中安装memcache
  18. DevExpress v18.1新版亮点——Office File API篇
  19. Linux Foundation(笔记)
  20. vue mock(模拟后台数据) 最简单实例(一)——适合小白

热门文章

  1. 阿拉伯数字转中文大写数字的JS
  2. PHP面试题 – 培训学校真实面试内部资料
  3. 20145230《java程序设计》第五次实验报告
  4. 8.14比赛j题 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87813#overview
  5. Centos6.5下Hbase安装
  6. SG函数略解
  7. 【P2564】生日礼物(单调队列)
  8. Phoenix性能优化
  9. [kuangbin带你飞]专题十 匹配问题 一般图匹配
  10. Avoid nesting too deeply and return early避免嵌套太深应尽早返回