-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Introduce jsonset operator #7742
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
One operation that will be pretty common, but would be very difficult (not impossible, just convoluted) with this design, is appending one item to a JSON array, i.e. the equivalent of An alternative approach would involve using jsonextract, stripping off the final ], adding a comma and the value desired (as a pre-formatted JSON string) and then putting the final ] back, then passing that in using Either one of these is convoluted. Would it be possible to get a design for Alternately, a new |
Hi @rmunn I was thinking that we'd make a separate |
The rationale for this is because it's quite useful in lots of situations to be able to access the last item of an array without knowing ahead of time how long the array is. (Indexes like -2 are useful a lot less often, but if you've already added the -1 feature than why not allow all negative numbers as indexes?) One place it might be useful, for example, is once a Anyway, I'll create a new issue to track that suggestion, because it would involve changing all the json operators, not just jsonset. (Though if negative indexes are allowed, I'd like them to apply to jsonset as well, naturally.) |
Hi @rmunn I like the idea of negative indices for accessing arrays from the end, and agree that it should be consistently applied to all the JSON operators. A slight tangent, but it is interesting to consider a |
Have come from the pre-release. It is great there is the addition of the JSONset operator. Since 5.3.x we have had the introduction of the parameters widget, we also got the $params=vaname attribute which stores all the parameters ans a JSON array of name/value pairs. Would it be possible to document the relationship and interoperability of the jsonset operator with the array generated by the $params attribute? For may users the parameters widget may be the first time they encounter a JSON array and may be inclined to explore the use of json operators with the parameters array. I ask here, rather than wait to contribute to the documentation because I need a developer to tell me if the above is interoperable (although I suspect it is) Post Script
|
This PR introduces a new
jsonset
operator that can set property values within JSON strings.Properties within a JSON object are identified by a sequence of indexes. In the following example, the value at
[a]
isone
, and the value at[d][f][0]
isfive
.The following examples assume that this JSON data is contained in a variable called
jsondata
.The jsonset operator uses multiple operands to specify the indexes of the property to set. When used to assign strings the final operand is interpreted as the value to assign. For example:
Indexes can be dynamically composed from variables and transclusions:
The data type of the value to be assigned to the property can be specified with an optional suffix:
For example:
A subtlety is that the special case of a single operand sets the value of that operand as the new JSON string, entirely replacing the input object. If that operand is blank, the operation is ignored and no assignment takes place. Thus:
(This PR was cherry picked from #7406)