JsonPath Fetch string after filter - java

I am using Jayway JsonPath 2.2 version in JAVA. I have few questions on the same.
Sample JSON:
{
"l5": [
{
"type": "type1",
"result": [
"res1"
]
},
{
"type": "type2",
"result": [
"r1"
]
}
]
}
I want to fetch a string after a filter is applied?
Eg:
Path used to fetch a string is l5[?(#.type == 'type2')].type
Expected result: type2 (string), but getting ["type2"] (array)
Please correct the path if i am doing anything wrong to fetch it as a string?
Unable to index the resultant array after the filter is applied. How can i achieve the same?
Eg:
If i use the path l5[?(#.type == 'type2')][0], instead of returning me the first JSONObject it returns []
Is it possible to extract substring from a string using jsonPath?
Something like l5[0].type[0,2] => res
Note: I cannot do any operation in JAVA, as i wanted to keep the library generic?

The return value of jsonPath is always an array, quoted from here.
So, the operation1 you are trying is not possible i.e. String value will never be returned.
Even operation 2 and operation 3 doesn't look feasible with jayway jsonpath.
Good read on jayway Jsonpath.
Only Possible way for operation 1 and 2 looks something like below:-
String jsonpath="l5[?(#.type == 'type2')].type";
DocumentContext jsonContext = JsonPath.parse(jsonString);
List<String> typeDataList= jsonContext.read(jsonpath);
System.out.println(typeDataList.get(0)); //type2

Related

How to access key value in array of objects - java

I'm getting result from cloudant db and response type would be Document object.
This is my query:
FindResult queryResult = cloudantConfig.clientBuilder()
.postFind(findOptions)
.execute()
.getResult();
This is my result from cloudant db:
{
"bookmark": "Tq2MT8lPzkzJBYqLOZaWZOQXZVYllmTm58UHpSamxLukloFUc8BU41GXBQAtfh51",
"docs": [
{
"sports": [
{
"name": "CRICKET",
"player_access": [
"All"
]
}
]
}
]
}
I'd like to access 'name' and 'player access,' but I can only go up to'sports,' and I can't get to 'name' or 'player access.' This is how I attempted to obtain 'name.'
queryResult.getDocs().get(0).get("sports").get(0).get("name");
With above one I'm getting an error like this The method get(int) is undefined for the type Object
I'm receiving the values when I try to get up to'sports.'
This is how I obtain sports:
queryResult.getDocs().get(0).get("sports");
When I sysout the aforementioned sports, I get the results below.
[{name=CRICKET, player_access=[All]}]
So, how do I gain access to 'name' and 'player access' here? Can somebody help me with this?
I've dealed with JSON values recently. But ended up just using regex and splitting/matching from there.
You can regex everything from the "name" (not including until the last comma) and do the same for sport Access.
Be aware that this is just a work around, and not the best option. But sometimes JSON objects on Java can be Tricky.

Retrieve and perform sorting on Array values from the JSON response using BeanShell and JSON extractor or any other way?

I was retrieved a JSON response in Jmeter4.0 its look like this:
{
"jsonrpc":"2.0",
"id":123456789,
"result":{
"id":123,
"name":"Stock",
"type":"ir.actions.act_window",
"view_id":false,
"domain":[
[
"id",
"in",
[
4,
1,
3
]
]
],
"xml_id":"stock.action_all",
"display_name":"Stock"{
}
}
}
I want to perform sorting in that ids (Expected Result: 1,3,4).
How can I achieve this with a BeanShell post-processor or JSON path extractor or any other way on Jmeter 4.0?
How about JSON JMESPath Extractor and the following JMESPath query:
sort(result.domain[0][2])
Demo:
More information:
JMESPath sort() function
The JMeter JSON JMESPath Extractor and Assertion: A Guide

How to get value with an underscore inside a string from Elasticsearch using QueryBuilder in Java?

I'm using Elasticsearch 3.2.7 and ElasticsearchRepository.search() which takes QueryBuilder as an argument (doc)
I have a BoolQueryBuilder and use it like this:
boolQuery.must(termQuery("myObject.code", value);
var results = searchRepository.search(boolQuery);
The definition of the field code is as follows:
"myObject": {
"properties": {
"code": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
The issue is, when I search with a value that has underscore inside, for example: FOO_BAR then it doesn't return any results. When I search with other values that have either leading or trailing underscore then it's fine.
I've read that ES may ignore the special character and split the words inside by it so there's a need for an exact match search. But I also read that the keyword setting guarantees that. So right now I'm confused.
yes, you are correct, using keyword field you can achieve the exact match, you need to use the below query
boolQuery.must(termQuery("myObject.code.keyword", value); --> note addition of keyword
var results = searchRepository.search(boolQuery);
you can use the analyze API to see the tokens for your indexed documents and search term, and basically your tokens in index must match search terms tokens, in order ES to return the match :)

JSON path condifitional index of element based on value of another field

I want to select array element based on value of another object(not array part)
{
"array": [
{
"id": 1
},
{
"id": 2
}
],
"conditionalField": "ab"
}
I want select array element based on value of $.conditionalField. I need something like this:
$.array[($.conditionalField == "ab" ? 0 : 1)]
Does json path support this? I use Jayway JSON Path
Unfortunately, this cannot be done in JSONPath as of now. Generally, current implementations lack the fluency of XPath 1.0 (let alone v2.0/3+).
A (well known) trick mimicking a conditional (if-else) in a language like XPath 1.0 that does not come with such a feature is to use a technique called "Becker's method", i.e. concatenating two mutually exclusive strings, where one of two strings becomes empty depending on a condition. In (XPath) pseudo-code:
concat(
substring('foo', 1, number(true) * string-length('foo')),
substring('bar', 1, number(not(true)) * string-length('bar'))
)
We might be able to do this using JSON-Path in JavaScript and leveraging script eval; I think it should look like this (slit-up for better readability):
$.[?(#.conditionalField && #.dict[0].id && #.dict[1].id && #.temp.concat(
#.dict[0].id.substring(0, #.conditionalField==='ab' * #.dict[0].id.length()) )
.concat( #.dict[1].id.substring(0, #.conditionalField==='xy' * #.dict[1].id.length()) )
)]
but it is not working. So, as of now, there seems to be no way to mimic an if-else construct in JSON-Path directly.
A practical approach is selecting both properties separately and make the decision in your host environment:
$.[?(#.conditionalField=='ab')].dict[0].id
$.[?(#.conditionalField=='xy')].dict[1].id
Try something like this:
$.[?(#.conditionalField=="ab")].array[1].id
Output should be:
[
2
]

how to parse(separate structure and value) json format String in java

let's say I have a String which has a format as Json.
If I want to separate values from the String.
and exchange the values with '#number'.
Currently, I don't know the keys.
Is there any easy way to get this result?
for example,
from
{
"data": [
{
"skills": "Java",
"platforms": "Web"
}
],
"status": "100"
}
to
{
"data": [
{
"skills": #1,
"platforms": #2
}
],
"status": #3
}
with array result [Java, Web, 100]
Probably if you are looking to parse a unknown json and print the values Check this out
I wont be able to give you the exact code you need, but you should be able to implement on similar lines

Categories

Resources