schibsted / jslt

JSON query and transformation language
Apache License 2.0
636 stars 120 forks source link

basic operations on objects and arrays #189

Open razibus opened 3 years ago

razibus commented 3 years ago

I have not found how to do basic operations on arrays and objects like :

The * : . operator is rejected inside a function. It would have been useful to define it relatively to the current context to provide unmatched keys/values. For instance:

def make1(data)
{
"v1": number($data.v[0]),
  * : .
}

or

def make2(data)
{
"v1": number($data.v[0]),
  for($data)
     .key: .value
}

None are accepted. It would have been a great help to get a local semantic of the * : . operator relatively to the data variable.

larsga commented 3 years ago

I'd be happy to help you with this, but most of these questions are underspecified, so I need to ask you questions to clarify each. And with four (at least) questions in a single issue I fear this quickly gets messy.

Could I ask you to break this into the following issues?

larsga commented 3 years ago

The object matching issue is easy to solve:

def make1(data)
  $data | {
"v1": number(.v[0]),
  * : .
}

The | sets the context node so that you can do object matching on it. The .v dot access is also relative to the context node.