Using Jmespath in Emacs

Using Jmespath in Emacs


Introduction

When you have a small JSON file, it is quite easy to look for what you want. But querying a large JSON data is very troublesome.

This is where people use tools like Jmespath to filter and transform data into their liking. This post is about a small wrapper over jp CLI utility that you can use while working on JSON files in Emacs.

Installing JP

Linux

On Linux, you can install the utility by first downloding the binary and then installing it.

> sudo wget https://github.com/jmespath/jp/releases/latest/download/jp-linux-amd64 \
>   -O /usr/local/bin/jp  && sudo chmod +x /usr/local/bin/jp

Mac

On Mac, you can install Jmespath CLI using brew.

> brew install jmespath/jmespath/jp

Adding Jmespath recipe in Emacs

This is my first recipe that I published on MELPA.Here are the steps to install it in Doom using straight. You can use any package manager to install it using MELPA repository.

Doom Emacs

  1. On Doom, you can just mention the below recipe in your package.el.
> (package! jmespath)
  1. Also, add the below line in your config.el.
> (use-package jmespath)

Using Jmespath

There is an interactive function, jmespath-query-and-show that you can use to query the currently opened buffer or a file. To use it with current buffer, you can simply call this function and enter your query. The output will be shown on a new buffer with name JMESPath Result.

To use this with a different file, you can set the Universal Argument using C-u or SPC-u (using evil). Then you can enter the file to execute the query on.

Read more

To learn more about JMESPath, visit Jamespath page.