JSON path evaluation inside JSON path expression - jsonpath

I've got this very simple json :
{
"authors": [
{
"id": 1,
"name": "Douglas Adams"
},
{
"id": 2,
"name": "John Doe"
}
],
"books": [
{
"name": "The Hitchhiker's Guide to the Galaxy",
"author_id": 1
}
]
}
I would like to request the name of the author of "The Hitchhiker's Guide to the Galaxy".
I've tried this JSON path but it doesn't work:
$.authors[?(#.id == $.books[?(#.name == "The Hitchhiker's Guide to the Galaxy")].author_id)].name
All online tools I tried indicate a syntax error which seems due to the presence of a JSON path inside my filter.
Could anyone please help me figure out what's wrong and what is the right syntax?
Thanks!

When you running this filter
$.books[?(#.name == "The Hitchhiker's Guide to the Galaxy")].author_id
it returns an array instead of a value:
[
1
]
Syntax error occurs when you pass an array to compare with the value of id:
$.authors[?(#.id == {the array value}].author_id)].name
However, you may not be able to extract the value using JSONPath, depends on the language you are using. See Getting a single value from a JSON object using JSONPath

Related

Can't get the desired properties via JsonPath evaluate method

I have a json schema that marks special properties in need of processing and I want to query those via JsonPath.Evaluate.
Here's a part of the schema to illustrate the issue
{
"type": "object",
"properties": {
"period": {
"description": "The period in which the rule applies",
"type": "object",
"properties": {
"start": {
"type": "string",
"format": "date-time"
},
"end": {
"type": "string",
"format": "date-time"
}
},
"required": [
"start"
],
"x-updateIndicatorProperties": [
"start"
]
},
"productType": {
"type": "string"
},
"x-updateIndicatorProperties": [
"productType"
]
}
}
I want to get the the JsonPath of the "x-updateIndicatorProperties" properties, so that I can then query the actual properties to process.
For this example, the expected result would be
[
"$['properties']['x-updateIndicatorProperties']",
"$['properties']['period']['x-updateIndicatorProperties']"
]
I've been trying for a while to get a JsonPath expression that would query these properties.
Currently I'm just iterating all properties and filter them manually :
"$..*"
I've also tried using :
$..['x-updateIndicatorProperties']
This works. But it returns a lot of duplicates. For the example above, I get 5 results instead of the expected 2. Can be demonstrated here : https://json-everything.net/json-path
Assuming I can't influence the schema itself, only the code that traverses it,
can anybody help with an expression to get the expected results or any other way to achieve the same outcome?
The stack is JsonPath 0.2.0, .net 6 and system.text.json.
This was a bug in the library when parsing paths that use a recursive descent (..) into a quoted-property-name selector (['foo']). So it would happen for any path in the form $..['foo'].
I've fixed the issue and released version 0.2.1.

AWS Step Functions: Filter an array using JsonPath

I need to filter an array in my AWS Step Functions state. This seems like something I should easily be able to achieve with JsonPath but I am struggling for some reason.
The state I want to process looks like this:
{
"items": [
{
"id": "A"
},
{
"id": "B"
},
{
"id": "C"
}
]
}
I want to filter this array by removing entries for which id is not in a specified whitelist.
To do this, I define a Pass state in the following way:
"ApplyFilter": {
"Type": "Pass",
"ResultPath": "$.items",
"InputPath": "$.items.[?(#.id in ['A'])]",
"Next": "MapDeployments"
}
This makes use of the JsonPath in operator.
Unfortunately when I execute the state machine I receive an error:
{
"error": "States.Runtime",
"cause": "An error occurred while executing the state 'ApplyFilter' (entered at the event id #8). Invalid path '$.items.[?(#.id in ['A'])]' : com.jayway.jsonpath.InvalidPathException: com.jayway.jsonpath.InvalidPathException: Space not allowed in path"
}
However, I don't understand what is incorrect with the syntax. When I test here everything works correctly.
What is wrong with what I have done? Is there another way of achieving this sort of filter using JsonPath?
According to the official AWS docs for Step Functions,
The following in paths are not supported # .. , : ? *
https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-paths.html

Correct invalid json for use with Json.Net

I have some JSON which I have no control over it's from a third-party supplier and the quotes are not handled properly resulting in malformed JSON. I have asked them to correct it but in the meantime, I would like to be able to use it.
var json = "{
"news": {
"headline": "Headline",
"items: [
{
"title": "title1",
"description": "description1",
},
{
"title": "title2",
"description": "description2",
},
{
"title": "title3",
"description": "description "with quotes" in the middle",
},
]
}
}";
I am trying to use DeserializeObject with it
var obj = JsonConvert.DeserializeObject<MyClass>(json);
Ideally, I would like all three items in my deserialised object, but even two would be better than the DeserializeObject just blowing up because the JSON is badly formatted.
Is there a possible correction which can be applied? I have looked at regexes but it's difficult to come up with something that could work with long and complex examples with many more items than this simplified version.

Writing specs for grammar in atom editor

I have written my own grammar in atom. I would like to write some specs for the same, but I am unable to understand how exactly to write one. I read the Jasmine documentation, but still not very clear. Can someone please explain how to write specs for testing out grammar in atom. Thanks
Grammars are availabe available under atom.grammars.grammarForScopeName("source.yourlanguage")
The grammar object it returns has methods you can feed code snippets (e.g. tokenizeLine, tokenizeLines).
These methods return arrays of tokens.
Testing is just verifying if these methods return what you expect.
E.g. (CoffeeScript alert):
grammar = atom.grammars.grammarForScopeName("source.yourlanguage")
{tokens} = grammar.tokenizeLine("# this is a comment line of some sort")
expect(tokens[0].value).toEqual "#"
expect(tokens[0].scopes).toEqual [
"source.yourlanguage",
"comment.line.number-sign.yourlanguage",
"punctuation.definition.comment.yourlanguage"
]
Happy testing!
Example specs
spec for MscGen (a simple language)
spec for Haskell (more complex)
The array returned by the grammar.tokenizeLine call above looks like this:
[
{
"value": "#",
"scopes": [
"source.yourlanguage",
"comment.line.number-sign.yourlanguage",
"punctuation.definition.comment.yourlanguage"
]
},
{
"value": " this is a comment line of some sort",
"scopes": [
"source.yourlanguage",
"comment.line.number-sign.yourlanguage"
]
},
{
"value": "",
"scopes": [
"source.yourlanguage",
"comment.line.number-sign.yourlanguage"
]
}
]
(Kept seeing this question pop up in the search results when I was looking for an answer to the same question - so just as well document it here.)

Serialized Entities displaying only ID

I'm using JMSSerializer and FOSRestBundle. I have a fairly typical object graph, including some recursion.
What I would like to accomplish is that included objects beyond a certain depth or in general are listed only with their ID, but when serialized directly, with all data.
So, for example:
Users => Groups => Users
when requesting /user/1 the result should be something like
{ "id": 1, "name": "John Doe", "groups": [ { "id": 10 }, { "id": 11 } ] }
While when I request /group/10 it would be:
{ "id": 10, "name": "Groupies", "users": [ { "id": 1 }, { "id": 2 }, { "id": 4 } ] }
With #MaxDeph I can hide the included arrays completely, so I get
{ "id": 1, "name": "John Doe", "groups": [] }
But I would like to include just the IDs so that the REST client can fetch them if it needs them, or consult his cache, or do whatever.
I know I can manually cobble this together using groups, but for consistency reasons I was wondering if I can somehow enable this behaviour in my entire application, maybe even with a reference to maxdepth so I can control where to include IDs and where to include full objects?
For the sake of those finding this:
I found no other solution, but doing this with groups works just fine and gives me the result I was looking for.

Resources