Hacker News new | past | comments | ask | show | jobs | submit login

Non web based approach using jq (https://stedolan.github.io/jq/):

    json_source_on_stdout | jq .



Or on macOS, to quickly format the contents of your clipboard:

    pbpaste | jq . | pbcopy
Pretty sure this is one of my most frequent commands.


You can use Automator to create a service with a shell script action; just pick Quick Action, the rest should be pretty obvious.


Not 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


Yeah, 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.


Python also has one built in.

cat input.json | python3 -m json.tool


I 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.


I 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


Playing 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.


Nice! 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.


Thanks for this suggestion. I too also have problems dealing with jq syntax. jp looks a lot more intuitive.


You can go even simpler

  json_source_on_stdout | jq
If you give `jq` no arguments it acts like `jq .`


I 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.


OS X has the Perl-based `json_pp` built in too.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: