JSONPath first occurrence of "gatherer" - jsonpath

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.

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"));

JSONPath and regex: filter to search end substring

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

Is there a way to find the row information in response result finding of Google DLP?

I am sending a multiple sentence at a time as a table and using inspect_content to find personal information for each sentences.
When I use the inspect_content function, I could receive a series of findings with various information.
However, I could not find which row the each inspect result came from.
I have to deliver both original text and the location range of each target info types.
Thank you for your precious time ^^
enter image description here
There is a rowIndex for inspect content.
Example request
{"item":{"table":{"headers":[{"name":"column1"}],"rows":[{"values":[{"stringValue":"not an email"}]},{"values":[{"stringValue":"test#gmail.com"}]}]}}}
Example response (see the table location)
{
"result": {
"findings": [
{
"infoType": {
"name": "EMAIL_ADDRESS"
},
"likelihood": "LIKELY",
"location": {
"byteRange": {
"end": "14"
},
"codepointRange": {
"end": "14"
},
"contentLocations": [
{
"recordLocation": {
"fieldId": {
"name": "column1"
},
"tableLocation": {
"rowIndex": "1"
}
}
}
]
},
"createTime": "2022-01-05T18:15:04.413Z",
"findingId": "2022-01-05T18:15:04.422252Z312288285164444315"
}
]
}
}

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

Forcing the emitting of the allOf section when using Newtonsoft.Json.Schema.Generation.JSchemaGenerator

I can successfully use the Newtonsoft.Json.Schema.Generation.JSchemaGenerator to generate valid JSONSchema for a given class. This works fine, however, a third party consumer requires that it has a
"allOf": [ { "$ref": "#/definitions/ClassName" } ]
block of code in the emitted JSON Schema.
I can currently get the output to appear as
{
"$id": "https://xxxxx/classname",
"definitions": {
"ClassName": {
"type": "object",
"properties": {
but I need it to look like
{
"$id": "https://xxxxx/classname",
"allOf": [
{
"$ref": "#/definitions/ClassName"
}
],
"definitions": {
"ClassName": {
"type": "object",
"properties": {
Does anybody know what I need to do to achieve this? There is only one class being referenced.
Currently, I have the generator using code like this:
var _jsonSchemaGenerator = new JSchemaGenerator();
_jsonSchemaGenerator.SchemaIdGenerationHandling = SchemaIdGenerationHandling.None;
_jsonSchemaGenerator.SchemaLocationHandling = SchemaLocationHandling.Definitions;
var schema = _jsonSchemaGenerator.Generate(typeof(T));
console.WriteLine(schema);
Any help would be appreciated.

Resources