fzf - Integrate with Jq for JSON Search

07/16/2023, Sun
Categories: #fzf
Tags: #cli-tools

Fzf Search JSON Data

JSON is a ubiquitous data format and searching for content in JSON will come up often enough to warrant a real-time search functionality. jq can be used to query json and fzf can be combined with this tool to fulfill our JSON search capabilities.

Start fzf with jq

echo '' | fzf --preview "jq {q} < us-data.json.json"

The us-data.json data is pulled from https://datausa.io/api/data?drilldowns=Nation&measures=Population

{
    "data": [
        {
            "ID Nation": "01000US",
            "Nation": "United States",
            "ID Year": 2020,
            "Year": "2020",
            "Population": 326569308,
            "Slug Nation": "united-states"
        },
        {
            "ID Nation": "01000US",
            "Nation": "United States",
            "ID Year": 2019,
            "Year": "2019",
            "Population": 324697795,
            "Slug Nation": "united-states"
        },
        {
            "ID Nation": "01000US",
            "Nation": "United States",
            "ID Year": 2018,
            "Year": "2018",
            "Population": 322903030,
            "Slug Nation": "united-states"
        },
        {
            "ID Nation": "01000US",
            "Nation": "United States",
            "ID Year": 2017,
            "Year": "2017",
            "Population": 321004407,
            "Slug Nation": "united-states"
        },
        {
            "ID Nation": "01000US",
            "Nation": "United States",
            "ID Year": 2016,
            "Year": "2016",
            "Population": 318558162,
            "Slug Nation": "united-states"
        },
        {
            "ID Nation": "01000US",
            "Nation": "United States",
            "ID Year": 2015,
            "Year": "2015",
            "Population": 316515021,
            "Slug Nation": "united-states"
        },
        {
            "ID Nation": "01000US",
            "Nation": "United States",
            "ID Year": 2014,
            "Year": "2014",
            "Population": 314107084,
            "Slug Nation": "united-states"
        },
        {
            "ID Nation": "01000US",
            "Nation": "United States",
            "ID Year": 2013,
            "Year": "2013",
            "Population": 311536594,
            "Slug Nation": "united-states"
        }
    ],
    "source": [
        {
            "measures": [
                "Population"
            ],
            "annotations": {
                "source_name": "Census Bureau",
                "source_description": "The American Community Survey (ACS) is conducted by the US Census and sent to a portion of the population every year.",
                "dataset_name": "ACS 5-year Estimate",
                "dataset_link": "http://www.census.gov/programs-surveys/acs/",
                "table_id": "B01003",
                "topic": "Diversity",
                "subtopic": "Demographics"
            },
            "name": "acs_yg_total_population_5",
            "substitutions": []
        }
    ]
}

Here we have the search for 'ID Year'

.data[]."ID Year"

This will yield all the values of the "ID Year" keys

2020
2019
2018
2017
2016
2015
2014
2013

Now to get the reverse, by using the value to find its parent object, we will use the following

.data[] | paths as $p | select(getpath($p) == "2013")

will provide this result

{
    "ID Nation": "01000US",
    "Nation": "United States",
    "ID Year": 2013,
    "Year": "2013",
    "Population": 311536594,
    "Slug Nation": "united-states"
}