Reputation: 13
I'm working on an API connection, I am receiving JSON datasets which contain multiple objects and I'm trying to pass that through to the next system.
The JSON dataset has the following information (OUTPUT.JSON):
[
{
"initials": "V.",
"firstName": "Victor",
"lastNamePrefix": " ",
"lastName": "Rutherford",
"employerReferenceId": "0258741",
"jobDescription": "Rental"
},
{
"initials": "P.",
"firstName": "Pippa",
"lastNamePrefix": " ",
"lastName": "Lewis",
"employerReferenceId": "98765431",
"jobDescription": "Rental"
},
{
"initials": "S.",
"firstName": "Stephanie",
"lastNamePrefix": " ",
"lastName": "Reid",
"employerReferenceId": "123456789",
"jobDescription": "Rental"
}
]
The API post I'm trying to do has to be a separate Post for each object (person) in the JSON datafile. This because the post has to have the referencenumber of the EMPLOYERREFEREBCEID in the URL for each post, but the post also needs to only have the section of data that belongs to that EMPLOYERREFEREBCEID in the JSON file.
curl -X POST "https://website.eu/test-Company/api2/external-employees/[EMPLOYERREFEREBCEID]" -H "accept: application/json" -H "Authorization: Token {TOKEN}" -H "Content-Type: application/json" -d "@output.json"
I did found an old post that works with JQ and Bash, but since I am running Windows Server 2016 this is going to be challenging
for (( i = 0 ; i < ${#id[@]} ; i++ ))
do
POST REST API
done
Upvotes: 0
Views: 512
Reputation: 3423
You might be interested in the JSON-parser xidel, for which this is a relatively easy task.
No need for FOR-loops. One xidel
call could be enough.
xidel -s "OUTPUT.JSON" -e ^"^
for $x in $json() return^
x:request({^
'headers':(^
'accept: application/json',^
'Authorization: Token %TOKEN%',^
'Content-Type: application/json'^
),^
'post':serialize($x,{'method':'json'}),^
'url':'https://website.eu/test-Company/api2/external-employees/'^|^|$x/employerReferenceId^
})/json^
"
This is the cmd prettified version (with the necessary escape-characters).
Or alternatively the minified version:
xidel -s "OUTPUT.JSON" -e "for $x in $json() return x:request({'headers':('accept: application/json','Authorization: Token %TOKEN%','Content-Type: application/json'),'post':serialize($x,{'method':'json'}),'url':'https://website.eu/test-Company/api2/external-employees/'||$x/employerReferenceId})"
For each JSON-object (person) x:request()
sends a POST-request. As you can see, it accepts options as a JSON-object.
%TOKEN%
-variable is set beforehand){"initials":"V.","firstName":"Victor","lastNamePrefix":" ","lastName":"Rutherford","employerReferenceId":"0258741","jobDescription":"Rental"}
employerReferenceId
-attribute added. The url for the first JSON-object for instance: https://website.eu/test-Company/api2/external-employees/0258741
.x:request()
's output is also a JSON-object. If this url/api also returns JSON, then you can parse it by selecting the json
-attribute: x:request(...)/json
. If not, then x:request(...)/raw
.Upvotes: 1
Reputation: 13
This took me soo long but.. i have the first part working.
Solved!
for i in $(cat "c:\Test\output.json" | jq -r .[].employerReferenceId); do
After this part is start the Curl Command, wich also works fine till the data part, and thats where im stuck now.
curl -X POST "https://website.eu/test-Company/api2/external-employees/$i" -H "accept: application/json" -H "Authorization: Token {TOKEN}" -H "Content-Type: application/json" -d "EXAMPLE: $initials:value $firstName:value $lastNamePrefix:value $lastName:value $employerReferenceId:value $jobDescription:value"
Problem: how to reffer to the corresponding value's that belong to person in $i in the curl command data section.
Upvotes: 0