How to filter on field inside array of references using Sanity GROQ? - sanity

Given this result from Sanity vision, how can I correctly filter out the shows array to only return shows that are active === true?
{
"result": {
"_createdAt": "",
"_id": "xyz",
"_rev": "abc",
"_type": "content",
"active": true,
"content": {
"_key": "47d6e74f1f81",
"_type": "radio",
"shows": [ <-- Reference of `radioShow`
{
"active": true
},
{
"active": false
}
]
},
}
}
Here is my groq query. How do I add a filter to the shows array?
*[_type == "content"
&& active == true
&& content[0]._type == "radio"
] {
...,
"content": content[0] {
...,
shows[]-> { <-- How do I had a filter here?
active,
}
}
}[0]

Related

Indexing Firebase Realtime Database

What I am trying to accomplish is accessing a single item in my database. I set up my indexing but when I query it, I get back an empty object.
Accessing it like this shows an empty object
https://yourapp.firebaseio.com/ticket.json?orderBy=%22TicketNumber%22&equalTo=2240
{
"-N6sEsiL25tQSzlNjvc4": {
"form": {
"Company": "Test",
"CompanyInvolved": "Test",
"DateCompleted": "2022-07-27T21:51:00.000Z",
"DateDepartShop": "2022-07-19T15:43:00.000Z",
"DateDepartSite": "2022-07-21T18:47:00.000Z",
"Details": "D2",
"ItemCode": "IC2",
"MoreDetails": [
[
{
"Details": "D1",
"ItemCode": "IC1",
"Quantity": "Q1"
}
]
],
"Quantity": "Q2",
"RecievedBy": "Test",
"Signature": "file:///Users/arod/Library/Developer/CoreSimulator/Devices/4498CCA0-C6F4-4E9F-BCAE-19ADB385E758/data/Containers/Data/Application/F0A9086E-A773-43C3-861F-91D3666AB627/Library/Caches/ExponentExperienceData/%2540arod1207%252FAOS/sign.png",
"ThirdParty": true,
"TicketNumber": 2240,
"TimeArriveShop": "2022-07-21T21:51:00.000Z",
"TimeArriveSite": "2022-07-19T18:47:00.000Z",
"TimeDepartShop": "2022-07-19T18:43:00.000Z",
"TimeDepartSite": "2022-07-21T18:51:00.000Z",
"TodaysDate": "2022-07-20T15:43:00.000Z"
}
},
My rules are as followed
{
"rules": {
".read": "true",
".write": "true",
"ticket": {
".indexOn": "TicketNumber"
}
}
}
Since the value you want to index lives under form/TicketNumber of each direct child node of the ticket path, that is also what you must define the index for.
So:
{
"rules": {
".read": "true",
".write": "true",
"ticket": {
".indexOn": "form/TicketNumber"
}
}
}
Same: that form/TicketNumber is also the path you need to specify for the orderBy parameter of your query.

How to check a particular value on basis of condition in karate

Goal: Match the check value is correct for 123S and 123O response in API
First check the value on this location x.details[0].user.school.name[0].codeable.text if it is 123S then check if x.details[0].data.check value is abc
Then check if the value on this location x.details[1].user.school.name[0].codeable.text is 123O then check if x.details[1].data.check is xyz
The response in array inter changes it is not mandatory first element is 123S sometime API returns 123O as first array response.
Sample JSON.
{
"type": "1",
"array": 2,
"details": [
{
"path": "path",
"user": {
"school": {
"name": [
{
"value": "this is school",
"codeable": {
"details": [
{
"hello": "yty",
"condition": "check1"
}
],
"text": "123S"
}
}
]
},
"sample": "test1",
"id": "22222"
},
"data": {
"check": "abc"
}
},
{
"path": "path",
"user": {
"school": {
"name": [
{
"value": "this is school",
"codeable": {
"details": [
{
"hello": "def",
"condition": "check2"
}
],
"text": "123O"
}
}
]
},
"sample": "test",
"id": "11111"
},
"data": {
"check": "xyz"
}
}
]
}
How I did in Postman but how to replicate same in Karate?
var jsonData = pm.response.json();
pm.test("Body matches string", function () {
for(var i=0;i<jsonData.details.length;i++){
if(jsonData.details[i].user.school.name[0].codeable.text == '123S')
{
pm.expect(jsonData.details[i].data.check).to.equal('abc');
}
if(jsonData.details[i].user.school.name[0].codeable.text == '123O')
{
pm.expect(jsonData.details[i].data.check).to.equal('xyz');
}
}
});
2 lines. And this takes care of any number of combinations of lookup values :)
* def lookup = { '123S': 'abc', '123O': 'xyz' }
* match each response.details contains { data: { check: '#(lookup[_$.user.school.name[0].codeable.text])' } }

JSONPath does not return values when using in Karate but does using online evaluator

I'm fairly new to JSONPath so this could be my fault but when I try this expression in an online evaluator (https://jsonpath.com/) it works but does not in Karate.
$..entry[?(#.resource.resourceType == 'AllergyIntolerance' && #.resource.category=='food')].resource.code.coding.*.system
If I use an index I am able to get the first element out but I want to grab all elements that match the expression regardless of their index in case there are more items in the array and not my specific data example.
Working JSONPath:
$..entry[?(#.resource.resourceType == 'AllergyIntolerance' && #.resource.category[0]=='food')].resource.code.coding.*.system
I've tried to use wildcards but that doesn't seem to work:
$..entry[?(#.resource.resourceType == 'AllergyIntolerance' && #.resource.category[*]=='food')].resource.code.coding.*.system
JSON snippit with relevant sections
{
"entry": [ {
"resource": {
"resourceType": "AllergyIntolerance",
"id": "allergyFood",
"category": [ "food" ],
"criticality": "high",
"code": {
"coding": [ {
"system": "http://snomed.info/sct",
"code": "91935009",
"display": "Allergy to peanuts"
} ],
"text": "Allergy to peanuts"
},
"reaction": [ {
"manifestation": [ {
"coding": [ {
"system": "http://snomed.info/sct",
"code": "271807003",
"display": "skin rash"
} ],
"text": "skin rash"
} ],
"severity": "mild"
} ]
}
}, {
"resource": {
"resourceType": "AllergyIntolerance",
"id": "allergyMed",
"verificationStatus": "unconfirmed",
"type": "allergy",
"category": [ "medication" ],
"criticality": "high",
"code": {
"coding": [ {
"system": "http://www.nlm.nih.gov/research/umls/rxnorm",
"code": "7980",
"display": "penicillin"
} ]
}
}
} ]
}
The JsonPath engine is known to have issues with such complex expressions. Please use karate.filter() instead which I am sure you will agree is much more readable: https://github.com/intuit/karate#json-transforms
* def resources = $..resource
* def fun = function(x){ return x.resourceType == 'AllergyIntolerance' && x.category[0] == 'food' }
* def temp = karate.filter(resources, fun)

POSTGRESQL query to extract attributes in JSON

I have the below JSON in a particular DB column. I need a query to extract fields stored within the savings rate(to and from).
{
"data": [
{
"data": {
"intro_visited": {
"portfolio_detail_investment_journey": true,
"dashboard_investments": true,
"portfolio_list_updates": true,
"portfolio_detail_invested": true,
"portfolio_list_offering": true,
"dashboard_more_bottom_bar": true
}
},
"type": "user_properties",
"schema_version": "1"
},
{
"data": {
"savings_info": {
"remind_at": 1583475493291,
"age": 100,
"savings_rate": {
"to": "20",
"from": "4"
},
"recommendation": {
"offering_name": "Emergency Fund",
"amount": "1,11,111",
"offering_status": "not_invested",
"ideal_amount": "1,11,111",
"offering_code": "liquid"
}
}
},
"type": "savings_info",
"schema_version": "1"
}
]
}
To get the "To"
$..data.savings_info.savings_rate.to
To get the "From"
$..data.savings_info.savings_rate.from
This script works
SELECT
<column> ->'data'->2->'data'->'savings_info'->'savings_rate'->>'to' AS to_rate
from <table>

make a new array from a nested object using Lodash

Here is my data
[
{
"properties": {
"key": {
"data": "companya data",
"company": "Company A"
}
},
"uniqueId" : 1
},
{
"properties": {
"key": {
"data": "companyb data",
"company": "Company B"
}
},
"uniqueId" : 2
},
{
"properties": {
"key": {
"data": "companyc data",
"company": "Company C"
}
},
"uniqueId" : 3
}
]
The format I need for my typeahead directive is below. I was trying to figure out the other post I made but still couldn't make it work. The best was to just make the nested collection as a simple collection of object.
[
{
"uniqueId" : 1,
"data": "companya data"
},
{
"uniqueId" : 2,
"data": "companyb data"
},
{
"uniqueId" : 3,
"data": "companyc data"
}
]
I got it!
console.log(
_(jsonData).map(function(obj) {
return {
d : obj.properties.key.data,
id : obj.uniqueId
}
})
.value()
);
You do not have to use the chaining feature of lodash as long as you are only performing one operation. You can simply use:
_.map(jsonData, function(obj) {
return {
d : obj.properties.key.data,
id : obj.uniqueId
}
});