20 ms·
Non web based approach using jq (https://stedolan.github.io/jq/ https://stedolan.github.io/jq/): json_source_on_stdout | jq .
by dividuum 7y ago
Non web based approach using jq (https://stedolan.github.io/jq/ https://stedolan.github.io/jq/):
json_source_on_stdout | jq .
- camel_Snake 7y agoPython also has one built in. cat input.json | python3 -m json.tool
- atomwaffel 7y agoOr on macOS, to quickly format the contents of your clipboard: pbpaste | jq . | pbcopy Pretty sure this is one of my most frequent commands.
- ben509 7y agoYou can use Automator to create a service with a shell script action; just pick Quick Action, the rest should be pretty obvious.
- blaydator 7y agoNot so obvious to me ! I have created the Quick Action but I don't know how to trigger it, so I have saved it as an app. Also when I execute it from Automator (play button), the script doesn't work (nothing is pasted). Would you mind helping ? Looks the best way to quickly format a copied json ! Thanks
- ben509 7y agoYeah, that was a very terse explanation, and Automator is so user-friendly it's hard to use. 1. Start Automator. 2. In the "Choose a type..." sheet, choose "Quick Action". 3. Set these: Workflow recieves "current text" in "any application". Input is "entire selection". [X] Output replaces text. 4. Add "Run shell script" and select your preferred shell. jq . (Note that Automator is handling your stdin / stdout.) 5. File / Save. You can now find your workflow by command-clicking the document name in the window's titlebar. It should be in /Users/You/Library/Services Now, fire up TextEdit, enter some JSON. Right-click, select Services and your service should be present. It's also in the TextEdit menu under Services.
- ben509 7y agoI want to like jq, and for doing a simple lookup it's great, but I find when I try to do something complicated the syntax just doesn't click.
- gregopet 7y agoI know, right? I've switched to using jp: its syntax is based on an actual standard and it clicks much more with me than jq's syntax. https://github.com/jmespath/jp https://github.com/jmespath/jp
- hotsilog 7y agoThanks for this suggestion. I too also have problems dealing with jq syntax. jp looks a lot more intuitive.
- mcintyre1994 7y agoNice! I didn’t actually realise JMESPath was a standard, I first came across it in the AWS CLI and definitely found it nicer than jq. Good to avoid trying to learn both.
- ben509 7y agoPlaying with `jp` a bit, I still like it, but it's very frustrating to do updates in place. For example, I wanted to write a simple expression to delete outputs from an ipython notebook. { "cells": [{ "stuff": "blah blah", "outputs": ["crap", "to", "delete"] }], "other stuff": "blah blah" } The simplest expression I could come up with was: merge(@, { "cells": map( &merge(@, {"outputs": `[]`}), cells[] ) }) And that's verbose because you're reconstructing the structure. In fairness, it's impressive that the language can express it at all, given it's a query language. Maybe the real answer is to denote a subset of JMESPath that is guaranteed to return assignable nodes, that is only selects, slices, filters, etc. Then you run the query and perform assignments against those.
- tlrobinson 7y agoOS X has the Perl-based `json_pp` built in too.
- eridius 7y agoYou can go even simpler json_source_on_stdout | jq If you give `jq` no arguments it acts like `jq .`
- ashelmire 7y agoI think it might be useful to note for other readers here that jq is very powerful for parsing json - you can programatically search, select, map, and mutate parts of any json. If you work with a lot of json, it's worth checking out.