The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Pretty Print JSON In Python

TL;DR

Use `sort_keys`, `indent`, and `default` to pretty print JSON from Python

TODO: Pull subtitle into page object
Code
import json

json_structure = { 
  "metadata": { "version": "0.1.0" }
}

json_string = json.dumps(
    json_structure, 
    sort_keys=True, 
    indent=2, 
    default=str
)

print(json_string)
Results
{
  "metadata": {
    "version": "0.1.0"
  }
}

The `default=str` argument prefents issues when converting Date objects.

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Pretty Print JSON In Python

-- tldr/

Use `sort_keys`, `indent`, and `default` to pretty
print JSON from Python

-- /tldr


-- code
-- python

import json

json_structure = { 
  "metadata": { "version": "0.1.0" }
}

json_string = json.dumps(
    json_structure, 
    sort_keys=True, 
    indent=2, 
    default=str
)

print(json_string)


-- results/

{
  "metadata": {
    "version": "0.1.0"
  }
}

-- /results

-- p


The `default=str` argument prefents issues when converting Date objects.


-- metadata
-- date: 2017-11-16
-- id: 0wrnbdkx
-- site: aws
-- type: post
-- status: published