2
\$\begingroup\$

I have an almost-json document in a file named "versions.json" like this:

{
  "package_a": {
    "sha_darwin": "6d94f041d06670977b2e5604b827f7e8bd46a1d200d14545ba75c17314ec12ad",
    "sha_linux": "6daa0fae252dc32e0af6ad89119555b277456349c55dfc9f276ee83718c6a9e2",
    "url": "https://github.com/someone/package_a/releases/download/v\(.version)/package_a_\(.version)_\(.platform)_x86_64.tar.gz",
    "version": "1.2.3"
  }, "package_b": {
    "sha_darwin": "6d94f041d06670977b2e5604b827f7e8bd46a1d200d14545ba75c17314ec12ad",
    "sha_linux": "6daa0fae252dc32e0af6ad89119555b277456349c55dfc9f276ee83718c6a9e2",
    "url": "https://storage.googleapis.com/package_b/release/v\(.version)/package_b_\(.version)_\(.platform)/amd64/package_b",
    "version": "6.3.0"
  }
}

It's input from a third party and I can't change it. What I need to do is interpolate the platform and version into the url. The problem is that that version value comes from the version in the file itself. I can't quite get it in the raw file because it's not actually valid json.

jq -r . versions.json
parse error: Invalid escape at line 5, column 129

I can get this in two passes:

echo '{"platform":"darwin","version":""}' |
  jq -f versions.json |
  jq -r '{version:.package_a.version,platform:"darwin"}' |
  jq -f versions.json |
  jq -r '"curl -sSL \(.package_a.url | @sh)"' |
  sh

But I'm not happy with the number of pipes and invocations of jq here, and looking to improve this all around. I would prefer a solution that involves a single invocation of jq, if that can be done.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.