Remove multiple entries from an array of objects using jq - jq

I have the following json and want to remove multiple entries from the ebooks array if they are not in the following array ["Pascal", "Python"] (will eventually be dynamic array, this is just for example)
{
"eBooks":[
{
"language":"Pascal",
"edition":"third"
},
{
"language":"Python",
"edition":"four"
},
{
"language":"SQL",
"edition":"second"
}
]
}
was hoping to do something like this, which if it worked would delete last one containing the SQL because it's not in the array, but this doesn't work
jq '.ebooks[] | select ( .language | in(["Pascal", "Python"]))' ebooks.json

You're almost there. Use del, IN and a capital B in eBooks :)
jq 'del(.eBooks[] | select(.language | IN("Pascal", "Python")))' ebooks.json
{
"eBooks": [
{
"language": "SQL",
"edition": "second"
}
]
}
Demo

Related

Adding key name to value using jq

I am trying to dynamically assign the key name as its value in my json
This is the json i am using:
{
"test1": "",
"test2": "",
"test3": ""
}
the result i would like to obtain looks like this:
{
"test1": "test1",
"test2": "test2",
"test3": "test3"
}
I am not familiar with jq and the closest result i got is using:
keys[] as $key | {"\($key)": "\($key)"} | .
here is the output:
{
"test1": "test1"
}
{
"test2": "test2"
}
{
"test3": "test3"
}
with_entries lets you manipulate .key and .value for each field. Just set one to the value of the other:
with_entries(.value = .key)
{
"test1": "test1",
"test2": "test2",
"test3": "test3"
}
Demo
Following your approach, you could collect your result objects into an array using the array constructors […] around your filter, and then add up the array's items producing one merged object. (Note that | . can be dropped as it doesn't do anything but reproduce itself, and that the string interpolation "\($key)" is just the same as $key, given $key is a string, which is the case here as object field names are always strings.)
[keys[] as $key | {($key): $key}] | add
Demo
You may also entirely drop the use of variables as there is no other context interfering:
[keys[] | {"\(.)": .}] | add
Demo
And there is a shortcut for patterns like [.[] | …] called map:
keys | map({"\(.)": .}) | add
Demo
Alternatively, you also might want to consider using reduce for an iterative manipulation, and/or keys_unsorted which acts like keys but produces the keys in the original (unsorted) order:
reduce keys_unsorted[] as $key (.; .[$key] = $key)
Demo

Need to compare array in Marklogic with xquery

I need to compare array in MarkLogic with Xquery .
Query parameters:
{
"list": {
"bookNo": 13,
"BookArray":[20,21,22,23,24,25]
}
}
Sample Data:
{
"no":01'
"arrayList"[20,25]
}
{
"no":02'
"arrayList"[20,27]
}
{
"no":03'
"arrayList"[20,23,25]
}
Output:
"no":01
"no":03
I need to return "no" where all values from arraylist should be match with bookArray.
Ok. You do not explain if the actual data is in the system or not. So I did an example as if it is all in memory.
I chose to keep the sample in the MarkLogic JSON representation which has some oddities like number-nodes and array-nodes under the hood. To make it more readable if you dig into it, i used fn:data() to get less verbose. In all reality, if this was an in-memory operation and I could not use Javascript, then I would have converted the JSON structures to maps.
Here is a sample to help you explore. I cleaned up the JSON to be valid and for my sample wrapped the three samples in a single array.
xquery version "1.0-ml";
let $param-as-json := xdmp:unquote('{
"list": {
"bookNo": 13,
"BookArray":[20,21,22,23,24,25]
}
}')
let $list-as-json := xdmp:unquote('[
{
"no":"01",
"arrayList":[20,25]
},
{
"no":"02",
"arrayList":[20,27]
},
{
"no":"03",
"arrayList":[20,23,25]
}
]')
let $my-list := fn:data($param-as-json//BookArray)
return for $item in $list-as-json/*
let $local-list := fn:data($item//arrayList)
let $intersection := fn:data($item//arrayList)[.=$my-list]
where fn:deep-equal($intersection, $local-list)
return $item/no
Result:
01
03

Add values to a JSON array if outer array's name == 'something'

I'm passing a JSON object to jq and want to add extra objects to an inner array ('accessories') if its parent array ('platforms') matches a certain name.
Here's my source JSON:
{
"bridge": {
"name": "Homebridge",
"port": 51395
},
"accessories": [],
"platforms": [
{
"name": "Config",
"port": 8581,
"platform": "config"
},
{
"platform": "homebridge-cbus.CBus",
"name": "CBus",
"client_ip_address": "127.0.0.1",
"accessories": [
{
"values": "existing"
}
]
}
]
}
This works beautifully:
jq '.platforms[1].accessories += [{ "values" : "NEW" }]'
... but of course it's poor form to expect platforms[1] to always the be array I want to append to, so I set about trying to form the right syntax for a search or if/then/else to only act on the .name of the appropriate one.
I thought this was my solution:
jq '.platforms[] | if ( .name=="CBus" ) then .accessories += [{ "values" : "NEW" }] else . end'
... until I realised it was only passing the 'platforms' through and eating the 'bridge' object and empty outer 'accessories' array, which I need to retain.
My issue looks to be similar to JQ | Updating array element selected by `select`, but I've tried LOTS of combinations but just can't break through.
Edit: Here's the correct JQPlay I've been working with:
https://jqplay.org/s/dGDswqAEte
Thanks for any help.
That's a good attempt. The key here is to use the select() function to identify the object you are going to update and overwrite the overall array using |= operator, i.e.
.platforms |= ( map(select(.name == "CBus").accessories += [{ "values" : "NEW" }] ) )
For the snippet in your jq-play link (now removed), you need to do
.gcp_price_list."CP-COMPUTEENGINE-OS"
|= with_entries(select(.value.cores == "shared").value.cores = "0.5")
Or if you want to be even more specific, and keep the entry in gcp_price_list configurable, do
.gcp_price_list |=
with_entries (
select(.key == "CP-COMPUTEENGINE-OS").value |=
with_entries(
select(.value.cores == "shared").value.cores = "0.5") )

RethinkDB filtering Object Array

I'm new to rethinkdb and I wanted to filter something like... get all with Kiwi or Strawberry as preferred fruit
{
"id": "65dbaa34-f7d5-4a25-b01f-682032fc6e05" ,
"fruits": {
"favorite": "Mango" ,
"preferred": [
"Kiwi" ,
"Watermelon"
]
}
}
I tried something like this after reading contains doc:
r.db('appname').table('food')
.filter(r.row('fruits').contains(function(doc) {
return doc('preferred').contains('Kiwi');
}))
And I'm getting a e: Cannot convert OBJECT to SEQUENCE in: error.
This is what you're looking for:
r.db('appname').table('food')
.filter((row) => {
r.or( // Returns true if any of the following are true
row('fruits')('preferred').contains('Kiwi'),
row('fruits')('preferred').contains('Strawberry')
)
});
You should know as well, that you can create your own index that calculates this for you, then you'd be able to do a .getAll query using your custom index and return all documents that fit this constraint very quickly.
Lastly, for something that would also work but is probably less efficient on large arrays:
r.db("appname").table('food')
.filter((row) => {
return row('fruits')('preferred').setIntersection(['Kiwi', 'Strawberry']).count().gt(0)
})

simple jq filter has null results

I'm using the filter
[.bar_1.baz_a, .bar_1.baz_b, .bar_2.qux_1,.bar_2.qux_2]
on the following JSON and it's returning four nulls instead of two lines each having four elements of nonsense data. This is my first attempt at a filter, what concept am I not comprehending?
{
"version": "0.1",
"foos": [
{
"bar_1": {
"baz_a": 673396201,
"baz_b": "dfgsfg"
},
"bar_2": {
"qux_1": "ghjhj",
"qux_2": "Q"
}
},
{
"bar_1": {
"baz_a": 674567484,
"baz_b": "tyutyj"
},
"bar_2": {
"qux_1": "bnmn",
"qux_2": "Z"
}
}
]
}
The root object doesn't have keys bar1 and bar2; those occur in the objects in the array assigned to the name foos. Compare your filter to
jq '.foos[] | [.bar_1.baz_a, .bar_1.baz_b, .bar_2.qux_1,.bar_2.qux_2]' tmp.json

Resources