Comments (9)
So, you want the output to be [1, 2, 3]
? Just checking that I've got the idea right.
from jslt.
yes, and just for completeness
{
"z": .a.b | {
"y": .,
"x":.
}
}
should give { "z": {"y": 1, "x": 1}}
from jslt.
Yes, I agree. After the pipe comes any expression you want, with .
now meaning the value you computed before the |
.
I've been seriously considering this addition to the language for a long time, so I think we'll just do this one.
from jslt.
After some pondering I think I know how this should work https://github.com/schibsted/jslt/blob/master/ideas/matcher.md
from jslt.
Doesn't the part
Object key: add the key to the matcher query. So if the matcher is inside "foo" : then prepend .foo to the query.
make a non-backwards compatible change to the language?
Imagine that I had a pre-existing JSLT file:
{ "foo": .bar }
and a input value
{
"foo": {
"bar": 1
},
"bar": 2
}
With JSLT syntax as it is today that JSLT + input will produce output {"foo": 2}
, and with the proposed change it will produce {"foo": 1}
, if I understood it right.
from jslt.
No, that's part of how object matching in JSLT has worked all along, although it may not be obvious. That's why if you write:
{
"foo" : {
"bar" : 2,
* : .
}
}
the object matcher will match the object in the foo
key in the context object.
The object matcher is the * : .
part, not .bar
. I guess that's where the confusion comes from.
from jslt.
I've found a case where I really need this and dynamic keys (issue #56). I want to take an object, remove the "item"
key, and replace it with a dynamically computed key. Right now I have to do this:
"parameters" : [{for ($json.customParameters)
.key : .value if (.key != "item")
} +
// workaround for absence of dynamic keys (issue #56)
{for ([[
(if (is-string($item))
"stringItem"
else
""), $item]])
.[0] : .[1]
}]
It's ugly as sin. But with the pipe and dynamic keys it becomes:
"parameters" : [$json.customParameters | {
(if (is-string($item))
"stringItem"
else
"") : $item,
* - "item" : .
}]
Clearly this is what we need.
from jslt.
We haven't discussed the operator precedence and associativity or how the interaction with the for-expression, object-for-expresson and if-expression should look like.
I've prepared a PR #109 in which I implemented a preliminary solution.
In that PR pipe operator is implemented has having highest precedence so
1 + 2 | (. * 5)
equals 11
as its interpreted as 1 + (2 | (. * 5))
which reduces to 1 + ((2 * 5))
.
To be honest I think it feels like it should have lower precedence than the arithmetic operators so that 1 + 2 | (. * 5)
equals (1 + 2) | (. * 5)
-> 3 | (. * 5)
-> 15
. But not lower than and
/or
operators so that true or false | { "a" : .}
evaluates to true
and not to {"a": true}
. @larsga what do you think?
Then associativity I assume it should be left-associative so that 1 | (. *2) | ( . + 3) | (. * 5)
is interpreted as 1 | (. * 2)
evaluated first and the result feeded to | (. +3)
and so on.
I can prepare other test with {if-expressions, for-expressions, object-for-expressions} on both sides of the operator if the PR is inline with what you had in mind @larsga
from jslt.
Just for future reference: at the end we didn't implement the operator precedence as described in previous comment. We made pipe operator the lowest precedence, so that means lower than and
/ or
operators.
from jslt.
Related Issues (20)
- Workaround to include null values when no API access? HOT 2
- Parser error when adding a new row after else statement HOT 6
- Object matcher preserves `null` HOT 4
- JSLT has working with this array HOT 3
- Format the given time from UTC/GMT to ET (including EST & EDT) HOT 3
- preserve null objects and empty arrays HOT 2
- Passing value from DB to JSLT node HOT 8
- Read the JSON from DB and convert to a different JSON format using JSLT HOT 1
- Obtaining the complete path / pointer in current context HOT 3
- Have multiple Json input files that needs to be transformed to one file HOT 2
- When using jslt function number() and passing string with big decimal for eg "180000000000" returns scientifc/exponential notation HOT 13
- if-expression leads to "parse error" if it is last line in header HOT 10
- jslt for loop capability HOT 6
- Is possible to check de last four characters of the current index of the array and group by if the next (index+1) of the array of the four characters is different? HOT 4
- Problem with nested attributes HOT 3
- How to convert JSON usin JSLT HOT 1
- Precision issue when sum array HOT 7
- generation of JSLT
- Wanted to replace convert the given input to a JSON and get a particular value HOT 2
- I wanted to convert the given input to JSON HOT 13
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jslt.