0

I would like to call another command inside of jq query.

For example,

{
   hostA: "somedomain.com",
   hostB: "anotherdomain.com"
}

I need to transform to:

{
   hostA: "123.123.123.123",
   hostB: "132.132.132.132"
}

Applying this command on every value: dig +short $value

So, each host is transformed to IP.

Or, bash, iterate over result, reconstruct json only?

If so, what is the easiest/cleanest solution? I see it as I have to recreate second, mapping json and apply solutions from jq, lookup 1 or jq, lookup 2. Still, looks like a brute force solution.

Vetal
  • 275
  • 1
  • 3
  • 13

1 Answers1

1

You can use bash arrays and iterate over it in a loop:

Took the answer from - https://stackoverflow.com/a/67638584/16822178

# read each item in the JSON array to an item in the Bash array
readarray -t host_array < <(jq -c '.[]' input.json)

# iterate through the Bash array
for host in "${host_array[@]}"; do
  dig +short "$host"
done
nir
  • 46
  • 2
  • Thank you, question was about possibility to call something inline. Which I was not sure if it is possible. Looks like it is not, as [inian](https://stackoverflow.com/users/5291015/inian) mentioned.
    What you are proposing is this "brute-force solution" I've mentioned. So, to reassemble JSON back with bash manually
    – Vetal Jul 05 '22 at 00:16