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."
tags:
- 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:
```json
{
"key": [
123,
456
],
"key2": "value"
}
```
If we want to minify the JSON, we can use the following one-liner:
```bash
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!
```json
{"key": [124, 456], "key2": "value"}
```
---
posts:
- https://www.jvt.me/posts/2019/11/23/minify-json-python/
- https://www.jvt.me/posts/2019/11/22/minify-json-ruby/
- https://www.jvt.me/posts/2019/11/21/git-add-tracked/
- https://www.jvt.me/posts/2019/11/20/intelligent-webmentions/
......
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