Adding Variable to JSON CURL - json

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?

Related

Can you print the values of a JSON array in an AWS, using a JMESPath query?

I am trying to list all the SQS queues in our account using aws sqs list-queues into a bash one-item-per-line output.
Problem is it comes through as a JSON object
{
"QueueUrls": [
"url",
"url",
"etc..."
]
}
I can use JMESpath to get this
aws sqs list-queues --query 'QueueUrls[]'
but that gives me:
[
"url",
"url",
"etc..."
]
Question
Is it possible to print the output from a JSON array, using a JMESPath query in a one-per-line style?
From:
{
"key": [
"string",
"string",
"string"
]
}
To:
string
string
string
What I want
I want:
url
url
url
What I've tried
Using aws sqs list-queues --query 'QueueUrls[]' --output text
I get the urls all in one line:
url url url etc...
I've also looked at this other question but nothing I tried to put into the query gave me the flat-output
Work around
Currently I'm doing aws sqs list-queues --output text | sed -E 's/^.+\s+(.+)$/\1/' to extract the url line from the text output.
(Definitely also posible with awk but the sed regex was the thing my mind did first)
Why I want it
I want to use the output in a bash script I'm writing. I want to grep the lines and do some other processing on the output.
In this case it was simple to do the --output text trick but for more complicated AWS objects I'd like to know if it is possible using JMESPath.
You can achieve this by joining the elements of the JSON array with the join bulletin function.
You have two possible and equivalent syntaxes:
Chaining the function on the array, and using the current node — #
QueueUrls.join(`\n`, #)
Wrapping the whole array in the function
join(`\n`, QueueUrls)
Note: backticks — `, in JMESPath is the delimiter for a literal expression.
So, your AWS command ends up being:
aws sqs list-queues --query 'QueueUrls.join(`\n`, #)' --output text

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.

Range query in ElasticSearch (GET without body)

So very basic question about elasticsearch which the docs not answer very clearly (because they seem to go into many advanced details but miss the basic ones!).
Example: range query
http://www.elasticsearch.org/guide/reference/query-dsl/range-query.html
Doesn't tell how to PERFORM the range, is it via the search endpoint?
And if it is, then how to do it via querystring? I mean, I want to do a GET, not a POST (because it's a query, not an insertion/modification). However the documention for GET requests doesn't tell how to use JSON like in the Range sample:
http://www.elasticsearch.org/guide/reference/api/search/uri-request.html
What am I missing?
Thanks
Use the Lucene query syntax:
curl -X GET 'http://localhost:9200/my_index/_search?q=my_field:[0+TO+25]&pretty'
Let's assume we have an index
curl -XPUT localhost:9200/test
And some documents
curl -XPUT localhost:9200/test/range/1 -d '{"age": 9}'
curl -XPUT localhost:9200/test/range/2 -d '{"age": 12}'
curl -XPUT localhost:9200/test/range/3 -d '{"age": 16}'
Now we can query these documents within a certain range via
curl -XGET 'http://localhost:9200/test/range/_search?pretty=true' -d '
{
"query" : {
"range" : {
"age" : {
"from" : "10",
"to" : "20",
"include_lower" : true,
"include_upper": true
}
}
}
}
'
This will return the documents 2 and 3.
I'm not sure if there is a way to perform these kind of complex queries via URI request, though.
Edit: Thanks to karmi here is the solution without JSON request:
curl -XGET --globoff 'localhost:9200/test/range/_search?q=age:["10"+TO+"20"]&pretty=true'
Replying to myself thanks to #javanna:
In the RequestBody section of the Search docs:
http://www.elasticsearch.org/guide/reference/api/search/request-body.html
At the end, it says:
The rest of the search request should be passed within the body itself. The body content can also be passed as a REST parameter named source.
So I guess that I need to use the search endpoint with the source attribute to pass json.

ElasticSearch, specific field not returning

I'm stuck with a little elasticsearch problem. I'm new to elasticsearch and don't know why this doesn't work.
curl -XPOST 'http://myhost.nl:9200/my_index/test/_search?pretty=true' -d '{ "fields": ["message"] }'
I don't get any field back. The field "message" does exist and realy looks like the example on the elasticsearch site. http://www.elasticsearch.org/guide/reference/api/search/fields.html
Can anybody see what I'm missing?
Your query would have worked if this field was stored. But since it's not stored and only available as part of source, you need to specify full path to it. Try:
curl -XPOST 'http://myhost.nl:9200/my_index/test/_search?pretty=true' -d '{ "fields": ["tweet.message"] }'