Trying to send json array to my json server with curl - json

I need to identify what JSON values my server expects. I've been doing testing within Haskell, and all is well. So now I need to take the next step so I can document what these values look like for other people to use.
One of my REST methods expects an Array
Array (fromList [String "BNAP",Number 312])
is how Haskell expresses this.
Testing with cURL, I want to do something like this:
curl -D- -X POST -H "Content-Type: application/json" --data '["BNAP":312]'
http://10.64.16.6:3000/Read
But this seems to not be valid JSON
ParseError {errorContexts = ["]"], errorMessage = "Failed reading: satisfyWith", errorPosition = 1:8}
If Haskell was expecting an Object Object (fromList [String "BNAP",Number 312]) for example, the JSON would be expressed this way
--data '{"BNAP":312}'
So to me, it follows that my above attempt would be right. But, it's not. So, how does one express an Array with one Pair consisting of a String and a Number, with cURL?

The colon is not valid JSON here:
["BNAP":312]
^
Array elements should be separated by a comma:
["BNAP", 312]
If you wanted an array with a single key-value pair, you need to add curly braces:
[{"BNAP": 312}]

Related

When creating a variable from command output, Bash removes a backslash from the JSON. How do I make it keep both backslashes to maintain valid JSON?

I'm doing the following to capture some ADO JSON data:
iteration="$(az boards iteration team list --team Test --project Test --timeframe current)"
Normally, the output of that command contains a JSON key/value pair like the following:
"path": "Test\\Sprint1"
But after capturing the STDOUT into that iteration variable, if I do
echo "$iteration"
That key/value pair becomes
"path": "Test\Sprint1"
And if I attempt to use jq on that output, it breaks because it's not recognized as valid JSON any longer. I'm very unfamiliar with Bash. How can I get that JSON to remain valid all the way through?
As already commented by markp-fuso:
It looks like your echo command is interpreting the backslashes. You can confirm this by running echo 'a\\b' and looking at the output.
The portable way to deal with such problems is to use printf instead of echo:
printf %s\\n "$iteration"

Adding Variable to JSON CURL

I am trying to add a text string to a CURL request with JSON. See below for the $test variable. When I submit this, the application sees it as literal $test.
--data "{"fields": {"project": {"key": "Ticket"},"summary":"Account - Missing Tags","description":"The following AWS assets do not have the minimally required tags................ $test ","issuetype": {"name": "Service"}}}}"
I have tried various methods such as "'$Test'" and that hasn't worked either. Can you help explain how to accomplish this?

Extract json object from wget request

I want to do with php exactly the opposite of what James Nine was asking in PHP: How to extract JSON strings out of a string dump.
That is to keep the json object(s) out from a large output from a wget request. I can't figure out what the regex should be, basically negate
~\{(?:[^{}]|(?R))*\}~

How can I fix JSON error on shell script?

I have a bash script which sends curl post requests. I want to pass data as bash script parameters. However one of the parameter has spaces in the string and it fails with the error below.
Error parsing JSON data.\n\tString not terminated on line
In shell script, I'm sending an argument like this format {"name":"'$2'"}
Could you please help me to solve that issue?
Thanks
jq is good not only for manipulating existing JSON data, but creating new data, as it does things like correctly handling characters that can't appear unescaped in JSON strings, and proper quoting. Something like
curl ... -d"$(jq -n --arg val "$2" '{name: $val}')"
It would be better if you add enough data while asking question.
I assume your json will be like
{
"name": "argument passed"
}
curl -XPOST "your/url/here" -H 'Content-Type:application/json' -d'{"name":"'$1'"}'
Save the above command as post_request.sh(Feel free to change the name).
Run using below comand.
sh post_request.sh "argument passed"
"argument passed" will be your name with space.

curl command json response value without jq

my curl command returns a json response
{"token":"abcd"}
how do I get the token value into a variable in the shellscript?
i can not use jq something most posts have suggested in the past. The pattern of this response is also set (there will be only 1 key-value pair), so if this response can in anyway be converted to a string then using substring could be a option.
I found this blog, did exactly what I want. Works wonder.