Home > OS >  How to get json array from file, format with jq and curl a post request with ddata from json
How to get json array from file, format with jq and curl a post request with ddata from json

Time:02-19

I'm trying to get an array from a json file.

{
    "Requests": [
        {
            "Item1": "2020-01-27 16:24:49",
            "Item2": "203i1Kj2gTEQgfdsfds23",
            "Item3": 1603,
            "Item4": "generic"
        },
        {
            "Item1": "2020-01-27 16:24:49",
            "Item2": "203i1Kj2gTEQgfdsfds23",
            "Item3": 1603,
            "Item4": "generic"
        },
        {
            "Item1": "2020-01-27 16:24:49",
            "Item2": "203i1Kj2gTEQgfdsfds23",
            "Item3": 1603,
            "Item4": "generic"
        },
        {
            "Item1": "2020-01-27 16:24:49",
            "Item2": "203i1Kj2gTEQgfdsfds23",
            "Item3": 1603,
            "Item4": "generic"
        }
    ]
}

Then I want to pass each of these items to a curl request and possibly handle that in parralel with xargs

I'm still stumbling on getting one item to the curl endpoint.

cat CurlArgsFile.json | jq -c '.[][0]' | xargs -I % curl -d % -H "Content-Type: application/json" -X POST http://localhost:53391/mySVCS/writeData

I'm running this on Git Bash on Windows 10. When I try to echo the json output into a file I get the below without the quotes and now I'm not sure if that's the correct json I'm sending at all.

{Item1:2020-01-27 16:24:49,Item2:203i1Kj2gTEQgfdsfds23,Item3:1603,Item4:generic}

How do I send the first item from the json to the endpoint and the enpoint recognizes it?

CodePudding user response:

This is the end result. The issue was I was not familiar with the jq utility and my json kept coming in wrong.

cat CurlArgsFile.json | jq -r '.Requests[]|tojson' | xargs -0 -I % curl -d % -H "Content-Type: application/json" -X POST http://localhost:53391/mySVC/writeData
  • Related