JQ – build JSON at bash script

From time to time there is requirement build JSON object at bash script. This can be done in several ways. Depend on complexity of requirement.

As first attemption came up to write exact JSON to variable, something like this

#!/usr/bin/env bash

JS…


This content originally appeared on DEV Community and was authored by Peter Meszaros

From time to time there is requirement build JSON object at bash script. This can be done in several ways. Depend on complexity of requirement.

As first attemption came up to write exact JSON to variable, something like this

#!/usr/bin/env bash

JSON=$(cat <<-END
  {
    "what":"something",
    "when":"now"
  }
END
)

echo "$JSON"

As result this JSON is produced

{
  "what": "something",
  "when": "now"
}

Next attempt is add key values from variables

#!/usr/bin/env bash

WHAT="something"
WHEN="now"

JSON=$(jq -n \
  --arg what "$WHAT" \
  --arg when "$WHEN" \
  '$ARGS.named'
)

echo "$JSON"

Here is used jq for JSON built.

--arg name value passes values predefined variables. Value is available as $name.

All named arguments are also available as $ARGS.named Because the format of $ARGS.named is already an object, jq can output it as is.

This is usable when JSON structure is fixed and known. But JSON can be variable, depend on usage. What if there some variable missing or next variable has to be added. Some more flexibility can be welcomed.

Here is approach how to create JSON progressively

#!/usr/bin/env bash

JSON=$(jq -n '')

WHAT="something"

if [[ -n "$WHAT" ]]; then 
  JSON=$(echo $JSON | jq --arg what "${WHAT}" '. += $ARGS.named')
fi

if [[ -n "$WHEN" ]]; then 
  JSON=$(echo $JSON | jq --arg what "${WHAT}" '. += $ARGS.named')
fi

echo "$JSON"

At beginning, with JSON=$(jq -n '') empty JSON is created.

Then variables are assigned.

Next condition is crucial for JSON built.

if [[ -n "$WHAT" ]]; then 
  JSON=$(echo $JSON | jq --arg what "${WHAT}" '. += $ARGS.named')
fi

If variable is set and not null, will be added to JSON.

With this technique, JSON creation in bash script can be so flexible.


This content originally appeared on DEV Community and was authored by Peter Meszaros


Print Share Comment Cite Upload Translate Updates
APA

Peter Meszaros | Sciencx (2023-05-16T14:10:53+00:00) JQ – build JSON at bash script. Retrieved from https://www.scien.cx/2023/05/16/jq-build-json-at-bash-script/

MLA
" » JQ – build JSON at bash script." Peter Meszaros | Sciencx - Tuesday May 16, 2023, https://www.scien.cx/2023/05/16/jq-build-json-at-bash-script/
HARVARD
Peter Meszaros | Sciencx Tuesday May 16, 2023 » JQ – build JSON at bash script., viewed ,<https://www.scien.cx/2023/05/16/jq-build-json-at-bash-script/>
VANCOUVER
Peter Meszaros | Sciencx - » JQ – build JSON at bash script. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/05/16/jq-build-json-at-bash-script/
CHICAGO
" » JQ – build JSON at bash script." Peter Meszaros | Sciencx - Accessed . https://www.scien.cx/2023/05/16/jq-build-json-at-bash-script/
IEEE
" » JQ – build JSON at bash script." Peter Meszaros | Sciencx [Online]. Available: https://www.scien.cx/2023/05/16/jq-build-json-at-bash-script/. [Accessed: ]
rf:citation
» JQ – build JSON at bash script | Peter Meszaros | Sciencx | https://www.scien.cx/2023/05/16/jq-build-json-at-bash-script/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.