Skip to content

Clarification on JSONata handling of (nested) singleton arrays #93

@tomklapiscak

Description

@tomklapiscak

I am working on a Java implementation of the JSONata engine, and I'm struggling to understand JSONata's treatment of singleton arrays...

From http://docs.jsonata.org/complex.html:

Within a JSONata expression or subexpression, any value (which is not itself an array) and an array containing just that value are deemed to be equivalent.

Firstly, if so - why does the following hold?

[1] = 1 => false (should this not be true?)

So, outside of the '=' operator itself, I suspect the singleton/primitive equivalence is effectively achieved by "unwrapping" singleton arrays as they are referenced by expressions.

I see the following behaviour:

{'a': 1 }.a => 1
{'a': [1] }.a => 1
{'a': [[1]] }.a => 1
{'a': [[[1]]] }.a => [1]
{'a': [[[[1]]]] }.a => [[1]]

so it seems to me that JSONata is applying (at most) two levels of flattening to nested singleton arrays pulled out of an associative array via a path. Why 2? Why does it not completely flatten the output array in cases like this?

Next, if I wrap the statements above in an array constructor, I see the the following:

[{'a': 1 }.a] => [1]
[{'a': [1] }.a]=> [1]
[{'a': [[1]] }.a] => [1]

these results seem reasonable (given that the embedded statement returns 1 in those cases), however:

[{'a': [[[1]]] }.a] => [1]

should this not be [[1]]? (given than the statement inside the array constructor evaluates to [1]?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions