🎄 Unwrapping JQ: A Developer's Advent Gift for JSON Mastery 🌟

Dec 1, 2023

Back to 2023 Advent Calendar

We start this year's Advent Calendar of Tools with JQ, one of the most useful yet underutilized tools among engineers.

Mastering this tool can help you stand out from your peers in the company.

Top 3 Uses of JQ in Your Daily Developer Life

1. Pretty Printing JSON Data Locally

Stop pasting JSON into random websites for formatting.

By default, JQ pretty-prints the output. Applying the simplest identity operator . to any JSON input will return the data in a neatly formatted manner.

For example:

echo '{"firstName":"John"}' | jq .
  "firstName": "John"

On a Mac, you can use the following command to paste from the clipboard, format the data, and then copy it back to the clipboard:

pbpaste | jq . | pbcopy

2. Filtering JSON Logs

If your company uses JSON logs, JQ enables you to filter and find specific results. You can also share these results along with the 'recipe,' so others can replicate the process.

cat my-service.log | jq '.[] | select(.error_count>40) | .exception_message'
"Failed to obtain client certificate"

3. Declarative Data Transformation

Imagine you have various sources of information (such as third-party APIs or different backend APIs) and you need to standardize this data into a uniform format (like an internal model or a UI view model).

For example, if you are building a Facebook feed and receiving data from 'photo posts', 'group posts', 'marketplace shares', etc., which can be dynamically added, a solution is to create a dynamic registry where JQ expressions are registered and then applied by the server. This eliminates the need to write new code for each post type.

def to_feed_item(post):
    jq_expression = get_jq_expression(post.type)
    return jq.apply(jq_expression, post)