How to map object values in JSONata - jsonata

I have an object like:
{ "contact": { "value": 0 },
"temperature": { "value": 5}
}
That I would like converted to
{ "contact": 0,
"temperature": 5
}
And I would like to avoid a spread/map/merge

I believe this is what you're looking for:
$keys($){
$: $lookup($$, $).value
}
You can check out this expression in Stedi's JSONata Playground here: https://stedi.link/V67vnsh

I know you wanted to avoid $merge, but this solution would also work and is relatively short:
$each($, function($v, $k) {{ $k: $v.value }}) ~> $merge
Check it out here: https://stedi.link/3tOCJHb

Related

Jsonpath or jmespath to get just id if created

{
"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?

How to use JSONpath to extract specific values

I'm using JSONpath to try and find data with an array of JSON objects but I'm struggling to get to the information I want. The array contains many objects similar to below where there are values for RecID throughout. If I use $..RecID I get them all when I only want the first Key.RecID of each object (with a value 1338438 in this example). Is there a way to only extract the top level Key.RecID value?
BTW I'm trying to do this in jMeter and I'm assuming JSONpath is the best way to do what I want but if there is a better way I'd be happy to hear about it.
Thanks in advance
[{
"Key": {
"RecID": 1338438
},
"Users": [{
"FullName": "Miss Burns",
"Users": {
"Key": {
"Name": "Burns",
"RecID": 1317474
}
}
},
{
"FullName": "Mrs Fisher",
"Users": {
"Key": {
"Name": "Fisher",
"RecID": 1317904
}
}
}
],
"User": {
"FullName": "Mrs Fisher",
"Key": {
"Name": "Fisher",
"RecID": 1317904
}
},
"Organisation": {
"Key": {
"RecID": 1313881
}
}
}]

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.

Nifi JOLT: flat JSON object to a list of JSON object

I am trying with Nifi JOLT processor to shift the flat JSON object to a list of JSON object as in below output. The params "p_7_1_0", "px_2_7_1_0", "pv_7_1_1" may be different in names or numbers (e.g. I could have { "timestamp": 1559347670, "pw_2_1_0": 1, "p_2_2_1_0": 1 } )
Could someone help me with the jolt specs?
Input Json:
{
"timestamp": 1559347670,
"p_7_1_0": 6,
"px_2_7_1_0": 1,
"pv_7_1_1": 1
}
Expected output JSON:
{
"values": [
{
"key": "p_7_1_0",
"value": 6,
"timestamp": 1559347670
},
{
"key": "px_2_7_1_0",
"value": 1,
"timestamp": 1559347670
},
{
"key": "pv_7_1_1",
"value": 1,
"timestamp": 1559347670
}
]
}
Thanks in advance
After reading up on this question JOLT transformation to copy single value along an array
and the answer
https://stackoverflow.com/a/50438480/2733184
I can see that what you want is eerily similar. However, I would have never hit the nail on the head with the question that needed to be asked.
I encourage you to go to the aforementioned Q and the A, read all of it (including the comments inside the spec) and give them some upvotes.
[
{
"operation": "shift",
"spec": {
"timestamp": "timestamp",
// put everything but the timestamp in a member (the pivot)
"*": "all.&"
}
},
{
"operation": "shift",
"spec": {
"all": {
"*": {
// grab the member key and put it in its final place
"$": "values[#2].key",
// grab the member value and put it in its final place
"#": "values[#2].value",
// Walk back two steps (* -> all -> root) and grab the timestamp
"#(2,timestamp)": "values[#2].timestamp"
// I wish I understood the logic behind "#2" but I don't
// and I'll have to read on it further
}
}
}
}
]
I hope someone can explain what # is for. My immediate guess is that it is like & (the member name) but it looks like it is the member position (?).

Update a subdocument list object value in RethinkDB

{
"id": 1,
"subdocuments": [
{
"id": "A",
"name": 1
},
{
"id": "B",
"name": 2
},
{
"id": "C",
"name": 3
}
]
}
How do update a subdocument "A"s "name" to a value of 2 in RethinkDB in either Javascript or Python?
If you can rely of the position of your "A " element you can update like this:
r.db("DB").table("TABLE").get(1)
.update({subdocuments:
r.row("subdocuments").changeAt(0, r.row("subdocuments").nth(0).merge({"name":2}))})
If you can not rely on the position, you have to find it yourself:
r.db("DB").table("TABLE").get(1).do(function(doc){
return doc("subdocuments").offsetsOf(function(sub){return sub("id").match("A")}).nth(0)
.do(function(index){
return r.db("DB").table("TABLE").update({"subdocuments":
doc("subdocuments").changeAt(index, doc("subdocuments").nth(index).merge({"name":2})) })})
})
As an alternative you can use the map function to iterate over the array elements and update the one that matches your condition
r.db("DB").table("TABLE").get(1)
.update({
subdocuments: r.row("subdocuments").map(function(sub){
return r.branch(sub("id").eq("A"), sub.merge({name: 2}), sub)
})
})

Resources