Search code examples
bashshellcron

add prefix in shell script to json resonse


I want to add some prefix to the field from output of hitting endpoint.

file.sh

result=$(curl -s <someEndpoint>)
echo $result

I have a cronjob to hit that endpoint regularly;

* * * * * /root/file.sh | jq .field > /root/file.txt 2>&1

I need to add "start" to the .field. I created another file2.sh:

prefix="start"
val=$(cat /root/file.txt)
concat="${prefix}${val}"
echo $concat

the output looks like: start"value from file.txt"

I want "start value from file.txt". how can I do that?


Solution

  • It seems like the better solution would be to use jq -r .field to output plaintext instead of a JSON string, but since you explicitly want to work with json, you can use jq again:

    $ echo '"string"' | jq '"start" + .'
    "startstring"