I am new to jq and not sure how to go about extracting just the filenames from the below JSON file.
{
"files" : [ {
"name" : "filename1.gz",
"StartDate" : "2018-07-09T11:00:00-04:00",
"EndDate" : "2018-07-09T12:00:00-04:00"
}, {
"name" : "filename2.gz",
"StartDate" : "2018-07-09T10:00:00-04:00",
"EndDate" : "2018-07-09T11:00:00-04:00"
}, {
"name" : "filename3.gz",
"StartDate" : "2018-07-09T09:00:00-04:00",
"EndDate" : "2018-07-09T10:00:00-04:00"
}, {
"name" : "filename4.gz",
"StartDate" : "2018-07-09T07:00:00-04:00",
"EndDate" : "2018-07-09T08:00:00-04:00"
} ]
}
expected output:
[filename1.gz,filename2.gz,filename3.gz,filename4.gz]
I see #Abdou has already given solution to this, just adding in another minified version to achieve the desired solution.
jq --compact-output '[.files[].name]'
Related
I have this:
{
"service" : {
"category" : "managed-object",
"resource" : "attribute",
"action" : "delete",
"options" : {
"uuid" : "#VALUE",
"attributes" : {
"name" : {
"value" : "#VALUE"
},
"contactInfo" : "",
"activationDate" : "",
"deactivationDate" : "",
"protectStopDate" : "",
"processStartDate" : ""
}
}
}
}
I need this:
{
"service" : {
"category" : "managed-object",
"resource" : "attribute",
"action" : "delete",
"options" : {
"uuid" : "#VALUE",
"attributes" : {
"name" : {
"value" : "#VALUE"
}
}
}
}
}
Previously I had a similar problem that was a little bit more complicated, and I received this amazingly simple answer from someone here:
.service.options |= (del(.max, .objectGroupMember) | .attributes|={name})
That jq command works even here, but of course (.max, .objectGroupMember) does not make sense because it does not exist.
How can I achieve my desired result?
A different take on the problem:
walk(if type=="object" then with_entries(select(.value!="")) else . end)
I found it in Weeble's answer to my earlier question:
'.service.options.attributes |= {name}'
how can I have transformed my json
{
"clients": [
{
"id" : "qwerty",
"accounts" : [{"number" : "6666"}, {"number" : "7777"}]
},
{
"id" : "zxcvb",
"accounts" : [{"number" : "1111"}, {"number" : "2222"}]
}
]
}
into following type of json? using JQ
{
"items": [
{
"id" : "qwerty",
"number" : "6666"
},{
"id" : "qwerty",
"number" : "7777"
},{
"id" : "zxcvb",
"number" : "1111"
},{
"id" : "zxcvb",
"number" : "2222"
}]
}
What kind of tools from JQ can help me? I can't choose any possible way to do it
Something like this should do the trick:
{items: [.clients[] | {id} + .accounts[]]}
Online demo
I would like to map the following structure
{
"id" : "OUTER_ID",
"name" : "OUTER_NAME"
"items" : [
{
"id" : "INNER_ID_1",
"name" : "INNER_NAME_1",
},
{
"id" : "INNER_ID_2",
"name" : "INNER_NAME_2",
}
]
}
into this
{
"payload": [
{
"key" : "INNER_NAME_1_KEY",
"data" : {
"id" : "OUTER_ID",
"name" : "OUTER_NAME",
"items" : [
{
"id" : "INNER_ID_1",
"name" : "INNER_NAME_1"
}
]
}
},
{
"key" : "INNER_NAME_2_KEY",
"data" : {
"id" : "OUTER_ID",
"name" : "OUTER_NAME",
"items" : [
{
"id" : "INNER_ID_2",
"name" : "INNER_NAME_2"
}
]
}
}
]
}
So, for each item in the initial items array, I want to create an entry in the output's payload, i.e I want to map items[i] to payload[i].data.items while also creating the payload, key and data keys in the output, and setting payload[i].data.id and payload[i].data.name to the input's outer id and name.
Can this be done with jq?
Sure, you can use the following filter :
.id as $id | .name as $name | {payload : [ .items[] | {key:.id, data:{id:$id, name: $name, items:[.]}} ] }
You can try it here.
I am trying to find a value in the json file and based on that I need to get the entire json data instead of that particular block.
Here is my sample json
[{
"name" : "Redirect to Website 1",
"behaviors" : [ {
"name" : "redirect",
"options" : {
"mobileDefaultChoice" : "DEFAULT",
"destinationProtocol" : "HTTPS",
"destinationHostname" : "SAME_AS_REQUEST",
"responseCode" : 302
}
} ],
"criteria" : [ {
"name" : "requestProtocol",
"options" : {
"value" : "HTTP"
}
} ],
"criteriaMustSatisfy" : "all"
},
{
"name" : "Redirect to Website 2",
"behaviors" : [ {
"name" : "redirect",
"options" : {
"mobileDefaultChoice" : "DEFAULT",
"destinationProtocol" : "HTTPS",
"destinationHostname" : "SAME_AS_REQUEST",
"responseCode" : 301
}
} ],
"criteria" : [ {
"name" : "contentType",
"options" : {
"matchOperator" : "IS_ONE_OF",
"values" : [ "text/html*", "text/css*", "application/x-javascript*" ],
}
} ],
"criteriaMustSatisfy" : "all"
}]
I am trying to match for "name" : "redirect" inside each behaviors array and if it matches then I need the entire block including the "criteria" section, as you can see its under same block {}
I managed to find the values using select methods but not able to get the parent section.
https://jqplay.org/s/BWJwVdO3Zv
Any help is much appreciated!
To avoid unwanted duplication:
.[]
| first(select(.behaviors[].name == "redirect"))
Equivalently:
.[]
| select(any(.behaviors[]; .name == "redirect"))
You can try this jq command:
<file jq 'select(.[].behaviors[].name=="redirect")'
I need to Intersect my Query Result. After intersection the data I want to use it in my page.
This is my Data : (I store the data in variable "hasilget")
[
{
"_id" : "2017-05-22",
"kamar" : [
[
{
"_id" : ObjectId("570f5095c8dbf1045d7fe9b3"),
"namkam" : "VIP ONE",
"idtipe" : ObjectId("57023e5e36b35501f17ea5c6")
}
],
[
{
"_id" : ObjectId("570f509dc8dbf1035d7fe9b5"),
"namkam" : "VIP TWO",
"idtipe" : ObjectId("57023e5e36b37601f17ea5c6")
}
]
]
},
{
"_id" : "2017-05-23",
"kamar" : [
[
{
"_id" : ObjectId("570f5095c8dbf1045d7fe9b3"),
"namkam" : "VIP ONE",
"idtipe" : ObjectId("57023e5e36b35501f17ea5c6")
}
]
]
}
]
I have try Use "Array-Intersection" from NPM.
var inter = intersection(hasilget);
console.log("Hasil "+JSON.stringify(inter));
But The Problem is the output is Still Originally Result. The Intersection Function is not working.
Please guide me.