Hard to understand JSONPath containing [?] - json

In a legacy system I found this JSONPath :
$['data']..['rels'][?]['persons'][*]
I am confused by [?],
as I am trying to rebuild the expected JSON that should by like (but it's not) :
{
"tag1": ["VALUE1"],
"data": [
{
"tag2": "VALUE2",
"rels": [
{
"persons": [
{
"uid": "uid12"
}
],
"tag3": {
"tag4": "tag4"
}
}
]
}
]
}
I used https://jsonpath.com/
Thanks for your help

You're right to be confused by this. It's not valid JSON Path syntax. It's an extension syntax supported by Jayway JsonPath (a JavaScript implementation) that they call "Filter Predicates." You can read about it in the project's readme.
I and a few others have been working on writing an official specification for JSON Path, and I brought this up recently after having seen another question around it. This kind of confusion is something we aim to avoid.

Related

JSONPath to get multiple values from nested json

I have a nested JSON in a field that contains multiple important keys that I would like to retrieve as an array:
{
"tasks": [
{
"id": "task_1",
"name": "task_1_name",
"assignees": [
{
"id": "assignee_1",
"name": "assignee_1_name"
}
]
},
{
"id": "task_2",
"name": "task_2_name",
"assignees": [
{
"id": "assignee_2",
"name": "assignee_2_name"
},
{
"id": "assignee_3",
"name": "assignee_3_name"
}
]}]}
All the queries that I've tried so far fx ( $.tasks.*.assignees..id) and many others have returned
[
"assignee_1",
"assignee_2",
"assignee_3"
]
But what I need is:
[
["assignee_1"],
["assignee_2", "assignee_3"]
]
Is it possible to do with JSONPath or any script inside of it, without involving 3rd party tools?
The problem you're facing is that tasks and assignees are arrays. You need to use [*] instead of .* to get the items in the array. So your path should look like
$.tasks[*].assignees[*].id
You can try it at https://json-everything.net/json-path.
NOTE The output from my site will give you both the value and its location within the original document.
Edit
(I didn't read the whole thing :) )
You're not going to be able to get
[
["assignee_1"],
["assignee_2", "assignee_3"]
]
because, as #Tomalak mentioned, JSON Path is a query language. It's going to remove all structure and return only values.

Proper validation of a referenced file when editing OpenAPI json in IntelliJ

I'm editing an OpenAPI JSON spec in IntelliJ. The automatic validation and code completion work very nicely.
The OpenAPI version used is 3.0.3, which IntelliJ detects correctly. It seems that it uses "openapi30.json" internally for validation, and all is good.
However, the file is getting very large and it's time to move some commonly-used models out of it using $ref.
This is where things break. The main spec looks like this (snippet):
{
"openapi": "3.0.3",
"info": {
"title": "Cars REST API",
"description": "Calls, Responses and DTOs for REST",
"version": "1.0.0"
},
"components": {
"schemas": {
"car": {
"$ref": "car.json"
},
"car-group": {
"$ref": "car-group.json"
}
And when editing it, IntelliJ recognizes it as "openapi30".
However, the referenced documents are not recognized. For example, the car.json file looks like this:
{
"car": {
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
}
}
And it's recognized simply as a JSON document, not an OpenAPI one, so there is no proper validation and no code completion, etc.
How does one tell IntelliJ that the file is part of an OpenAPI specification, to be validated as such? One should think this could be inferred from begin $ref'ed from the main spec, but this doesn't work.
Trying to add a $schema value in the referenced file had no effect (and probably isn't in line with the OpenAPI spec anyway).
Manually selecting the OpenAPI 3.0 file type for car.json is not helpful, because then validation (rightly) fails - as it doesn't have the top-level structure required (info, openapi, paths).
Perhaps some specific JSON schema mapping needs to be added in IntelliJ preferences? If that's the case, it would be actually a sub-schema or some tag in the main OpenAPI spec, how can that be done?
IntelliJ version is: IntelliJ IDEA 2021.3.2 (Ultimate Edition)
Any help would be greatly appreciated.
Ron!
Such functionality is not yet supported. Please vote for https://youtrack.jetbrains.com/issue/IDEA-284305

JsonPath filter query syntax

I'm trying to write a JsonPath query that selects a specific object based on a condition, but either the syntax fails me or I fail the syntax.
Given the below Json object, how would I select the "Data" object containing Dirk Gently's details based on the fact that he uses the "Stumble" method?
{
"Investigators": [
{
"Type": "Legend",
"Object": {
"Method": "Investigate",
"Data": {
"Name": "Sherlock",
"Surname": "Holmes"
}
}
},
{
"Type": "Visionary",
"Object": {
"Method": "Stumble",
"Data": {
"Name": "Dirk",
"Surname": "Gently"
}
}
}
],
"Version": 1
}
I know that I can get to the Method-field like this:
$.Investigators..Object.Method
I assumed that something like this would work:
$.Investigators..Object[?(#.Method=="Stumble")].Data
I'm testing this using: https://jsonpath.com/ to evaluate the query - and I can't seem to get it right.
Am I trying to do something that is not achievable or am I just making a stupid mistake?
Go to this jsonpath online sandbox and try this experssion:
$.Investigators..Object[?(#.Method=="Stumble")].Data
using either the Jayway or Gatling implementations, the output will be:
[
{
"Name" : "Dirk",
"Surname" : "Gently"
}
]

How can I do a UML Schema from a JSON file?

Hello I have this JSON file :
{
"Hello": "",
"Olá": [
{
"Hour": "00:04:35",
"hotel": [""],
}
]
"Ciao" : [
{
"Hour": "02:36:52"
}
]
}
And actually I am trying to convert this JSON file to an UML file.
Could you help me please ? :)
I thought of this :
Solution
You may want to take a look at JSONDiscoverer http://som-research.uoc.edu/tools/jsonDiscoverer/#/
(the site includes also the technical details on how the UML model is created)
gliffy diagrams can do it, actually is the original format to save files

PATCH request for SCIM 2.0

We are sending PATCH request to a server in SCIM specification.
As per the SCIM specifications, the request should contain following attributes in PATCH request.
op
path
value
So if we are changing the 'givenName' attribute from core schema then the PATCH request will be in following way, (ref: https://www.rfc-editor.org/rfc/rfc7644#section-3.5.2)
{
"schemas" : ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
"Operations":[
{
"op":"replace",
"path":"name.givenName",
"value":"Ravindra"
}
]
}
Now what should be the 'path' attribute if are modifying any SCIM extension, let's say enterprise extension.
Is the following representation is correct for enterprise extension?
{
"schemas" : ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
"Operations":[
{
"op":"replace",
"path":"urn:ietf:params:scim:schemas:extension:enterprise:2.0:user.department",
"value":"Engineering"
}
]
}
As in the ABNF to which scim filters should adhere (see section 3.4.2.2 of RFC 7644), when you refer to an attribute part of an extension you should do URI:attribute_path, so in your case this is "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:department"
I was getting a similar error when attempting to PATCH the "active" value of an enterprise user. The solution is simple: change your "path" value in the example above to simply "department".
For completeness, here's the PATCH body that worked for me in Postman:
{
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:PatchOp"
],
"Operations": [
{
"op":"replace",
"path":"active",
"value":"false"
}
]
}
Attribute '.Operations.[].value' must be of type hash
{
"schemas": [
"urn:ietf:params:scim:schemas:core:2.0:User"
],
"Operations": [
{
"op": "replace",
"value": {
"active": false
}
}
]
}
Hope this will work!!