Skip to content
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

Blocks does not work with nested array structure #332

Closed
yahelnachum opened this issue May 17, 2019 · 6 comments
Closed

Blocks does not work with nested array structure #332

yahelnachum opened this issue May 17, 2019 · 6 comments
Labels

Comments

@yahelnachum
Copy link

yahelnachum commented May 17, 2019

Summary

In the jsonata playground I was trying out parenthesized expressions and blocks and came across some inconsistent output. I used blocks to turn a selection into an array so that I could pick a single index. Then when I tried adding a key to step into that single index it seemed to revert back to a selection.

More Details

JSON Input 1:

[
  {
    "phone": [
      {"number" : 0}
    ]
  },
  {
    "phone": [
      {"number" : 1}
    ]
  },
  {
    "phone": [
      {"number" : 2}    
    ]
  }
]

Then I tried a series of JSONata expressions


Expression 1:

phone

Actual Output 1:

[
  {"number":0},
  {"number":1},
  {"number":2}
]

Expression 2:

(phone)[0]

Actual Output 2:

{
  "number": 0
}

Expression 3:

(phone)[0].number

Actual Output 3:

[
  0,
  1,
  2
]

I would have expected the output of Expression 3 to be the following given that I was able to select a single index in Expression 2.

Expected Output 3:

0

This seems to me like a bug in the code somewhere, but if there is an explanation for this behavior then please let me know what that is. Thanks in advance for reading!

@yahelnachum yahelnachum changed the title Flattening does not work with nested array structure Blocks does not work with nested array structure May 17, 2019
@yahelnachum
Copy link
Author

yahelnachum commented May 17, 2019

My co-worker was trying some other inputs and got the expected behavior by wrapping my JSON input 1 inside of a JSON Object. I don't know why the code would treat those two cases differently.

JSON input 2:

{
  "account": [
    {
      "phone": [
        {"number" : 0}
      ]
    },
    {
      "phone": [
        {"number" : 1}
      ]
    },
    {
      "phone": [
        {"number" : 2}    
      ]
    }
  ]
}

Expression 4:

(account.phone)[0].number

Actual Output 4:

0

@yahelnachum
Copy link
Author

Just tried another input with a nested array with the same Expression 3 and it worked this time. Still not sure why its behaving differently.

JSON input 3:

[
  [
    {
      "phone": [
        {"number" : 0}
      ]
    },
    {
      "phone": [
        {"number" : 1}
      ]
    },
    {
      "phone": [
        {"number" : 2}    
      ]
    }
  ]
]

Expression 5:

(phone)[0].number

Actual Output 5:

0

@andrew-coleman
Copy link
Member

Yes, the result of your expression 3 looks like a bug.

@andrew-coleman
Copy link
Member

@yahelnachum - because the input data is an array, to access the first item, you can use $[0]. So your expression 3 could be rewritten as

$[0].phone.number

which will return

0

@andrew-coleman
Copy link
Member

This issue is related to how JSONata handles input arrays. There is a lengthy discussion in #170 on this topic. I'm currently prototyping a solution that uses explicit syntax to determine whether to map over the input array verses just use it as a single context value.

@andrew-coleman
Copy link
Member

resolved by #363

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants