Getting all subitems from all documents with a Couchbase map/reduce view - couchbase

I have Couchbase documents with structure like this:
{
"subscriberIds": [
{
"type": "END_USER",
"identity": "00000000223"
}
],
"userDataId": "SUB-00000000223",
"status": "ACTIVATED",
"subscriptions": [
{
"id": "Something1",
"attributes": [
{
"value": "Active",
"name": "Status"
}
],
"priority": "1",
"subscriptionStartDate": somedate,
"productShortName": "Something1"
},
{
"id": "Something2",
"attributes": [
{
"value": "Active",
"name": "Status"
}
],
"priority": "1",
"subscriptionStartDate": somedate,
"productShortName": "Something2"
}
],
}
And I'm trying to write a view to get all the 'subscriptions' from all documents in bucket as:
{"total_rows":900,"rows":[
{"id":"Something1","key":null,"value":"00000000223"},
{"id":"Something2","key":null,"value":"00000000223"},
...
but, I can't get nested item from doc
function (doc, meta) {
for (var i in doc.subscriptions) {
emit(doc.subscriptions.id, doc.id);
}
}
I know this is possible, but apparently I do not fully understand the concept of views.

for (var i in doc.subscriptions) {
emit(doc.subscriptions[i].id, doc.id);
}

Related

Merge Json Array Nodes and roll up a child element

I have a requirement to roll a collection of nodes that uses the current node name (within the collection) and for the value take each child nodes value (single node) into a string array, then use the parents key as the key.
Given.
{
"client": {
"addresses": [
{
"id": "27ef465ef60d2705",
"type": "RegisteredOfficeAddress"
},
{
"id": "b7affb035be3f984",
"type": "PlaceOfBusiness"
},
{
"id": "a8a3bef166141206",
"type": "EmailAddress"
}
],
"links": [
{
"id": "29a9de859e70799e",
"type": "Director",
"name": "Bob the Builder"
},
{
"id": "22493ad4c4fd8ac5",
"type": "Secretary",
"name": "Jennifer"
}
],
"Names": [
{
"id": "53977967eadfffcd",
"type": "EntityName",
"name": "Banjo"
}
]
}
}
from this the output needs to be
{
"client": {
"addresses": [
"RegisteredOfficeAddress",
"PlaceOfBusiness",
"EmailAddress"
],
"links": [
"Director",
"Secretary"
],
"Names": [
"EntityName"
]
}
}
What is the best way to achieve this? Any pointers to what/how to do this would be greatly appreciated.
Ron.
You can iterate over entries of your client object first with the help of the $each function, then get types for each of them, and combine via $merge:
{
"client": client
~> $each(function($list, $key) {{ $key: $list.type }})
~> $merge
}
Live playground: https://stedi.link/OpuRdE9

Json Path Read from a Kafka Message

I have a kafka message like below, where im trying to read the data from the json path. However im having a challenge when reading some of the attributes from the json path. here is the sample message.
sample1:
{
"header": {
"bu": "google",
"id": "12345",
"bum": "google",
"originTimestamp": "2021-10-09T15:17:09.842+00:00",
"batchSize": "0",
"jobType": "Batch"
},
"payload": {
"derivationdetails": {
"Id": "6783jhvvh897u31y283y",
"itemid": "1234567",
"batchid": 107,
"attributes": {
"itemid": "1234567",
"lineNbr": "1498",
"cat": "5929",
"Id": "6783jhvvh897u31y283y",
"indicator": "false",
"subcat": "3514"
},
"Exception": {
"values": [
{
"type": "PICK",
"value": "blocked",
"Reason": [
"RULE"
],
"rules": [
"439"
]
}
],
"rulesBagInfo": [
{
"Idtype": "XXXX",
"uniqueid": "7889423rbhevfhjaufdyeuiryeukjbdafvjd",
"rulesMatch": [
"439"
]
}
]
}
}
}
}
sample 2: Same message but see the difference in "Payload"
{
"header": {
"bu": "google",
"id": "12345",
"bum": "google",
"originTimestamp": "2021-10-09T15:17:09.842+00:00",
"batchSize": "0",
"jobType": "Batch"
},
"payload": {
"Id": "6783jhvvh897u31y283y",
"itemid": "1234567",
"batchid": 107,
"attributes": {
"itemid": "1234567",
"lineNbr": "1498",
"cat": "5929",
"Id": "6783jhvvh897u31y283y",
"indicator": "false",
"subcat": "3514"
},
"Exception": {
"values": [
{
"type": "PICK",
"value": "blocked",
"Reason": [
"RULE"
],
"rules": [
"439"
]
}
],
"rulesBagInfo": [
{
"Idtype": "XXXX",
"uniqueid": "7889423rbhevfhjaufdyeuiryeukjbdafvjd",
"rulesMatch": [
"439"
]
}
]
}
}
}
If you observe, sometimes the message has "derivationdetails", and sometimes it doesn't. But irrespective of its existence, i need to read the values of id,itemid and batchid. I tried using
$.payload[*].id
$.payload[*].itemid
$.payload[*].batchid
But i see that for batchid is returning null even though it has a value in the message, and the attributes under "attributes" return null if im using the above. For fields under "attributes" im using this(example):
$.payload.attributes.itemId
And, completely blank on how to read the below part.
"Exception": {
"values": [
{
"type": "PICK",
"value": "blocked",
"Reason": [
"RULE"
],
"rules": [
"439"
]
}
],
"rulesBagInfo": [
{
"Idtype": "XXXX",
"uniqueid": "7889423rbhevfhjaufdyeuiryeukjbdafvjd",
"rulesMatch": [
"439"
]
Im new to this and need some suggestions on how to read the attributes properly. Any help would be much appreciated.Thanks
Use ..(recursive descent, Deep scan. JSONPath borrows this syntax from E4X.) to get the values. But It will return a list if there are multiple entries with same key nested in deep.
Below jsonpath expressions will return a list with one item each for both sample1 and sample2
$.payload..attributes.Id
$.payload..attributes.itemid
$.payload..batchid
$.payload..Exception

Query fields in google contacts REST API

I am trying to build a bash scripts to manage my google contacts programatically using the google contacts api. From the api-reference I can see the rest api to list full/single contacts entry in xml or json format. But I cannot able see any filter option to fetch name, mobile number alone.
https://www.google.com/m8/feeds/contacts/{userEmail}/full?alt=json
Response:
{
"version": "1.0",
"encoding": "UTF-8",
"entry": {
"id": {
"$t": "http://www.google.com/m8/feeds/contacts/xxxxxxxx%40gmail.com/base/xxxxxxxxxxxxxxxx"
},
"updated": {
"$t": "2019-03-16T02:56:27.686Z"
},
"category": [
{
"scheme": "http://schemas.google.com/g/2005#kind",
"term": "http://schemas.google.com/contact/2008#contact"
}
],
"title": {
"$t": "NEW_CONTACT_NAME",
"type": "text"
},
"link": [
{
}
],
"content": {
"$t": "Tets Entry!",
"type": "text"
},
"gd$organization": [
{
"rel": "http://schemas.google.com/g/2005#other",
"gd$orgName": {
"$t": "javis"
}
}
],
"gd$email": [
{
"address": "xxxxxxxxxx#gmail.com",
"rel": "http://schemas.google.com/g/2005#other"
}
],
"gd$phoneNumber": [
{
"label": "Prime",
"uri": "tel:+xx-xxxxx-xxxxx",
"$t": "+xx xxxxx xxxxx"
}
],
"gContact$groupMembershipInfo": [
{
"deleted": "false",
"href": "http://www.google.com/m8/feeds/groups/xxxxxxxx%40gmail.com/base/xxxxxxxxxxxxxxxx"
}
],
"xmlns": "http://www.w3.org/2005/Atom",
"xmlns$batch": "http://schemas.google.com/gdata/batch",
"xmlns$gContact": "http://schemas.google.com/contact/2008",
"xmlns$gd": "http://schemas.google.com/g/2005"
}
}
Could you please help me to find the solution to filter specific field like contact id, name, number or mail from the json response.

Angular HTTP Res JSON Data print in html

Below is the HTTP get request response. how can i print same in view using *ngFor . iam trying to print single object print like data[0].value1 in console. it returns undefined. inside data array 3 JSON Objects. and some arrays also
Any help much appreciated...
getEmp(){
this.EmpService.getAccounts().subscribe((res : any[])=>{
this.products = JSON.stringify(res);
});
.
{
"data": [
{
"value1": "3546786908765432",
"category": "Clothing (Brand)",
"category_list": [
{
"id": "001",
"name": "Clothing (Brand)"
},
{
"id": "39324324230",
"name": "Boutique Store"
},
{
"id": "12342324324",
"name": "Website"
}
],
"name": "wqeqws",
"id": "qwsqwsq w",
"tasks": [
"ANALYZE",
"ADVERTISE",
"MODERATE",
"CREATE_CONTENT",
"MANAGE"
]
},
{
"value1": "111-9=-09876543",
"category": "Education",
"category_list": [
{
"id": "23456",
"name": "Education"
}
],
"name": "sdcsfcsdfvsd",
"id": "111111111111",
"tasks": [
"ANALYZE",
"ADVERTISE",
"MODERATE",
"CREATE_CONTENT",
"MANAGE"
]
}
],
"paging": {
"cursors": {
"before": "dsfsds",
"after": "sdfsfs"
}
}
}
this.products = res.data;
In your page
< *ngFor="let item of products >

Get nested Childrens in JSON using Angular2

I'm working on json Data with inner childrens. i need to get all the name's in the json.
json data
this.res = [
{
"children": [
{
"children": [
{
"children": [],
"name": "P Sub Child 3",
},
{
"children": [
{
"children":[],
"name" : "data"
}
],
"name": "PSubChild2",
}
],
"name": "PChild1",
},
{
"children": [
{
"children": [],
"name": "PSubChild3",
}
],
"name": "PChild2",
}
],
"name": "Parent1",
},
{
"children": [],
"name": "Parent2",
}
];
in my application names of every child has to store in the variables using angular2.
Data is dynamic changes as per the user in my application
From what I understand you want to just get the value of the name property down the hierarchical data you have then as most here suggested you have to use recursion to get to the name property. With Rxjs it becomes a bit easier to do like following:
Observable.from(this.res)
.expand(d => d.children.length > 0 ? Observable.from(d.children) : Observable.empty())
.subscribe(d => console.log(d.name));
I have made a working sample here: https://jsfiddle.net/ibrahimislam/ba17w8xz/1/
Here is a plunkr of a possible implementation
https://plnkr.co/edit/njM1HLx7vuZY9loto2pM?p=preview
Create your own datatype:
export class MyNode {
children:Array<MyNode>;
name:string;
}
and then create a method to recursively loop your parents to get their names and the names of the children
parents:Array<MyNode> = [
{
"children": [
{
"children": [
{
"children": [],
"name": "P Sub Child 3",
},
{
"children": [
{
"children":[],
"name" : "data"
}
],
"name": "PSubChild2",
}
],
"name": "PChild1",
},
{
"children": [
{
"children": [],
"name": "PSubChild3",
}
],
"name": "PChild2",
}
],
"name": "Parent1",
},
{
"children": [],
"name": "Parent2",
}
];
names:Array<string>=[];
getAllNames(){
this.getNameHelper(this.parents);
}
getNameHelper(parents:Array<MyNode>){
for(var p of parents){
if(p.name){
this.names.push(p.name);
}
if(p.children){
this.getNameHelper(p.children);
}
}
}