I would like to print {score: score_value, id: id_value}
, by using jq
from the following json script (only a small part is here for introduction), I tried jq '[score: .hits.hits[]._score, id: .hits.hits[]._id]'
and also jq '{.hits.hits[]._score, .hits.hits[]._id}'
, but both have compile error.
{
"took" : 17,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 100,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "ff0",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"server_id" : 86,
"@timestamp" : "2023-03-01T11:09:23.474948161Z",
3
Answers
To get a stream of elements (i.e. multiple JSON documents):
Output:
Or to get a single JSON array:
Output:
A solution using with_entries:
though the _score and _id keys may not appear in the order you want (eg you may want the _score at the top of the JSON). If this doesn’t matter, this is another possible solution.
You swapped the symbols for arrays (
[...]
) and objects ({...}
).As in any other language, a JavaScript array is a collection of items (presumably of the same type but this is not required), listed in a certain order. In the JavaScript source code or as JSON they are separated by commas and enclosed in square brackets (
[
and]
). They do not have names/labels and are identified by their index (starting with0
).jq '.hits.hits'
extracts from the input JSON an array (of objects); the JSON fragment that you posted in the question shows only a fragment of the first item of the array.A JavaScript object is a collection of properties that have values. The order of the properties does not matter. In JavaScript code (or in JSON, which is a valid fragment of JavaScript code), an object is represented as pairs of (property, value) joined by
:
. The pairs are joined by commas (,
) and all the pairs are wrapped in curly braces ({
and}
). The JSON in the question encodes an object having propertiestook
,timed_out
and so one.In order to be correct, your attempts should be like:
or:
They probably do not extract what you want but at least they are correct and extract some data.
In order to extract the data you need, you can try something like this:
How it works
Note that
[]
(or| .[]
) splits an input array into items and feeds the next filter multiple input objects. They are processed individually by the next filter and collected back into an array by the wrapping square brackets ([...]
).An alternative way to get the same result is to use the
map()
filter. It runs the filter passed as argument against each item of the input array and collects the results into a new array.Technically it is the same as:
Check these solutions online.