Skip to content

Add bashio::var.json_array() function#194

Merged
frenck merged 1 commit intohassio-addons:mainfrom
lmagyar:pr-add-var-json-array
Apr 10, 2026
Merged

Add bashio::var.json_array() function#194
frenck merged 1 commit intohassio-addons:mainfrom
lmagyar:pr-add-var-json-array

Conversation

@lmagyar
Copy link
Copy Markdown
Contributor

@lmagyar lmagyar commented Feb 2, 2026

Proposed Changes

Can be used like:

"$(bashio::var.json \
  foo "^$(bashio::var.json_array "bar" "baz")" \
)"

Related Issues

Summary by CodeRabbit

  • New Features
    • Added support for converting Bash arrays to JSON array format. The function processes array inputs and produces valid JSON output with comprehensive error handling and status codes to indicate operation success or failure.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 2, 2026

Walkthrough

Added a new public function bashio::var.json_array() to lib/var.sh that converts Bash arrays into JSON arrays using jq. The function handles success and failure cases with OK and NOK exit codes, following the existing json_string pattern.

Changes

Cohort / File(s) Summary
Bash Array to JSON Conversion
lib/var.sh
Added bashio::var.json_array() function to convert Bash arrays into JSON arrays using jq, with error logging and return code handling mirroring the json_string approach.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Arrays transform to JSON bright,
With jq magic, structured right,
A new helper hops into place,
Adding beauty to our space! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main change: adding a new bashio::var.json_array() function to the codebase.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 4, 2026

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. Thank you for your contributions.

@github-actions github-actions bot added the stale There has not been activity on this issue or PR for quite some time. label Mar 4, 2026
@lmagyar
Copy link
Copy Markdown
Contributor Author

lmagyar commented Mar 4, 2026

not stale

@github-actions github-actions bot removed the stale There has not been activity on this issue or PR for quite some time. label Mar 5, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 4, 2026

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. Thank you for your contributions.

@github-actions github-actions bot added the stale There has not been activity on this issue or PR for quite some time. label Apr 4, 2026
@lmagyar
Copy link
Copy Markdown
Contributor Author

lmagyar commented Apr 4, 2026

not stale

@github-actions github-actions bot removed the stale There has not been activity on this issue or PR for quite some time. label Apr 5, 2026
@frenck frenck added the new-feature New features or options. label Apr 7, 2026
@frenck frenck requested a review from Copilot April 7, 2026 13:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the Bashio variable utilities by adding a helper to serialize Bash positional array values into a JSON array string, intended for embedding into bashio::var.json payloads.

Changes:

  • Added bashio::var.json_array() to convert positional arguments into a JSON array using jq.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/var.sh
local json_array

# https://stackoverflow.com/a/67489301/2755656
if json_array=$(jq -cn '$ARGS.positional' --args -- "${array[@]}"); then
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The jq invocation appears to place the --args option after the filter ('$ARGS.positional'). jq option parsing is not guaranteed to accept options after the filter (notably on stricter getopt implementations), which can lead to --args being treated as an input filename and the command failing. Reorder the command so --args is parsed as an option (and ensure -- is only used to terminate option parsing for the positional values).

Suggested change
if json_array=$(jq -cn '$ARGS.positional' --args -- "${array[@]}"); then
if json_array=$(jq -cn --args '$ARGS.positional' -- "${array[@]}"); then

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @lmagyar 👍

../Frenck

                       

Blogging my personal ramblings at frenck.dev

@frenck frenck merged commit 2bd4dc0 into hassio-addons:main Apr 10, 2026
8 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2026
@lmagyar lmagyar deleted the pr-add-var-json-array branch April 13, 2026 09:04
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

new-feature New features or options.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants