JSONPath and regex: filter to search end substring - jsonpath

I need to extract "https://www.domain3.com" from this JSON
{
"jobs": [
{
"name": "alfa",
"url": "https://www.domain1.com"
},
{
"name": "beta_publish",
"url": "https://www.domain2.com"
},
{
"name": "gammma_release",
"url": "https://www.domain3.com"
}
]
}
using a regular expression.
I'm trying to use this one (in https://www.jsonquerytool.com/ ...),
$..jobs[?(#.name=~/release*?/i)].url
but it doesn't work, but if I try to use it in https://regex101.com/ it seems to work fine.
Any suggestion will be appreciated!

Use match instead of =~
JSONPath
$.jobs[?(#.name.match(/release/i))].url

Related

Using JSONPath to filter and get only specific properties as result?

Is it possible to use JSONPath to both filter and "select" properties to keep for the result.
For instance if we have the following JSON:
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "naist street",
"city": "Nara",
"postalCode": "630-0192"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
}
And we run a JSONPath like:
$.phoneNumbers[?(#.type=='iPhone')]
We would end up with the result:
[
{
"type": "iPhone",
"number": "0123-4567-8888"
}
]
Now lets say that what I actually want is the phonenumber.. so we could do this:
$.phoneNumbers[?(#.type=='iPhone')].number
Which would result in:
[
"0123-4567-8888"
]
However.. the result that I would like to get would be:
[
{
"number": "0123-4567-8888"
}
]
Notice how the actual JSON-object is returned and the property..
Is that possible to achive somehow using JSONPath?
Since you know what property you're looking for (i.e. number), what you can do is to construct a new Json object with the property name and the returned value.
The actual implementation depends on what programming language you're using, but the pseudocode should look somewhat like this:
returnedList = json.find("$.phoneNumbers[?(#.type=='iPhone')].number")
initialize newJsonArray
foreach(returnedValue in returnedList):
newJsonArray.add(newJsonObject("number", returnedValue))
An alternative way to tackle the problem in OOP languages is to use lambda functions to filter your Json objects instead of using Jsonpath filters. An example on how to do it in C# using Newtonsoft.Json:
JArray jArray = new JArray(jObject.SelectToken("$.phoneNumbers").Where(x => x["type"].ToString() == "iPhone"));

How to find the parent keys of a jsonpath result

I have this json obj
{
"abc": {
"some_field": {
"title": "Token",
"type": "password"
},
"some_field_2": {
"title": "Domain",
"type": "text"
},
"some_field_3": {
"title": "token2",
"type": "password"
}
}
}
And I want to get a list of keys [some_field,some_field_3] where type=password
This jsonpath $..[?(#.type=='password')] returns:
[
{
"title": "Token",
"type": "password"
},
{
"title": "token2",
"type": "password"
},
]
What should I do?
Thanks.
You are almost there, just need to add a ~ to the end to get the key(s) instead of the value(s):
$..[?(#.type=='password')]~
Note: this might not work depending on your jsonpath engine. It works on https://jsonpath.com.

Is it possible to have multiple restrictions on the same attribute on proof request on Indy?

We currently have a proof request like this:
{
"name": "pr",
"version": "1.0",
"nonce": "0994650939",
"requested_attributes": {
"attr0_referent": {
"name": "first_name",
"restrictions": [{
"cred_def_id": "credDefIdShareGlobal"
}]
}
},
"requested_predicates": {},
"non_revoked": {}
}
As you can see, on the restricton fields now we have only one restriction. Is it possible to have multiple restrictions on the same attribute (like the example beneath)?
{
"name": "pr",
"version": "1.0",
"nonce": "0994650939",
"requested_attributes": {
"attr0_referent": {
"name": "first_name",
"restrictions": [{
"cred_def_id": "credDefIdShareGlobal1"
}, {
"cred_def_id": "credDefIdShareGlobal2" // <-- Is this possible?
}]
}
},
"requested_predicates": {},
"non_revoked": {}
}
Well, to give some closure to this question, yeah. It works exactly like that.
To give more context, the attribute can be present in different schemas or different credentials and by adding the restrictions as shown they'll be dealt with as an "OR" so, the first one matching will be returned.
If you want to see an test example, I'll advise looking here: https://github.com/eduelias/indy-sdk/blob/MultipleReq/samples/nodejs/src/gettingStarted.js#L470

How to filter by nested attributes in JSONAPI?

Assuming we have the following data structure
"data": [
{
"type": "node--press",
"id": "f04eab99-9174-4d00-bbbe-cdf45056660e",
"attributes": {
"nid": 130,
"uuid": "f04eab99-9174-4d00-bbbe-cdf45056660e",
"title": "TITLE OF NODE",
"revision_translation_affected": true,
"path": {
"alias": "/press/title-of-node",
"pid": 428,
"langcode": "es"
}
...
}
The data returned is compliant with JSON API standards, and I have no problem retrieving and processing it, except for the fact that I need to be able to filter the nodes returned by the path pid.
How can I filter my data by path.pid?
I have tried:
- node-press?filter[path][pid]=428
- node-press?filter[path][pid][value]=428
to no avail
It's not well defined in the filters section of the specification but other parameters such as include describe accessing nested keys with dot-notation. You could try ?filter[path.pid]=428 and parse the filter that way.
"field_country": {
"data": {
"type": "taxonomy_term--country",
"id": "818f11ab-dd9d-406b-b1ca-f79491eedd73"
}
}
Above structure can be filtered by ?filter[field_country.id]=818f11ab-dd9d-406b-b1ca-f79491eedd73

JSONPath first occurrence of "gatherer"

I have a json file like this:
{
"cards": [
{
"artist": "Steve Argyle",
"images": {
"mtgimage": "http://mtgimage.com/set/pMPR/ponder.jpg"
},
},
{
"artist": "Mark Tedin",
"images": {
"gatherer": "http://gatherer.wizards.com/Handlers/Image.ashx?type=card&multiverseid=139512",
"mtgimage": "http://mtgimage.com/set/LRW/ponder.jpg"
},
},
{
"artist": "Dan Scott",
"images": {
"gatherer": "http://gatherer.wizards.com/Handlers/Image.ashx?type=card&multiverseid=190159",
"mtgimage": "http://mtgimage.com/set/M10/ponder.jpg"
},
}
]
}
I would like to get the first "gatherer" link from it using JSONPath.
I tried "$..gatherer[0]" but that doesn't work. However "$..gatherer" does give both instances:
[
"http:\/\/gatherer.wizards.com\/Handlers\/Image.ashx?type=card&multiverseid=139512",
"http:\/\/gatherer.wizards.com\/Handlers\/Image.ashx?type=card&multiverseid=190159"
]
How do I get only the first one? (without getting the last one in code, so only using a jsonpath string.)
(Tested with http://jsonpath.curiousconcept.com/ and in my program.)
You may need an interim step to save the array
var gatherers = $..gatherer; //however this gets it, I'm not sure
var first = gatherers[0];
that may work.

Resources