Closed mitar closed 2 months ago
There is a regex option which should allow matches on substrings of an element. Modifying the returned string is really out of scope and easily done in go. What did you have in mind?
I am trying to do something similar to JSON Patch where I want to describe with a JSON object a change to another JSON object. But I was thinking of using JSON Path instead of JSON Pointer the JSON Patch is using to be more expressive. While at it, I would also like to be able to express patching part of the string, instead of having to repeat the whole string (like what JSON Patch also requires). This is why I came to this issue, because I am using ojg for JSON manipulation.
This issue is about extracting substrings, because I have yet to figure out how to use ojg to set values at a particular JSON Path in JSON.
Anyway, just some thinking. I can completely understand if this would be out of scope of this project. Or it might be possible to do it with some custom JSON Path function, not sure.
I think I understand what you are trying to do. Let me describe it an see if I got it right. A patch would first identify a string element and then replace a portion of that string. Put another way, find a string matching a regex and apply some function to generate a replacement for the string element.
It would not have to be a regex, simple start/end offsets would be enough. And it would just be a static replacement string. So it is about storing a patch in JSON. This is why "extract substring in Go" would not be reasonable. Unless I describe it using two fields, something like:
{
"op": "replace",
"path": "$.post.description",
"substr": [1,4],
"value": "new value"
}
But ideally I could do:
{
"op": "replace",
"path": "$.post.description[1:4]",
"value": "new value"
}
So the steps would be:
Sound about right?
Yes.
Step 1 can be handled with the existing JSONPath regex comparator. Step 2 can implemented with a Set after a Get and creation of the replacement.
So it is possible but not as a single call which I think is what you are asking for, right?
Hm, I can probably transform offsets into regex, you are right. But it is a bit verbose. But it could do.
No, multiple calls are OK, this would then be internal implementation detail how patches are applied.
Thanks.
The regex shouldn't be too verbose. ".{5}abc" would match x[5:8] == 'abc'
using go code.
I came to the need that I would want to extract a substring from a string inside a JSON based on character offsets. From my understanding, JSON Path standard does not support this. But it could be a natural extension to see strings as arrays (like they are in JavaScript and Go and many languages) and allows indexing and slicing into strings. Would that be something you would entertain in this package?