Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

directive support #520

Open
He-Pin opened this issue Apr 30, 2024 · 9 comments
Open

directive support #520

He-Pin opened this issue Apr 30, 2024 · 9 comments

Comments

@He-Pin
Copy link

He-Pin commented Apr 30, 2024

Backgroud:
I need to do some translation of a dedicated string field from Chinese to other languages, but some fields live inside a internal json object string( a json object, but a jsonString type`.

{
  "data": {
    "header_1": {
      "a": "1",
      "b": "2",
      "body": "{\"c\":\"3\"}"
    },
    "header_2": {
      "a": "1",
      "b": "2",
      "body": "{\"c\":\"3\"}"
    }
  }
}

With the current java jsonpath:

//to json
{"data":{"header_1":{"a":"1","b":"2","body":{"c":"3"}},"header_2":{"a":"1","b":"2","body":{"c":"3"}}}}
// update `c`
{"data":{"header_1":{"a":"1","b":"2","body":{"c":"6666"}},"header_2":{"a":"1","b":"2","body":{"c":"6666"}}}}
//conver back
{"data":{"header_1":{"a":"1","b":"2","body":"{\"c\":\"6666\"}"},"header_2":{"a":"1","b":"2","body":"{\"c\":\"6666\"}"}}}

I more like to do this in a one run. eg:

$.data[/header_\d+/].body[@toJson()].c[@translate()]

Just like the GraphQL's directives https://www.apollographql.com/docs/apollo-server/schema/directives/#custom-directives

@gregsdennis
Copy link
Collaborator

As I mentioned in #516, I don't think JSON Path is the right tool for you.

Also, have a look at the conversation in #514 as to why mutation of data, even internally, isn't likely to happen.

@He-Pin
Copy link
Author

He-Pin commented Apr 30, 2024

I just implemented it internally, but this would be a nice addition, graphql is a query language so does jsonnet, I think it's pretty valid to extends it with directives, which operates on the selected nodes, and the default behavior is identity function.

I took this from graphql , jaywalking Jason path have a pathfunction which let you do this too.

And it has a update and set method.

@gregsdennis
Copy link
Collaborator

graphql is a query language so does jsonnet

I'm quite familiar with both of these.

These languages can query, but they can also do a lot more, including mutation. JSON Path is only a query language. It does not mutate. It's read-only.

@He-Pin
Copy link
Author

He-Pin commented Apr 30, 2024

graphql is a query language so does jsonnet

I'm quite familiar with both of these.

These languages can query, but they can also do a lot more, including mutation. JSON Path is only a query language. It does not mutate. It's read-only.

It's not read only, I implement one and still use it at work, directive can modify the node , as @uppercase

@gregsdennis
Copy link
Collaborator

You misunderstand.

JSON Path, per the original idea and the specification, is definitely read-only. Any behavior that you might add to your implementation is extension behavior and not compliant with the spec. As such it should be disabled by default.

@He-Pin
Copy link
Author

He-Pin commented Apr 30, 2024

You misunderstand.

JSON Path, per the original idea and the specification, is definitely read-only. Any behavior that you might add to your implementation is extension behavior and not compliant with the spec. As such it should be disabled by default.

Yes and no, with the directive, the implementation can achieve me and still fulfill the spec.

@gregsdennis
Copy link
Collaborator

Part of implementing the specification is failing when it's supposed to. If you support extra syntax, it won't fail.

@He-Pin
Copy link
Author

He-Pin commented May 10, 2024

If jsonpath support directive/function and | like jq which will be very powerful.

@gregsdennis
Copy link
Collaborator

Ideally something like jq, which uses the "path" term but does not claim to use JSON Path, could update to use JSON Path and build upon it.

This concept is similar to #514 in that it adds a new operating mechanic, and you're welcome to read the reception that idea had.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants