Commit 5dccc497 authored by Jamie Tanna's avatar Jamie Tanna

Document minifying JSON with Python

Closes #803.
parent 82a3607c
Pipeline #98083105 passed with stages
in 7 minutes and 37 seconds
title: "Minifying JSON with Python"
description: "How to take a pretty-printed JSON string and replace it with a minifed JSON string using Python."
- python
- command-line
- blogumentation
- nablopomo
- json
- minify-json
license_code: Apache-2.0
license_prose: CC-BY-NC-SA-4.0
date: 2019-11-23T19:58:07+0000
slug: "minify-json-python"
series: nablopomo-2019
Yesterday I wrote about [_Minifying JSON Ruby_]({{< ref "2019-11-22-minify-json-ruby" >}}), but today let's talk about using Python instead.
Let's say that we have a nicely pretty-printed JSON file, such as:
"key": [
"key2": "value"
If we want to minify the JSON, we can use the following one-liner:
python -c $'import json\nimport sys\nwith open(sys.argv[1], "r") as f: print(json.dumps(json.load(f)))' file.json
This then outputs it as the minified JSON string representation of the object above!
{"key": [124, 456], "key2": "value"}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment