elasticsearch search for elements with specified ID example - json

I want to find certain elements in my elastic search that have a given ID and I can't figure an easy way to do this.
I see http://www.elasticsearch.org/guide/reference/query-dsl/ids-query/ but can't for the life of me figure out how to structure a query to use it, or when I do toy around with es-head or curl I see errors like:
Parse Failure [Failed to parse source [{"query":{"match_all":{}},"ids
{"values""1","4","100"]}}]]]; nested: SearchParseException[[dailyaggregates][4]:
query[ConstantScore(NotDeleted(*:*))],from[-1],size[-1]: Parse Failure [No parser for
element [ids]]]; }]
etc. Can anyone tell me how to set this up? Thanks.
edit: My attempt with that error was from es-head but similar errors through curl. I believe what I tried was some variant of this:
{
"query": {
"match_all": {}
},
"ids": {
"values": [
"100"
]
}
}

ids is a type of query, just like match, or match_all. So the format should be:
{"query":{ "ids":{ "values": [ 100 ] } } }
You can alternatively do it as a filter, like so:
{"filter":{ "query": {"ids":{ "values": [ 100 ] } } } }

Related

JSONPath to get multiple values from nested json

I have a nested JSON in a field that contains multiple important keys that I would like to retrieve as an array:
{
"tasks": [
{
"id": "task_1",
"name": "task_1_name",
"assignees": [
{
"id": "assignee_1",
"name": "assignee_1_name"
}
]
},
{
"id": "task_2",
"name": "task_2_name",
"assignees": [
{
"id": "assignee_2",
"name": "assignee_2_name"
},
{
"id": "assignee_3",
"name": "assignee_3_name"
}
]}]}
All the queries that I've tried so far fx ( $.tasks.*.assignees..id) and many others have returned
[
"assignee_1",
"assignee_2",
"assignee_3"
]
But what I need is:
[
["assignee_1"],
["assignee_2", "assignee_3"]
]
Is it possible to do with JSONPath or any script inside of it, without involving 3rd party tools?
The problem you're facing is that tasks and assignees are arrays. You need to use [*] instead of .* to get the items in the array. So your path should look like
$.tasks[*].assignees[*].id
You can try it at https://json-everything.net/json-path.
NOTE The output from my site will give you both the value and its location within the original document.
Edit
(I didn't read the whole thing :) )
You're not going to be able to get
[
["assignee_1"],
["assignee_2", "assignee_3"]
]
because, as #Tomalak mentioned, JSON Path is a query language. It's going to remove all structure and return only values.

Json script question to match array of stings

I have a script using the "anything-but" function, please see below. I have way too many RDS-EVENTs (hundreds) and it will be much easier to match RDS-EVENT that I need. How do I re-write this and instead of returning "anything-but", return everything that matches particular RDS-EVENTs?
Thank you for your help.
{
"source": [
"aws.rds"
],
"detail": {
"EventID": [{
"anything-but": [
"RDS-EVENT-0004",
"RDS-EVENT-0006",
"RDS-EVENT-0204",
"RDS-EVENT-0207",
"RDS-EVENT-0213",
"RDS-EVENT-0214",
"RDS-EVENT-XXXX",
"RDS-EVENT-0203",
"RDS-EVENT-0206",
"RDS-EVENT-0205",
"RDS-EVENT-0208"
]
}]
}
}

JsonPath filter query syntax

I'm trying to write a JsonPath query that selects a specific object based on a condition, but either the syntax fails me or I fail the syntax.
Given the below Json object, how would I select the "Data" object containing Dirk Gently's details based on the fact that he uses the "Stumble" method?
{
"Investigators": [
{
"Type": "Legend",
"Object": {
"Method": "Investigate",
"Data": {
"Name": "Sherlock",
"Surname": "Holmes"
}
}
},
{
"Type": "Visionary",
"Object": {
"Method": "Stumble",
"Data": {
"Name": "Dirk",
"Surname": "Gently"
}
}
}
],
"Version": 1
}
I know that I can get to the Method-field like this:
$.Investigators..Object.Method
I assumed that something like this would work:
$.Investigators..Object[?(#.Method=="Stumble")].Data
I'm testing this using: https://jsonpath.com/ to evaluate the query - and I can't seem to get it right.
Am I trying to do something that is not achievable or am I just making a stupid mistake?
Go to this jsonpath online sandbox and try this experssion:
$.Investigators..Object[?(#.Method=="Stumble")].Data
using either the Jayway or Gatling implementations, the output will be:
[
{
"Name" : "Dirk",
"Surname" : "Gently"
}
]

POSTMAN How to parse a nested json object in postman which has dynamic keys?

supposing the json body returned from a call contains some dynamic keys ie
{
"message": "search results matching criteria",
"permission": {
"261ef70e-0a95-4967-b078-81e657e32699": {
"device": {
"read:own": [
"*"
]
},
"account": {
"read:own": [
"*"
]
},
"user": {
"read:own": [
"*"
]
}
}
}
I can validate the json as follows easily enough although I am having a lot of trouble working out how to validate the objects BELOW the dynamic guid level of the response.
pm.test("response body to have correct items", function () {
pm.expect(jsonData.message).to.eq("search results matching criteria");
pm.expect(jsonData).to.have.property('permission');
pm.expect(jsonData.permission).to.have.property(pm.variables.get("otherUserId"));
});
Would ideally like to verify the device and account and user levels of the object.
Anyone with some tips?
I've tried a few ways to try and reference the otherUserId variable but nothing is working. It is either not resolving the variable therefore failing the test as its looking for a level in the json called otherUserId or it fails to run the test due to a syntax error.
This works:
pm.expect(jsonData.permission[pm.variables.get("otherUserId")]).to.have.property('device');

Talend XPath syntax problems when trying to access Google Translate API Response

I am trying to parse multiple translations in Talend using a tExtractJSONFields component. I am not that familiar with XPath.
{
"data": {
"translations": [
{
"translatedText": "Bonjour"
},
{
"translatedText": "Au Revoir"
}
]
}
}
When I am only translating a single element, this configuration works:
However when I am requesting multiple translations, I am trying to guess at the syntax to pull out the different translatedText values in the response.
For example, this doesn't work it seems:
Any help appreciated. I am sending 4 items for translation so expect an array of 4 JSON objects each with a "translatedText" property.
Updated:
Response with 4 items is as below:
{
"data": {
"translations": [
{
"translatedText": "Product 1"
},
{
"translatedText": "04/12/1984"
},
{
"translatedText": "Withdrawn"
},
{
"translatedText": "national"
}
]
}
}
When I try this:
I get close, but all the output looks like it has square brackets around it indicating an array of sorts.
And I have tried the above with "translations[0]/translatedText[0]" as the XPath query and it does the same thing.
Actually this seems to have worked: