N1QL search certain objects within the object - json

I have multiple documents in my couchbase bucket with the following structure:
{
"objectType": "someObjectType",
"id": "1",
"latest": {
"id": "1",
"version": "biyr4jqi4nny"
},
"history": {
"bi11b295bjng": {
"id": "1",
"version": "bi11b295bjng"
}
"bi1189wx1h6v": {
"id": "1",
"version": "bi1189wx1h6v"
}
}
}
As seen in the snippet above, history is an object of objects. What I'm trying to do is selecting objectType, id, latest and history, but history should include only the object specified in query instead of all the history (which may be vast).
My query looks like this:
SELECT
bucket.id,
bucket.objectType,
bucket.latest,
bucket.history.bi11b295bjng
FROM bucket WHERE objectType = 'someObjectType'
Which produces me the following response:
[
{
"objectType": "someObjectType",
"id": 1,
"latest": {
"id": "9mobile_NG_001-ROW",
"version": "biyr4jqi4nny"
},
"biyr4jqi4nny": {
"id": "1",
"version": "biyr4jqi4nny"
}
}
]
Queried object got unwrapped from the parent one. Desired output should look like this:
[
{
"objectType": "someObjectType",
"id": 1,
"latest": {
"id": "9mobile_NG_001-ROW",
"version": "biyr4jqi4nny"
},
"history": {
"biyr4jqi4nny": {
"id": "1",
"version": "biyr4jqi4nny"
}
}
}
]
How could I get history.{version} without losing the parent object?

Construct object and Alias as history
SELECT
b.id,
b.objectType,
b.latest,
{ b.history.bi11b295bjng } AS history
FROM bucket AS b
WHERE b.objectType = "someObjectType";
If need multiple objects of same field
SELECT
b.id,
b.objectType,
b.latest,
{ b.history.bi11b295bjng, b.history.bi1189wx1h6v } AS history
FROM bucket AS b
WHERE b.objectType = "someObjectType";
If you have many want to remove one of them
SELECT
b.id,
b.objectType,
b.latest,
OBJECT_REMOVE(b.history,"bi11b295bjng") AS history
FROM bucket AS b
WHERE b.objectType = "someObjectType";

Related

Extract value of Tags from cloudTrail logs using Athena

I am trying to query cloudtrail logs using Athena. My goal is to find specific instances and extract them with their Tags.
The query I am using is:
SELECT eventTime, awsRegion , json_extract(responseelements, '$.instancesSet.items[0].instanceId') AS instanceId, json_extract(responseelements, '$.instancesSet.items[0].tagSet.items') AS TAGS FROM cloudtrail_logs_PP WHERE (eventName = 'RunInstances' OR eventName = 'StartInstances' ) AND requestparameters LIKE '%mytest1%' AND "timestamp" BETWEEN '2021/09/01' AND '2021/10/01' ORDER BY eventTime;
Using this query - I am able to get all Tags under one column.
Output of query
I want to extract only specific Tags and need help in the same. How cam I extract the only specific Tag?
I tried enhancing my query as json_extract(responseelements, '$.instancesSet.items[0].tagSet.items[0]' but the order of Tags is diff in diff logs - so cant pass the index location.
My json file in S3 is something like below:
{
"eventVersion": "1",
"eventTime": "2022-05-27T18:44:29Z",
"eventName": "RunInstances",
"awsRegion": "us-east-1",
"requestParameters": {
"instancesSet": {
"items": [{
"imageId": "ami-1234545",
"keyName": "DDKJKD"
}]
},
"instanceType": "m5.2xlarge",
"monitoring": {
"enabled": false
},
"hibernationOptions": {
"configured": false
}
},
"responseElements": {
"instancesSet": {
"items": [{
"tagSet": {
"items": [ {
"key": "11",
"value": "DS"
}, {
"key": "1",
"value": "A"
}]
}]
}
}
}

Microsoft Power-Automate Select operation does not work as intended

I've got a Select-Operation on an an object that contains one key more than once.
It's practically two versions of one object in one JSON object.
I want to get the id of both of those Objects.
When i inspect the Object, i can clearly see the two different Id's, but the Select-Operation returns only one of them twice.
This is the original Object:
[
{
"Created": "2020-06-05T11:47:42",
"ID": 9,
},
{
"Created": "2020-06-05T11:06:04",
"ID": 10,
}
]
The Select-Operation looks like this:
{
"inputs": {
"from": "#body('Rest')?['value']",
"select": {
"ID": "#triggerBody()?['ID']",
"Created": "#triggerBody()?['Created']"
}
}
}
And it returns:
[
{
"Created": "2020-06-05T11:47:42",
"ID": 9,
},
{
"Created": "2020-06-05T11:47:42",
"ID": 9,
}
]
I don't really understand what's going on.
"select": {
"ID": "#triggerBody()?['ID']",
"Created": "#triggerBody()?['Created']"
}
is wrong, it should select item()?['ID']

AWS Athena - Querying JSON - Searching for Values

I have nested JSON files on S3 and am trying to query them with Athena.
However, I am having problems to query the nested JSON values.
My JSON file looks like this:
{
"id": "17842007980192959",
"acount_id": "17841401243773780",
"stats": [
{
"name": "engagement",
"period": "lifetime",
"values": [
{
"value": 374
}
],
"title": "Engagement",
"description": "Total number of likes and comments on the media object",
"id": "17842007980192959/insights/engagement/lifetime"
},
{
"name": "impressions",
"period": "lifetime",
"values": [
{
"value": 11125
}
],
"title": "Impressions",
"description": "Total number of times the media object has been seen",
"id": "17842007980192959/insights/impressions/lifetime"
},
{
"name": "reach",
"period": "lifetime",
"values": [
{
"value": 8223
}
],
"title": "Reach",
"description": "Total number of unique accounts that have seen the media object",
"id": "17842007980192959/insights/reach/lifetime"
},
{
"name": "saved",
"period": "lifetime",
"values": [
{
"value": 0
}
],
"title": "Saved",
"description": "Total number of unique accounts that have saved the media object",
"id": "17842007980192959/insights/saved/lifetime"
}
],
"import_date": "2017-12-04"
}
What I'm trying to do is to query the "stats" field value where name=impressions.
So ideally something like:
SELECT id, account_id, stats.values.value WHERE stats.name='engagement'
AWS example: https://docs.aws.amazon.com/athena/latest/ug/searching-for-values.html
Any help would be appreciated.
You can query the JSON with the following table definition:
CREATE EXTERNAL TABLE test(
id string,
acount_id string,
stats array<
struct<
name:string,
period:string,
values:array<
struct<value:string>>,
title:string
>
>
)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
LOCATION 's3://bucket/';
Now, the value column is available through the following unnesting:
select id, acount_id, stat.name,x.value
from test
cross join UNNEST(test.stats) as st(stat)
cross join UNNEST(stat."values") as valx(x)
WHERE stat.name='engagement';

Sending the JSON response as array or normal object

I am implementing a restful service where I am getting the pdf names and their ids from the database in the JSON fromat. Which one of the both the convenient JSON resful service response?
First Option:
{
"results": {
"documentNames": [
"test.pdf",
"ireport-ultimate-guide.pdf",
"sending report.pdf",
"Motor Hour.pdf"
],
"documentds": [
21116,
21117,
21118,
21119
]
}
}
Second Option:
{
"results": {
"21116": "test.pdf",
"21117": "ireport-ultimate-guide.pdf",
"21118": "sending report.pdf",
"21119": "Motor Hour.pdf"
}
}
I would use this "third option": The result is a list of object.
{
"result": [{
"id": "21116",
"filename": "test.pdf"
},
{
"id": "21117",
"filename": "ireport-ultimate-guide.pdf"
},
{
"id": "21118",
"filename": "sending report.pdf"
},
{
"id": "21119",
"filename": "Motor Hour.pdf"
}
]
}
because it better models the object structure.
I would create an entity for each document that contains both name and I'd.
[
{"name": "doc_1", "id": 123},
{"name": "doc_2", "id": 456}
]

How to Append JSON Object in already created object in mysql json document

My object is
{
"name":"Testing",
"id": "hcig_3fe7cb00-e936-11e6-af69-a748c8cc89ad",
"belongsTo": {
"id": "69616d26-c3bb-405c-8c84-c51c091524b2",
"name": "test"
},
"locatedAt": {
"id": "49616d26-c3bb-405c-8c84-c51c091524b2",
"name":"Test"
} }
I want to merge one more object like
"obj":[{
"a": 123
}}
With the help of JSON_MERGE in mysql document store i am able to add object.
But it looks likes
{
"name":"Tester",
"id": "hcig_3fe7cb00-e936-11e6-af69-a748c8cc89ad",
"belongsTo": {
"id": "69616d26-c3bb-405c-8c84-c51c091524b2",
"name": "test"
},
"locatedAt": {
"id": "49616d26-c3bb-405c-8c84-c51c091524b2",
"name":"Test"
},{
"obj":[{
"a": 123
}]
}}
I want my object to be as
{
"name": "Tester",
"id": "hcig_3fe7cb00-e936-11e6-af69-a748c8cc89ad",
"belongsTo": {
"id": "69616d26-c3bb-405c-8c84-c51c091524b2",
"name": "test"
},
"locatedAt": {
"id": "49616d26-c3bb-405c-8c84-c51c091524b2",
"name": "Test"
},
"obj": [{
"a": 123
}]}
Any idea on how to add object as above manner using JSON Functions in mysql ??
Use lodash for a recursive deep copy - https://lodash.com/
lodash.merge(targetObj, sourceObj);
Or if you have programmatic access:
targetObj.obj = sourceObj;