I have a file which is formatted like this:
{"cat":["bat","mat","hat"]}
{"dog":["frog","clog"]}
{"mouse":["house"]}
..and so on and so forth with many more (~50 more) lines like this.
I want to get the first word in each line (e.g. cat) and then run a curl on a URL with that word (e.g. curl https://urlHere='"${animalVariableHere}") so the first word is from each line should be put into the URL where is says "${animalVariableHere}". Once curl is run on that URL, I then want to get that output (curling the URL gives a single line in JSON - e.g. {"type":"animal,"info":{"house pet","furry","brown"}}), particularly just the "info" part, into a new file that similar to this:
"cat": "house pet","furry","brown"
so as a result, the final output using the 3 input lines above will be:
"cat": "house pet","furry","tabby"
"dog": "house pet","furry","golden retriever"
"mouse": "house pet","tail","pygmy"
So far I've tried to see whether I can use the 'awk' command to get the first word within each line, but it hasn't worked at all/ I'm not entirely sure how as I'm just learning to shell script. But once with each word, I was just thinking of piping it, i.e:
<first word> | curl 'https://urlHere'"${animalVariableHere}" | jq -r '.info' | tee -a outputFile
I also wasn't sure how to match the animal variable to the output of info. Any help would be appreciated, thank you
CodePudding user response:
You'll need a tool that can parse JSON.
example with jq
#!/bin/bash
while IFS='' read -r animal
do
curl "https://urlHere=$animal" |
jq --arg a "$animal" '$a ": " (.info | join(", "))'
done < <(jq -r 'keys[0]' file.json) > output.json
CodePudding user response:
Using jq
, keys_unsorted[0]
will give you the first key, and --arg
lets you import values from outside. Using bash
, you could loop linewise using while
.
jq -r 'keys_unsorted[0]' input.json | while read -r animalVariableHere
do curl 'https://urlHere'"${animalVariableHere}" \
| jq --arg id "${animalVariableHere}" -r '@csv "\([$id]): \(.info)"'
done > output.txt
This assumes, the curl
output is valid JSON, i.e. it looks more like {"type":"animal","info":["house pet","furry","brown"]}
.