Using jq to format JSON on the clipboard

(chris48s.github.io)

60 points | by stefanvdw1 2 days ago ago

22 comments

  • _flux a day ago ago

    I use similar approach for fixing up clipboard contents with

      tr '\n' ' ' | sed 's/\t/ /g; s/  */ /g'
    
    But also I switched to xsel at one point, as xclip doesn't seem to deal as well with unicode. It could be xclip has been fixed since then, though.
    • CamJN a day ago ago

      /\ \ */ is the same as / +/, but if you only want to replace 2+ spaces and leave the single spaces alone, then what you really want is /\ \ +/.

      Edit: had to escape the spaces because HN ate my spaces.

      • _flux a day ago ago

        You need to use -E to get that behavior with GNU Sed.

  • OskarS a day ago ago

    Lovely little alias, but I much prefer fracjson[0] pretty printing to jq, so I might go with that instead!

    [0]: https://github.com/j-brooke/FracturedJson

  • dpflan a day ago ago

    There is also: - xq for xml - yq for yaml

    • mcoliver a day ago ago

      I wrote uq specifically for urls https://github.com/mcoliver/uq

    • akdev1l a day ago ago

      you can use xmllint for xml via xmllint —-xpath

      It’s great as it is included by default in RHEL/Fedora

      • 11 hours ago ago
        [deleted]
    • dividedbyzero a day ago ago

      yq can do a lot more than just yaml:

      > yq is a portable command-line YAML, JSON, XML, CSV, TOML, HCL and properties processor

  • bilkow a day ago ago

    Loved it! I made this version for `fish` and `wl-clipboard`:

        function clipboard-format-json
            # Inspired by https://chris48s.github.io/blogmarks/posts/2021/jsontidy/
            if set -l formatted "$(wl-paste | jq '.')"
                wl-copy $formatted
            end
        end
    
    By saving it to .config/fish/functions/clipboard-format-json.fish, it should immediately work
  • wodenokoto 21 hours ago ago

    I set up my Linux on WSL to use 'pbpaste' and 'pbcopy' as shorthand’s for the long and hard to remember native commands.

    On mobile, but I believe for one of them on WSL is something like 'paste.exe'

  • netsharc a day ago ago

    A universal tool (well, since I use Windows, a Windows tool) to modify the clipboard would be cool.

    So, copy text to clipboard, press a hotkey to popup the menu, and it'll offer options depending on the detected text. For example changing the clipboard contents to prettified (if it detects JSON, XML, HTML), or formatting a number back and forth (e.g. working with SSN's out of numerical DB entries when they prefer AAA-GG-SSSS).

  • chamomeal a day ago ago

    I have a similar alias for borkdude’s ‘jet’ tool.

    The extra nice thing about jet is you can convert json into edn, then pipe it into an inline babashka repl where you can inspect the object with all the power of clojure. It is super duper nice for quickly looking through hunks (even huge hunks) of json. You can sum things up, filter through thousands of elements, generate a json schema with malli, whatever you want!

  • 1-more a day ago ago

    I do a lot of this and `pbpaste | sed s/^ // | pbcopy` and things like that. It just fits my brain better. What's nice about raycast as a macOS pasteboard manager is that whatever you last selected via Cmd-Shift-V is the output of `pbpaste`. I'm not 100% sure how that is affected by copying images though.

  • lkbm a day ago ago

    I have this as a bash script and a some similar bash and python scripts for converting between python's print(my_dict) and json, or to run some python (mostly the dicts) through black.

    The j2p is pretty self-evident if you know any python, and the only trick to p2j was knowing `ast.literal_eval(input_str)`.

    • megagpt1 a day ago ago

      Should be print(repr(my_dict)) in case my_dict is actually a string, or you'd have an injection attack

  • a day ago ago
    [deleted]
  • nullgeo a day ago ago

    I like the version where it throws the error. That way I will realize I might not have copied the right JSON (or forgotten to copy)

  • pletnes a day ago ago

    Cool idea! I think it would look better as a function than an alias, but that doesn’t matter much.

  • mhalikosen a day ago ago

    I didn't even know such a tool existed until Opus used it.

  • stefanvdw1 2 days ago ago

    Not mine, just very useful

  • jdbloodstone a day ago ago

    [dead]