how can i retrieve all the childrens by n1ql query - n1ql

I am planning to retrieve the child elements all the parent, How can i retrieve it, being inside the array of array objects.
SELECT sd.*
FROM test AS t
UNNEST stateDetails AS sd;
{
"type":"countries",
"docName":"CountryData",
"countryDetails":[
{
"name":"US",
"code":"+1",
"stateInfo":[
{
"name":"Florida",
"id":"1212"
},
{
"name":"NewYork",
"id":"1214"
}
]
},
{
"name":"France",
"code":"+33",
"stateInfo":[
{
"name":"Grand Est",
"id":"5212"
},
{
"name":"Brittany",
"id":"5214"
}
]
}
]
}
I am expecting the following output to bring out the state details of all the countries with their respective country Name
[
{
"countryName":"US",
"name":"Florida",
"id":"1212"
},
{
"countryName":"US",
"name":"NewYork",
"id":"1214"
},
{
"countryName":"France",
"name":"Grand Est",
"id":"5212"
},
{
"countryName":"France",
"name":"Brittany",
"id":"5214"
}
]

Use double UNNEST and project what you need
SELECT cd.name AS countryName, sd.name, sd.id
FROM test AS t
UNNEST t.countryDetails AS cd
UNNEST cd.stateInfo AS sd
WHERE t.type = "countries";

Related

Fetch key value from json data using terraform

I have a json data file from which I need to fetch only the id attribute value into a separate list variable. I tried for loop but not able to get the required data using terraform. Can someone tell how to fetch the id part ? your help is much appreacited.
code:
locals {
data = jsondecode(file("./data.json"))[*]
sections = [ for item in local.data : item ]
}
output "ids" {
value = [for a in local.sections[0]: a]
}
json file:
{
"c":[
{
"id":"6",
"key":"c",
"name":"s01"
}
],
"l":{
"id":"7",
"key":"l",
"name":"s02"
},
"m":{
"id":"8",
"key":"mp",
"name":"s03"
},
"n":{
"id":"5",
"key":"cn",
"name":"s04"
},
"od":"odk",
"s":{
"id":"9",
"key":"cs",
"name":"s05"
},
"ss":{
"id":"1",
"key":"ss",
"name":"s06"
},
"in":{
"id":"65",
"key":"cn",
"name":"s07"
},
"b":{
"id":"2",
"key":"cb",
"name":"s08"
}
}
Not sure if "od":"odk", is mistake or not, but you can do the following:
locals {
data = jsondecode(file("./data.json"))
}
output "ids" {
value = [for v in values(local.data): v.id if can(v.id)]
}

Extract a list of profileId's using jsonpath

I have the following JSON object r2,
[
{
"reserva_id":"200",
"estancias":[
{
"reserva_estancia":"266",
"huespedes":[
{
"reserva_huesped":"272",
"reserva_estancia":"266",
"numero_huesped":"1",
"huesped":"123",
"huesped_nombre":"dos dos, dos",
"rfid":null
},
{
"reserva_huesped":"276",
"reserva_estancia":"266",
"numero_huesped":"2",
"huesped":"183",
"huesped_nombre":"MUESTRA MUESTRA, CARMEN",
"rfid":null
}
]
}
]
},
{
"reserva_id":"201",
"estancias":[
{
"huespedes":[
{
"reserva_huesped":"273",
"reserva_estancia":"267",
"numero_huesped":"1",
"huesped":"148",
"huesped_nombre":"MUESTRA MUESTRA, CARMEN",
"rfid":null
},
{
"reserva_huesped":"277",
"reserva_estancia":"267",
"numero_huesped":"2",
"huesped":"187",
"huesped_nombre":"TEST TEST, TESTCIVITFUN",
"rfid":null
}
]
}
]
}
]
I am trying to get the first huesped for each reservation, and for that I am using the following script, to create a list called profiles and store profileId's:
def profiles = jsonpath(r2,'$..[:].estancias[:].huespedes[0].huesped')
The output should be the following:
[
"123",
"148"
]
However, when I print profiles.text I get all the content of the estancias object, instead of just the huesped number.
When using Jayway's JSONPath like this I get the desired oputput:
$..[*].estancias[*].huespedes[0].huesped
You can try the path expression with your JSON here online.

how to get child key without going with parent key or with iteration

1.I am having a list which contains multiple Maps that looks like below Map.
Each map contains many keys I want to get value of "name"
{
"question":{
"com.forms.tree":{
"requiredByDefault":true,
"questionDetails":{
"com.forms.Details":{
"preferredFormComponent":"TEXT"
}
},
"locale":{
"language":"en"
},
"formField":{
"name":"CUSTOM_347",
"tag":"input",
"url":"Demo"
}
}
},
"Field":"true"
},{
"question":{
"com.forms.tree":{
"questionDetails":{
"com.forms.Details":{
"preferredFormComponent":"TEXT"
}
},
"locale":{
"language":"en"
},
"formField":{
"name":"CUSTOM_348",
"url":"Demo"
}
}
},
"Field":"true"}
I want to get the value of "name" which falls in every Map but don't want to iterate like question?."com.forms.tree"?.formField?.name.
Is there any other approach in groovy?
So given the json:
def jsonTxt = '''{
"question":{
"com.forms.tree":{
"requiredByDefault":true,
"questionDetails":{
"com.forms.Details":{
"preferredFormComponent":"TEXT"
}
},
"locale":{
"name": "test",
"language":"en"
},
"formField":{
"name":"CUSTOM_347",
"tag":"input",
"url":"Demo"
}
}
},
"Field":"true"
}'''
We can parse it with:
import groovy.json.*
def json = new JsonSlurper().parseText(jsonTxt)
You want to find the "formField" entry in that object, so lets write a recursive finder that will walk through out map of maps looking for the first entry with the given key:
static findFirstByKey(Map map, key) {
map.get(key) ?: map.findResult { k, v -> if(v in Map) findFirstByKey(v, key) }
}
And you can then check it works:
assert findFirstByKey(json, 'formField')?.name == "CUSTOM_347"

N1ql to Get data from collection based on field value

I have a document as below
{
"GMParcelStatus": {
"storeNumber": 5678,
"GMVehicleTrips": {
"GMVehicleTrip": [
{
"GMVehicleTripId": "1000101",
"MultiChannelOrders": {
"MultiChannelOrder": [
{
"multiChannelOrderID": "4BQGBNJ3U",
"multichannelParcels": [
{
"multiChannelParcelStatus": "LOADING_MISSING",
"UPI": "00000008101058629797"
},
{
"multiChannelParcelStatus": "OUTFORDELIVERY",
"UPI": "00000008101058684938"
}
]
},
{
"multiChannelOrderID": "4BQGUNY56W",
"multichannelParcels": [
{
"multiChannelParcelStatus": "DELIVERED",
"UPI": "00000008101058629793"
},
{
"multiChannelParcelStatus": "DELIVERED",
"UPI": "00000008101058684932"
}
]
}
]
}
}
]
}
},
"_class": "com.tesco.bean.MultiChannelParcelRequestVO"
}
I want to get all the document in my bucket data based on storeNumber and GMVehicleTripId.
I have 4 document similar to above with different GMVehicleTripId.
I have written N1ql query like below Select d.* from Delivery d JOIN Delivery.GMParcelStatus.GMVehicleTrips.GMVehicleTrip[0] b
on keys meta(d).id where b.GMVehicleTripId in ['1000101']
but i don't want to do this GMVehicleTrip[0].
please get me the right way to do.
Thanks,
Vinay J
SELECT d.* FROM Delivery d JOIN Delivery b ON KEYS meta(d).id
WHERE ANY v IN b.GMParcelStatus.GMVehicleTrips.GMVehicleTrip SATISFIES v.GMVehicleTripId IN ['1000101'] END;

Elasticsearch - MissingMethodException on running distanceInKm on geo_point array

I have an elasticsearch document which has an array of geo_points. I have created the mapping as:
{
"test": {
"properties": {
"locations": {
"type": "geo_point"
}
}
}
}
Now, I am trying to create a query in which I want to do some processing on the array of geo_points. I have created my query like this:
{
"query": {
"filtered": {
"filter": {
"script": {
"script": "sDistance = doc['locations'].values[0].distanceInKm(28.51818,77.096080);"
}
}
}
}
}
I want to calculate the distance of the point (28.51818,77.096080) from the first element in the locations array.
It is giving me this error:
GroovyScriptExecutionException[MissingMethodException[No signature of method: org.elasticsearch.common.geo.GeoPoint.distanceInKm() is applicable for argument types: (java.lang.Double, java.lang.Double) values: [28.51818, 77.09608]]
I tried using sDistance = doc['locations'][0].distanceInKm(28.51818,77.096080); but it also resulted in the same error.
What am I doing wrong here?
Thanks in advance.
Your script should not retrieve the first geo point by itself, but simply call distanceInKm on the locations field directly, like this:
{
"query": {
"filtered": {
"filter": {
"script": {
"script": "sDistance = doc['locations'].distanceInKm(28.51818,77.096080);"
}
}
}
}
}
Under the hood, the first geo point will be retrieved and the distance will be computed on it.

Resources