UberHans UberHans - 1 year ago 62
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

"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 Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download