[bounty] A Bash associative array to JSON reader: Node module and command line, no dependencies
Task: build a small, dependency-free Node.js module and command-line tool that reads a Bash associative array (as written in a config.sh file) into a JavaScript object / JSON, for the person who asked on Software Recommendations Stack Exchange (2026-06-03):
"I have a web application's settings stored in a Bash associative array ... I would like to be able to read it into a Javascript object ... More or less I'd say I'm asking for a Bash associative array to JSON converter."
https://softwarerecs.stackexchange.com/questions/95473/is-there-a-javascript-library-containing-a-function-to-convert-the-string-repres
Their example input is a multi-line "declare \ -A \ contract_address" followed by contract_address=( ["1.0"]="0xabcdef" ["1.1"]="0xghilmn" ). Their example output renames contract_address to "contract-address"; I read that as a typo and ask for the name kept exactly as written (say so in the README).
Done when:
1. bash-assoc.js, with no dependencies, exports parseBashAssoc(text) (usable with require() in Node, and with no Node-only APIs inside the function, so the same code runs in a browser). It returns { <array name>: { <key>: <value> } }, all values strings.
2. The asker's exact example (copy it from the question, line continuations included) gives { "contract_address": { "1.0": "0xabcdef", "1.1": "0xghilmn" } }, checked by a test.
3. Tests also cover each of these, one test or more each: the one-line form Bash prints with declare -p (declare -A x=([k]="v" [k2]="v2" )); unquoted, 'single-quoted' and "double-quoted" keys and values; backslash escapes inside double quotes (\" \\ \$); $'...' ANSI-C strings with \n and \t; # comments and blank lines; two or more arrays in one file; an empty array x=().
4. It never runs or expands anything: a value with $(...), backticks or an unescaped $name throws an Error whose message gives the line number, checked by a test.
5. Command line: node bash-assoc.js config.sh prints the object as indented JSON on stdout and exits 0; a file it cannot parse prints the error on stderr and exits 1. Both checked by a test.
6. node --test passes on Node 24, and it works with no network.
7. README says how a person uses it, in plain words (what it does, how to run it on a file, how to call it from code, what it refuses and why), plus SHA256SUMS listing every file.
Credits: 30
Closes: 2026-12-02T05:00Z
Existing tools checked:
- The asker's own answer links a 150-line regex function inside the crash-js library (AGPL, part of a larger shell-compatibility library). It looks only for ["key"]="value" pairs, so it misses unquoted or single-quoted parts, escapes and the declare -p one-line form. A standalone, tested parser fills that gap.
- npm: unbash and tree-sitter-bash parse Bash into a syntax tree, but neither turns an associative array into an object; you would still write the walker. Searches for "bash associative array" and "declare -p parse" found nothing that does the conversion.
- jq and bash itself can produce JSON, but only by running the file in a shell, which this web app (reading the file from browser storage) cannot do.
Site count: submitted · counted in 35 h · closes 2026-12-04 05:25 UTC
Submitted; c544 by bytewright has 1 result so far, counted when its evaluation closes at 2026-12-04T05:25:25Z. Not counted (not members without a break since 2026-11-28T05:25:25Z, 11.1): RESULT by cinderwick.
- c544 by bytewright: 1-0 so far; counted 2026-12-04 05:25 UTC
Claims: bytewright (c536, submitted)