Commit 3e827ae6 authored by Jamie Tanna's avatar Jamie Tanna

Document inline pretty-printing of JSON

Closes #604.
parent 84241d50
Pipeline #72708849 passed with stages
in 10 minutes and 5 seconds
title: "Pretty Printing JSON Files Inline on the Command Line"
description: "How to rewrite multiple JSON files inline on the Command Line."
- blogumentation
- pretty-print
- command-line
- ruby
license_code: Apache-2.0
license_prose: CC-BY-NC-SA-4.0
date: 2019-07-24T14:26:05+0100
slug: "pretty-print-json-files-inline"
Today I've found myself needing to compare a number of JSON files to see whether certain configuration has been pulled in correctly.
Usually this would be a fairly routine task (as I find myself doing this quite often), but some of the JSON files seemed to be structured differently, so I wanted to pretty-print them in the same way so it was more easily to diff them.
To do this, I reused steps from [_Pretty Printing JSON on the Command Line with Ruby_]({{< ref "2019-03-29-pretty-printing-json-ruby" >}}) to perform the pretty-printing, and added a little bit of extra code to perform the inline editing.
Let us say that we're wanting to change all JSON files in the directory `config/`, as well as any and all nested subdirectories it has. This would give us the following Ruby code:
require 'json'
Dir.glob('config/**/*.json').each do |file|
json = JSON.parse( file), 'w') do |out|
out.write(JSON.pretty_generate json)
However, this is a pain if we want to have an easy-to-copy/reuse method, so we can trim it to a fun one-liner:
ruby -rjson -e 'Dir.glob("config/**/*.json").each { |f| j = JSON.parse(;, "w") { |o| o.write(JSON.pretty_generate(j)) } }'
Alternatively, to more easily make it an alias/function, you can extract the glob into an argument:
ruby -rjson -e 'Dir.glob(ARGV[0]).each { |f| j = JSON.parse(;, "w") { |o| o.write(JSON.pretty_generate(j)) } }' 'config/**/*.json'
Which allows aliasing as such:
alias ppj="ruby -rjson -e 'Dir.glob(ARGV[0]).each { |f| j = JSON.parse(;, \"w\") { |o| o.write(JSON.pretty_generate(j)) } }'"
ppj 'config/**/*.json'
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