UberHans UberHans - 20 days ago 5
JSON Question

Iteratively posting via cURL set of Json records in bash

I have set of Json records in file. I would like to post iteratively.
For example file "jsondata.dat" contains following json records

{
"json_records":[
{
"name": "name_1",
"mark": "mark_1"
},
{
"name": "name_2",
"mark": "mark_2"
}
]
}


I would like to post each json record i.e.

{
"name": "name_X",
"mark": "mark_X"
}


in separate cURL post. How to achieve this in bash?

Answer

I would use jq to pipe each record as a single line, then read that output with a while loop to pass to curl.

jq -c '.json_records[]' | while IFS= read -r rec; do
  curl -X POST -d"$rec" "$URL"
done
Comments