DEV Community

Yuya Takeyama
Yuya Takeyama

Posted on

3 1

Representation of a Data Structure Like an Array of Hash Tables in Bash Using jq

Have you ever needed a Hash Table in Bash?
I needed it when I was writing a deploy script.

I found that Bash 4 has a data structure called Dictionary.
https://stackoverflow.com/questions/1494178/how-to-define-hash-tables-in-bash

But the version of Bash in my laptop is 3.2.57 and I couldn't use it.
Of course, it's easy to update the Bash.
But we may need to work on environments with an older version of it.
So I thought up about the other way.

How to

#!/bin/bash -eu

# A data structure like an array of hash tables
# Strictly, it's a stream of JSONs
PARAMETERS=$(cat <<_EOT_
{
  "foo":"FOO 1",
  "bar":"BAR 1"
}
{"foo":"FOO 2","bar":"BAR 2"}
{
  "foo": "フー",
  "bar": "\u30d0\u30fc"
}
_EOT_
)

# Transform into a line-delimited JSONs
PARAMETERS=$(echo "${PARAMETERS}" | jq . --monochrome-output --compact-output)

# Enumerate JSONs and output foo and bar of each item
while IFS='' read -r param || [[ -n "${param}" ]]; do
  foo=$(echo "${param}" | jq .foo -r)
  bar=$(echo "${param}" | jq .bar -r)

  echo "foo=${foo}"
  echo "bar=${bar}"

  echo "---"
done < <(echo "${PARAMETERS}")
Enter fullscreen mode Exit fullscreen mode
$ bash json_stream.sh
foo=FOO 1
bar=BAR 1
---
foo=FOO 2
bar=BAR 2
---
foo=フー
bar=バー
---
Enter fullscreen mode Exit fullscreen mode
  • Build a JSON as a string using heredoc
  • Transform the JSON into a line-delimited JSONs
  • Read a JSON from each line
  • Retrieve each property like jq .foo in the loop

Hope you enjoyed it!

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

If this post resonated with you, feel free to hit ❤️ or leave a quick comment to share your thoughts!

Okay