Jsonpath or jmespath to get just id if created - shell

{
"data": [
{
"compartment-id": "ocid1.compartment.oc1..42949232syq",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "svc-1",
"CreatedOn": "2022-08-19T05:48:06.460Z"
}
},
"display-name": "service1-ic-20220819T054722",
"freeform-tags": {},
"id": "ocid1.instanceconfiguration.oc1.me-jeddah-1.adhk32khsaq",
"time-created": "2022-08-19T05:48:06.586000+00:00"
},
{
"compartment-id": "ocid1.compartment.oc1..aaaahsa232dvsanmsakhfyq",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "svc-1",
"CreatedOn": "2022-08-16T05:47:49.269Z"
}
},
"display-name": "service2-ic-20220819T054705",
"freeform-tags": {},
"id": "ocid1.instanceconfiguration.oc1.me-jeddah-1.aaasafhsknvlsasrhv4w5lgcfyuvgtbsofd2wuk7c6yh2rgigx2xfaa",
"time-created": "2022-08-16T05:47:49.546000+00:00"
} ,
{
"compartment-id": "ocid1.compartment.oc1..aaaahsa232dvsanmsakhfyq",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "svc-1",
"CreatedOn": "2022-08-15T05:47:49.269Z"
}
},
"display-name": "service2-ic-20220819T054705",
"freeform-tags": {},
"id": "ocid1.instanceconfiguration.oc1.me-jeddah-1.aaasafhsknvlsasrhv4w5lgcfyuvgtbsofd2wuk7c6yh2rgigx2xfaa",
"time-created": "2022-08-15T05:47:49.546000+00:00"
}
]
}
Above is json array,
I want to get list of "id" if any time-created is before 2022-08-19, it means we have in our array two elements. So we should return
[ocid1.instanceconfiguration.oc1.me-jeddah-1.aaasafhsknvlsasrhv4w5lgcfyuvgtbsofd2wuk7c6yh2rgigx2xfaa, ocid1.instanceconfiguration.oc1.me-jeddah-1.aaasafhsknvlsasrhv4w5lgcfyuvgtbsofd2wuk7c6yh2rgigx2xfaa]
If this is doable using shell script? or there is way to use jmespath to return this values.

Lexicographical ordering in Python should do the job. For example,
shell> cat get_id.py
#!/usr/bin/python3
import json
import jmespath
compareDate = '2022-08-19'
f = open('data.json', 'r')
data_json = json.loads(f.read())
# print(json.dumps(data_json, indent=2))
ids = jmespath.search(f'data[?"time-created" < `{compareDate}`].id', data_json)
print(*ids, sep='\n')
gives
shell> ./get_id.py
ocid1.instanceconfiguration.oc1.me-jeddah-1.aaasafhsknvlsasrhv4w5lgcfyuvgtbsofd2wuk7c6yh2rgigx2xfaa
ocid1.instanceconfiguration.oc1.me-jeddah-1.aaasafhsknvlsasrhv4w5lgcfyuvgtbsofd2wuk7c6yh2rgigx2xfaa
See:
JMESPath date filtering
How are strings compared?

Related

Incorrectly selected data in the query

Only articles that contain the EmailMarketing tag are needed.
I'm probably doing the wrong search on the tag, since it's an array of values, not a single object, but I don't know how to do it right, I'm just learning graphql. Any help would be appreciated
query:
query {
enArticles {
title
previewText
tags(where: {name: "EmailMarketing"}){
name
}
}
}
result:
{
"data": {
"enArticles": [
{
"title": "title1",
"previewText": "previewText1",
"tags": [
{
"name": "EmailMarketing"
},
{
"name": "Personalization"
},
{
"name": "Advertising_campaign"
}
]
},
{
"title": "title2",
"previewText": "previewText2",
"tags": [
{
"name": "Marketing_strategy"
},
{
"name": "Marketing"
},
{
"name": "Marketing_campaign"
}
]
},
{
"title": "article 12",
"previewText": "article12",
"tags": []
}
]
}
}
I believe you first need to have coded an equality operator within your GraphQL schema. There's a good explanation of that here.
Once you add an equality operator - say, for example _eq - you can use it something like this:
query {
enArticles {
title
previewText
tags(where: {name: {_eq: "EmailMarketing"}}){
name
}
}
}
Specifically, you would need to create a filter and resolver.
The example here may help.

Parsing a JSON array with jq

I need to parse out specific information from the following JSON example bellow using jq. depending on the 'repo' I need to be able to parse from the properties array all 'key' & 'values' and associated with the repository. For example the repo "libs-production-local" would need to parse out any properties key with 'prod' in the string and its associated date value. has to be jq and run in .sh.
{
"results":[
{
"repo":"libs-production-local",
"path":"com/company/version",
"name":"sql.21.tar",
"type":"file",
"size":"40123",
"created":"date",
"created_by":"someone",
"modified":"date",
"modified_by":"someone",
"updated":"date",
"depth":4,
"actual_md5":"asdflsdf23a4324234",
"orginal_sha1":"sadlkfjsdklfjsadf",
"properties":[
{
"key":"deploy.uat",
"value":"2018-09-23"
},
{
"key":"deploy.prod.TLE",
"value":"2018-10-20"
},
{
"key":"deploy.prodXYZ",
"value":"2018-10-20"
},
{
"key":"deploy.prodPDQ",
"value":"2018-10-20"
},
{
"key":"deploy.prod.ABC",
"value":"2018-10-21"
},
{
"key":"businessUnit.name",
"value":"IndivdualName"
},
{
"key":"deploy.qa.ser2",
"value":"2018-10-20"
},
{
"key":"deploy.qa.ser1",
"value":"2018-11-23"
},
{
"key":"build.timestamp",
"value":"1510850899004"
}
],
"virtual_repos":[
"libs-production "
]
},
{
"repo":"libs-production-local",
"path":"com/company/version",
"name":"sql.22.tar",
"type":"file",
"size":"40123",
"created":"date",
"created_by":"someone",
"modified":"date",
"modified_by":"someone",
"updated":"date",
"depth":4,
"actual_md5":"asdflsdf23a4324234",
"orginal_sha1":"sadlkfjsdklfjsadf",
"properties":[
{
"key":"deploy.prodPDQ",
"value":"2018-10-22"
},
{
"key":"deploy.prodABC",
"value":"2018-10-20"
},
{
"key":"businessUnit.name",
"value":"IndivdualName"
},
{
"key":"deploy.qa",
"value":"2018-10-20"
},
{
"key":"deploy.dev",
"value":"2018-11-19"
}
],
"virtual_repos":[
"libs-production "
]
}
],
"range":{
"start_pos":0,
"end_pos":479,
"total":479
}
}
I've tried a number of ways to do this (including this one) and nothing works.
jq -r '.results[] | ( .properties |map(select(.key[] contains ("prod")) '
I solved it like this:
jq -r '[ .results[].properties[] | select(.key | contains("prod")) ]'
This grabs all key-value pairs from each result's properties array. It then selects those that contain "prod" in the key, and returns an array of those keys and values. Given your example input from above, this is the return value:
[
{
"key": "deploy.prod.TLE",
"value": "2018-10-20"
},
{
"key": "deploy.prodXYZ",
"value": "2018-10-20"
},
{
"key": "deploy.prodPDQ",
"value": "2018-10-20"
},
{
"key": "deploy.prod.ABC",
"value": "2018-10-21"
},
{
"key": "deploy.prodPDQ",
"value": "2018-10-22"
},
{
"key": "deploy.prodABC",
"value": "2018-10-20"
}
]
Is that close to what you're looking for?

How to filter on a array with graphql in Generic Mesh CMS?

Im trying to get just entrys which have in a specific value in a array (myArray: [String]).
Displaying of this Array is not Problem:
query ($lang: [String!], $filter: String!) {
nodes(lang: $lang, filter: {schema: {is: myObj}, fields: {myObj: {name: {regex: $filter}}}}) {
elements {
uuid
language
availableLanguages
fields {
... on module {
name
myArray
}
}
node {
language
fields {
... on module {
name
myArray
}
}
}
}
}
}
Result:
{
"data": {
"nodes": {
"elements": [
{
"uuid": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
"language": "de",
"availableLanguages": [
"de"
],
"fields": {
"name": "ASDF",
"myArray": [
"CAT_1",
"CAT_2"
]
},
"node": null
}
]
}
}
}
How can I filter on myArray? That I just et elements with the value uf $filter in the array myArray?
In the GraphiQL I can't find the myArray in the docs under the filter - fields.
GraphQL-Filtering for list types is not supported yet. In the GraphiQL docs you will only find supported fields for now.
See supported field types here: https://getmesh.io/docs/beta/graphql.html#_filtering_limitations
Here is the open issue on Github regarding this feature: https://github.com/gentics/mesh/issues/27

DynamoDB DocumentClient returns Set of strings (SS) attribute as an object

I'm new to DynamoDB.
When I read data from the table with AWS.DynamoDB.DocumentClient class, the query works but I get the result in the wrong format.
Query:
{
TableName: "users",
ExpressionAttributeValues: {
":param": event.pathParameters.cityId,
":date": moment().tz("Europe/London").format()
},
FilterExpression: ":date <= endDate",
KeyConditionExpression: "cityId = :param"
}
Expected:
{
"user": "boris",
"phones": ["+23xxxxx999", "+23xxxxx777"]
}
Actual:
{
"user": "boris",
"phones": {
"type": "String",
"values": ["+23xxxxx999", "+23xxxxx777"],
"wrapperName": "Set"
}
}
Thanks!
The [unmarshall] function from the [AWS.DynamoDB.Converter] is one solution if your data comes as e.g:
{
"Attributes": {
"last_names": {
"S": "UPDATED last name"
},
"names": {
"S": "I am the name"
},
"vehicles": {
"NS": [
"877",
"9801",
"104"
]
},
"updatedAt": {
"S": "2018-10-19T01:55:15.240Z"
},
"createdAt": {
"S": "2018-10-17T11:49:34.822Z"
}
}
}
Please notice the object/map {} spec per attribute, holding the attr type.
Means you are using the [dynamodb]class and not the [DynamoDB.DocumentClient].
The [unmarshall] will Convert a DynamoDB record into a JavaScript object.
Stated and backed by AWS. Ref. https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/Converter.html#unmarshall-property
Nonetheless, I faced the exact same use case, as yours. Having one only attribute, TYPE SET (NS) in my case, and I had to manually do it. Next a snippet:
// Please notice the <setName>, which represents your set attribute name
ddbTransHandler.update(params).promise().then((value) =>{
value.Attributes[<setName>] = value.Attributes[<setName>].values;
return value; // or value.Attributes
});
Cheers,
Hamlet

How to grep and sort from JSON file by specific word?

I have the following JSON file:
{
"opscenter_adhoc_2014-02-03-12-34-16-UTC": {
"keyspaces": {
"test": {
"nodes": [
"10.242.214.188",
"10.62.77.47",
"10.244.15.39"
],
"cfs": {
"test": "/var/lib/cassandra/data/test/test/snapshots/opscenter_adhoc_2014-02-03-12-34-16-UTC"
}
}
},
"id": "adhoc",
"time": 1391430856
},
"opscenter_adhoc_2014-02-03-13-16-04-UTC": {
"keyspaces": {
"test": {
"nodes": [
"10.242.214.188",
"10.62.77.47",
"10.244.15.39"
],
"cfs": {
"test": "/var/lib/cassandra/data/test/test/snapshots/opscenter_adhoc_2014-02-03-13-16-04-UTC"
}
}
},
"id": "adhoc",
"time": 1391433364
},
"opscenter_adhoc_2014-02-03-11-32-06-UTC": {
"keyspaces": {
"test": {
"nodes": [
"10.242.214.188",
"10.62.77.47",
"10.244.15.39"
],
"cfs": {
"test": "/var/lib/cassandra/data/test/test/snapshots/opscenter_adhoc_2014-02-03-11-32-06-UTC"
}
}
},
"id": "adhoc",
"time": 1391427126
}
I'd like to grep the file by opscenter_adhoc_2014-02-03-11-32-06-UTC only where 2014-02-03-11-32-06-UTC is timestamp of backup and than sort the list by timestamps to get the latest timestamp.
Tried in Ruby by using
File.readlines("/tmp/out.txt").grep(/opscenter_adhoc/)
which returns me all the string but I need just only opscenter_adhoc_TIMESTAMP list
If I understood you correctly, you are expecting to get sorted array of dates. Here you go:
require 'json'
JSON.parse(File.read('/tmp/qq.json')).keys.sort
# ⇒ => [
# [0] "opscenter_adhoc_2014-02-03-11-32-06-UTC",
# [1] "opscenter_adhoc_2014-02-03-12-34-16-UTC",
# [2] "opscenter_adhoc_2014-02-03-13-16-04-UTC"
# ]

Resources