batch curl -d from json file

I have a json file and I would like to pass each object to a curl -d command :

[
{
    "number":"+336770002979",
    "message":"La plupart\ntest",
    "sender":"BEcompany",
    "date": 1539286620000
},
{
    "number":"+336600000780",
    "message":"La plupart\ntest",
    "sender":"BEcompany",
    "date": 1539286620000
},
...
]

for now I tried this

curl -X POST \
-H "X-Primotexto-ApiKey: 784155eed9d0a4d1ffdb67466" \
-H "Content-Type: application/json" \
-d @json.json \
https://api.primotexto.com/v2/notification/messages/send;

but it only reads the first object.

EDIT I fixed the [...] in the json and the curl command thanks to your comments.

1 answer

  • answered 2018-10-12 11:22 tripleee

    You can use jq -c .[] to split the file to one array element per line, and take it from there.

    jq -c .[] json.json |
    while IFS= read -r fragment; do
        curl -options -etc -d "$fragment" "http://url"
    done