Selecting the first JSON result after predicate - json

I'm trying to write a JSON path that contains a predicate not specific enough to select a single element, yet I want to force the result to be the first one. I would like to have it done using the path only (not as part of the C# code later).
The following path returns the first book, which I expected: $.book[0]
The following path returns exactly two books with price > 0, which I expected: $.book[?(#.price>20)]
Now, I would like to return the first element from the result. I was hoping for this to work, but it doesn't. Somehow this does not work at all:
$.book[?(#.price>20)][0]
I am using .NET and Json.NET library, but I am open to any other.
The example JSON is as follows:
{
"book":[
{
"title":"Beginning JSON",
"author":"Ben Smith",
"price":49.99
},
{
"title":"JSON at Work",
"author":"Tom Marrs",
"price":29.99
},
{
"title":"Learn JSON in a DAY",
"author":"Acodemy",
"price":8.99
},
{
"title":"JSON: Questions and Answers",
"author":"George Duckett",
"price":6.00
}
]
}

Related

Expression issues (JSON) with power automate

I'm working on power automate and I have an issue because I've tried a lot expressions and none of them working.
I did a loop (foreach) and inside a message (compose) and there I tried to display "f7626790-0756-43bf-a757-a645a33b853d" from"client"
To do so, I've tried all those expressions:
item()?['client']
item()?['client']?[0]
item()['client']
Here my json file:
[
{
"gift": [
{
"client": "f7626790-0756-43bf-a757-a645a33b853d",
"details": [
{
"client_id": 10859085,
"type": "christmas",
"application_id": "6e6d758d-8e74-3ae3-ac84-50eb23ae65f"
}
]
}
]
}
]
Thank's in advance.
Based on the JSON you provided, you have two arrays. So the question for me is, which array do you want to iterate? Both or the inner one?
I'm going to assume both or if not, it won't matter if you only ever have one item for both of them, my answer will still retrieve the client property.
The Initialize Data step has your JSON.
The Initialize Client step is a blank string at this stage.
The For Each Outer Item expression is simply a link to the Data variable as initialized in the first step.
The For Each Gift expression is set as ... item()?['gift'] ... as you can work out, that will loop over each gift item.
Finally, I get the value of the client property using this expression ... item()['client']

Searching for an item by a known value of an element in the array of an element in the array

I have a known value for an element, ENTRYID in my JSON Document. I want to get the Tasks.Id for the element of Tasks that has an ENTRYID matching my known value. The issue is that ENTRYID is a little bit buried inside Tasks. As an example, here is the JSON Path to find the first ENTRYID in the document.
$.Tasks[0].Activities[0].Entry.AnswersFlat[0].ENTRYID
For pathing to ENTRYID from a Tasks element, it is always in Activities[0].Entry.AnswersFlat[0]. Those two arrays do not need to be searched.
I know I can use ?(#.Tag == 'Known Value') to find elements that contain a known value for a specific tag name but when I try to use that in my JSON path, I keep getting errors. I've tried:
$.Tasks[?(#.Activities[0].Entry.AnswersFlat[0].ENTRYID=='95B53C6CF6104F0E86B5744083A22FAD')].Id
But this returns an error.
I have also tried nesting the filters but that also returns an error :
$.Tasks[?(#.Activites[?(#.Entry.AnswersFlat[?(#.ENTRYID=='95B53C6CF6104F0E86B5744083A22FAD')])])].Id
What is the correct syntax in JSON Path to get the Tasks.Id for my known value of ENTRYID?
I have created a very simplified version of my document for your reference:
{
"Tasks":[
{
"Id":"652a187f2a584a0f84aaadb9016cfa4b",
"Activities":[
{
"Entry":{
"AnswersFlat":[
{
"ENTRYID":"95B53C6CF6104F0E86B5744083A22FAD"
}
]
}
}
]
},
{
"Id":"e586073810a746cca440adb901090326",
"Activities":[
{
"Entry":{
"AnswersFlat":[
{
"ENTRYID":"7FD608F3F9E24EB084F58A1245677B3D"
}
]
}
}
]
}
]
}

How to get a json value dynamically?

in Jmeter, for the message below i want to get the value of tag CC2650HumidityData (or tag name and tag value) but I don't know the name of tag; it is contained in a variable.
I've used this json path:
$.message.inputData[*].${tagName}
$.message.inputData[*].vars.get("tagName")
but without success.
This is an example of message:
{
"message": {
"inputData": [
{
"CC2650HumidityData": "51",
"dateTime": "2020-12-18T08:35:21.342Z"
},
{
"CC2650BarometricPressureData": "21",
"dateTime": "2020-12-18T08:35:15.136Z"
}
]
}
}
Could you help me?
Your first option, to wit this one: $.message.inputData[*].${tagName} should work just fine
Just make sure that your tagName variable exists and has the anticipated value, it can be done using Debug Sampler and View Results Tree listener combination
Also be aware that you won't be able to use JSONPath Tester mode of the View Results Tree listener for testing JSONPath queries as it doesn't evaluate variables, you will need to run your test to see the variables values (or use this Dummy Sampler like I did)

JSON Deserialization on Talend

Trying to figuring out how to deserialize this kind of json in talend components :
{
"ryan#toofr.com": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"rbuckley#toofr.com": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
"ryan.buckley#toofr.com": {
"confidence":18,"email":"ryan.buckley#toofr.com","default":16
},
"ryanbuckley#toofr.com": {
"confidence":17,"email":"ryanbuckley#toofr.com","default":17
},
"ryan_buckley#toofr.com": {
"confidence":16,"email":"ryan_buckley#toofr.com","default":18
},
"ryan-buckley#toofr.com": {
"confidence":15,"email":"ryan-buckley#toofr.com","default":19
},
"ryanb#toofr.com": {
"confidence":14,"email":"ryanb#toofr.com","default":14
},
"buckley#toofr.com": {
"confidence":13,"email":"buckley#toofr.com","default":13
}
}
This JSON comes from the Toofr API where documentation can be found here .
Here the actual sitation :
For each line retreived in the database, I call the API and I got this (the first name, the last name and the company change everytime.
Does anyone know how to modify the tExtractJSONField (or use smthing else) to show the results in tLogRow (for each line in the database) ?
Thank you in advance !
EDIT 1:
Here's my tExtractJSONfields :
When using tExtractJSONFields with XPath, you need
1) a valid XPath loop point
2) valid XPath mapping to your structure relative to the loop path
Also, when using XPath with Talend, every value needs a key. The key cannot change if you want to loop over it. Meaning this is invalid:
{
"ryan#toofr.com": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"rbuckley#toofr.com": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
but this structure would be valid:
{
"contact": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"contact": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
So with the correct data the loop point might be /contact.
Then the mapping for Confidence would be confidence (the name from the JSON), the mapping for Email would be email and vice versa for default.
EDIT
JSONPath has a few disadvantages, one of them being you cannot go higher up in the hierarchy. You can try finding out the correct query with jsonpath.com
The loop expression could be $.*. I am not sure if that will satisfy your need, though - it has been a while since I've been using JSONPath in Talend because of the downsides.
I have been ingesting some complex json structures and did this via minimal json libraries, and tjava components within talend.

Couchbase View not returning array value

I am trying to create a view to group on a particular attribute inside an array. However, the below map function is not returning any result.
JSON Document Structure :
{
"jsontype": "survey_response",
"jsoninstance": "xyz",
"jsonlanguage": "en_us",
"jsonuser": "test#test.com",
"jsoncontact": "test#mayoclinic.com",
"pages": [
{
q-placeholder": "q1-p1",
q:localized": "q1-localized-p1",
q-answer-placeholder": "jawaabu121",
q-answer-localized": "localized jawaabu1"
},
{
q-placeholder": "q2-p2",
q:localized": "q2-localized-p2",
q-answer-placeholder": "jawaabu221",
q-answer-localized": "localized jawaabu2"
},
{
"q-placeholder": "q3-p3",
"q:localized": "q3-localized-p3",
"q-answer-placeholder": "jawaabu313",
"q-answer-localized": "localized jawaabu3"
}
]
}
Map Function :
function(doc, meta){
emit(doc.jsoninstance,[doc.pages[0].q-placeholder, doc.pages[0].q-localized,doc.pages[0].q-answer-placeholder,q-answer-localized]);
}
It looks like you made a typo at the end of your emit statement:
doc.pages[0].q-answer-placeholder,q-answer-localized.
Instead q-answer-localized should be changed to doc.pages[0].q-answer-localized.
Further to this it seems that you have defined a field as q-localized in your emit statement, but actually according to the sample document that you posted this should actually be q:localized, I assume that this was a mistake in the snippet of the document and not the emit statement, but if not then will also need amending.
I would imagine errors like this would be flagged up in the view engine's map-reduce error log, in future you should check this log so that you will be able to debug errors like this yourself.
The location of the mapreduce_errors log can be found in the Couchbase documentation