1

I am trying to strip a substring from an array of strings; this array is obtained as a key set from a map. Some example incoming data:

{
  "appOneVersion": "1.0",
  "appTwoVersion": "1.0",
  "appThreeVersion": "1.0"
}

First I'm extracting the key set and making lower case which works:

 $ jq -r 'with_entries( .key |= ascii_downcase) | keys' example.json
[
  "apponeversion",
  "appthreeversion",
  "apptwoversion"
]

and then I'm attempting to strip the version part of each element with sub("version$"; "") however I'm not quite understanding how to make it iterate over the array instead of sub trying to operate on the array as a whole:

$ jq -r 'with_entries( .key |= ascii_downcase) | keys | sub("version$"; "")' example.json
jq: error (at example.json:5): array (["apponever...) cannot be matched, as it is not a string
1
  • I am trying to get all the keys; @peak below gave some options that work for me. Commented Aug 18, 2021 at 2:12

1 Answer 1

1

It looks like map would do the trick, and you might also consider keys_unsorted:

with_entries( .key |= ascii_downcase )
| keys_unsorted | map(sub("version$"; ""))

Of course, there are other approaches....

If you wanted to retain the original structure:

with_entries( .key |= (ascii_downcase | sub("version$";"")))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the original structure is not important so either will be fine. As with jq there probably are other approaches but this looks good for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.