Note: The Listed platform will permanently shut down December 31, 2026. Your data published on Listed will still be available in your personal Standard Notes account. Learn more

jq examples

json to csv

DO

DON'T

use [] square brackets use {} curly braces
.[] | [.postId, .name] .[] | {postId, name}
arrays containing the values of the fields. we need to pre-pend the top row manually does not work
```json [ 1, "alias odio sit" ] [ 1, "vero eaque aliquid doloribus et culpa" ] ``` ```json { "postId": 1, "name": "alias odio sit" } { "postId": 1, "name": "vero eaque aliquid doloribus et culpa" } ```

Adding heading row

["one", "two"],(<your-jq>) | @csv

  • fields need to be in an array
  • added to the other arrays with a comma ,
  • putting the heading row next to the arrays (instead of before the whole lot) will create a sequence of heading and row.
    • .[] | ["one", "two"],<your-jq> | @csv
"one","two"
1,"alias odio sit"
"one","two"
1,"vero eaque aliquid doloribus et culpa"
"one","two"
2,"et fugit eligendi deleniti quidem qui sint nihil autem"

concatenate two arrays | select specific elements | generate a key:value object for each | merge these objects into a single object

Steps
  1. Concatenate two arrays
  • The Step: (.ingredients + ."optional ingredients")
  • The Goal: This uses the + operator to combine the main ingredients array and the optional ingredients array (wrapped in quotes because it contains a space) into one single, flat array containing all 11 items.
  1. Select specific elements from the resulting array
  • The Step: | map(select(.substitute))
  • The Goal: It filters through the concatenated list and extracts only the elements that possess a .substitute key ("buttermilk", "melted butter", and "blueberries").
  1. Generate a key:value object for each of these elements
  • The Step: | map({key: .item, value: .substitute})
  • The Goal: This reformats each selected ingredient object into the standard jq schema (key/value) required for building an object.
  • Intermediate Output:
    [
      { "key": "buttermilk", "value": "regular milk" },
      { "key": "melted butter", "value": "vegetable oil" },
      { "key": "blueberries", "value": "chopped apple" }
    ]
  1. Merge these objects into a single object
  • The Step: | from_entries
  • The Goal: This collapses the array of key/value pairs into one clean, consolidated JSON dictionary object.
output
{
  "buttermilk": "regular milk",
  "melted butter": "vegetable oil",
  "blueberries": "chopped apple"
}
Source json
{
  "name": "Ingredients for pancakes",
  "ingredients": [
    {
      "item": "flour",
      "amount": {
        "quantity": 1,
        "unit": "cup"
      }
    },
    {
      "item": "sugar",
      "amount": {
        "quantity": 0.25,
        "unit": "cup"
      }
    },
    {
      "item": "baking powder",
      "amount": {
        "quantity": 1,
        "unit": "teaspoon"
      }
    },
    {
      "item": "baking soda",
      "amount": {
        "quantity": 0.25,
        "unit": "teaspoon"
      }
    },
    {
      "item": "salt",
      "amount": {
        "quantity": 1,
        "unit": "pinch"
      }
    },
    {
      "item": "buttermilk",
      "amount": {
        "quantity": 1,
        "unit": "cup"
      },
      "substitute": "regular milk"
    },
    {
      "item": "eggs",
      "amount": {
        "quantity": 1,
        "unit": "egg"
      }
    },
    {
      "item": "melted butter",
      "amount": {
        "quantity": 1,
        "unit": "tablespoon"
      },
      "substitute": "vegetable oil"
    }
  ],
  "optional ingredients": [
    {
      "item": "cinnamon",
      "amount": {
        "quantity": 0.25,
        "unit": "teaspoon"
      }
    },
    {
      "item": "vanilla extract",
      "amount": {
        "quantity": 0.5,
        "unit": "teaspoon"
      }
    },
    {
      "item": "blueberries",
      "amount": {
        "quantity": 0.25,
        "unit": "cup"
      },
      "substitute": "chopped apple"
    }
  ]
}

More from dernorberto
All posts