Jsonpath - filter at child level to get parent attribute - jsonpath

Is there any way to extract "a" value by filtering on "d" in the following json?
[
{
"a":1,
"b":{ "c":11,"d":12 }
},
{
"a":2,
"b":{ "c":21,"d":22}
}
]

$..[?(#.b.d == 12)].a
above expression can solve this

Related

Terraform: Add item to a DynamoDB Table

What is the correct way to add tuple and key-pair values items to a DynamoDB database via Terraform?
I am trying like this:
resource "aws_dynamodb_table_item" "item" {
table_name = aws_dynamodb_table.dynamodb-table.name
hash_key = aws_dynamodb_table.dynamodb-table.hash_key
for_each = {
"0" = {
location = "Madrid"
coordinates = [["lat", "40.49"], ["lng", "-3.56"]]
visible = false
destinations = [0, 4]
}
}
item = <<ITEM
{
"id": { "N": "${each.key}"},
"location": {"S" : "${each.value.location}"},
"visible": {"B" : "${each.value.visible}"},
"destinations": {"L" : [{"N": "${each.value.destinations}"}]
}
ITEM
}
And I am getting the message:
each.value.destinations is tuple with 2 elements
│
│ Cannot include the given value in a string template: string required.
I also have no clue on how to add the coordinates variable.
Thanks!
List should be something like that :
"destinations": {"L": [{ "N" : 1 }, { "N" : 2 }]}
You are trying to pass
"destinations": {"L": [{ "N" : [0,4] }]}
Also you are missing the last } in destinations key
TLDR: I think the problem here is that you are trying to put L(N) - i.e. a list of numeric values, while your current Terraform code tries to put all the destinations into one N/number.
Instead of:
[{"N": "${each.value.destinations}"}]
you need some iteration over destinations and building a {"N": ...} of them.
"destinations": {"NS": ${jsonencode(each.value.destinations)}}
Did the trick!

How to know if there are other paths than a given path?

Is it possible to know with JSONPath that other "paths" exist?
By an existing "path" I mean a string in the form "a.b.c" or "a.b.d" like for this JSON:
{
'a' : {
'b' : [ { 'c' : 0 }, { 'd': 1 ]
}
}
Can a JSONPath be written to tell if there any other "paths" other then a.b.c and a.b.d ?
For example in the following JSON, it should find that there is the property 'e':
{
'a' : {
'b' : [ { 'c' : 0 }, { 'd': 1 ],
'e': 2
}
}
It depends on JSONPath Implementation. The jsonpath options have a setting to output the path or value.
JSONPATH
$..*
All possible Output Paths
[
"$['a']",
"$['a']['b']",
"$['a']['e']",
"$['a']['b'][0]",
"$['a']['b'][1]",
"$['a']['b'][0]['c']",
"$['a']['b'][1]['d']"
]
Tool : https://jsonpath.com/
Checkbox Option : Output paths
Tool : https://jsonpath.herokuapp.com/
Checkbox option : Normalized path expressions

How to access final element in Handlebars.js array?

Given the following data structure:
{
"name" : "test",
"assetType" : "Home",
"value" : [
{
"value" : "999",
"date" : "10/03/2018"
},
{
"value" : "1234.56",
"date" : "10/04/2018"
}
]
}
How can I directly access the first and last element of value in Handlebars? My code is:
<td>{{value.[0].value}}</td>
<td>{{value.[value.length - 1].value}}</td>
The first statement works fine, but I cannot figure out how to access the last element. value.length inside the []'s does not return any value, but rendering value.length outside of the array works fine.
I tried using #first and #last in the array but those don't seem to work either.
As already pointed out, you can easily solve this with a template helper:
Template.myTemplate.helpers({
first (arr) {
return arr && arr[0]
},
last (arr) {
return arr && arr[arr.length - 1]
},
atIndex (arr, index) {
return arr && index >= 0 && index <= arr.length - 1 && arr[index]
}
})
You can then create a context using #with where the context object is the result of the helpers. Inside this context you can access properties using this.
Accessing values with the name value would look like the following:
<td>{{#with first value}}{{this.value}}{{/with}}</td>
<td>{{#with last value}}{{this.value}}{{/with}}</td>
<td>{{#with atIndex value 5}}{{this.value}}{{/with}}</td>

simple jq filter has null results

I'm using the filter
[.bar_1.baz_a, .bar_1.baz_b, .bar_2.qux_1,.bar_2.qux_2]
on the following JSON and it's returning four nulls instead of two lines each having four elements of nonsense data. This is my first attempt at a filter, what concept am I not comprehending?
{
"version": "0.1",
"foos": [
{
"bar_1": {
"baz_a": 673396201,
"baz_b": "dfgsfg"
},
"bar_2": {
"qux_1": "ghjhj",
"qux_2": "Q"
}
},
{
"bar_1": {
"baz_a": 674567484,
"baz_b": "tyutyj"
},
"bar_2": {
"qux_1": "bnmn",
"qux_2": "Z"
}
}
]
}
The root object doesn't have keys bar1 and bar2; those occur in the objects in the array assigned to the name foos. Compare your filter to
jq '.foos[] | [.bar_1.baz_a, .bar_1.baz_b, .bar_2.qux_1,.bar_2.qux_2]' tmp.json

Query to get exact matches of Elastic Field with multile values in Array

I want to write a query in Elastic that applies a filter based on values i have in an array (in my R program). Essentially the query:
Matches a time range (time field in Elastic)
Matches "trackId" field in Elastic to any value in array oth_usr
Return 2 fields - "trackId", "propertyId"
I have the following primitive version of the query but do not know how to use the oth_usr array in a query (part 2 above).
query <- sprintf('{"query":{"range":{"time":{"gte":"%s","lte":"%s"}}}}',start_date,end_date)
view_list <- elastic::Search(index = "organised_recent",type = "PROPERTY_VIEW",size = 10000000,
body=query, fields = c("trackId", "propertyId"))$hits$hits
You need to add a terms query and embed it as well as the range one into a bool/must query. Try updating your query like this:
terms <- paste(sprintf("\"%s\"", oth_usr), collapse=", ")
query <- sprintf('{"query":{"bool":{"must":[{"terms": {"trackId": [%s]}},{"range": {"time": {"gte": "%s","lte": "%s"}}}]}}}',terms,start_date,end_date)
I'm not fluent in R syntax, but this is raw JSON query that works.
It checks whether your time field matches given range (start_time and end_time) and whether one of your terms exact matches trackId.
It returns only trackId, propertyId fields, as per your request:
POST /indice/_search
{
"_source": {
"include": [
"trackId",
"propertyId"
]
},
"query": {
"bool": {
"must": [
{
"range": {
"time": {
"gte": "start_time",
"lte": "end_time"
}
}
},
{
"terms": {
"trackId": [
"terms"
]
}
}
]
}
}
}

Resources