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

An error occurred when convert empty array to an object in 1.8.5 #547

Closed
hankei6km opened this issue Oct 30, 2021 · 4 comments
Closed

An error occurred when convert empty array to an object in 1.8.5 #547

hankei6km opened this issue Oct 30, 2021 · 4 comments

Comments

@hankei6km
Copy link

I'm trying to convert an array of API responce to an object with JSONata.
in many cases it works very well.
(JSONata is very powerful)

However, I get an error when I convert an empty array in JSONata 1.8.5.
(in 1.7.0 work as expected)

Key in object structure must evaluate to a string; got: undefined

steps to reproduce

I tried it with JSONata Exerciser.

array to object

It works in the following cases.

https://try.jsonata.org/uvEJnSWm-

jsonata: $${id:{"label":label,"value":value}}

json:

[
  {
    "id": "abc",
    "label": "ABC",
    "value": 123
  },
  {
    "id": "def",
    "label": "DEF",
    "value": 321
  }
]

result:

{
  "abc": {
    "label": "ABC",
    "value": 123
  },
  "def": {
    "label": "DEF",
    "value": 321
  }
}

empty array

An error occurred when json was an empty array.
https://try.jsonata.org/r9UY9cPmx
(in 1.7.0 work as expected)

json:

[]

result (1.8.5):

Key in object structure must evaluate to a string; got: undefined

result (1.7.0):

{}

workaround

The following query works even with an empty array

$count($$) > 0 ? $${id:{"label":label,"value":value}} : {}
@hankei6km
Copy link
Author

Additional Information.

In "1.8.0 - 1.8.4" worked as expected.

steps to reproduce

$ node --version
v14.18.1
import jsonata from 'jsonata';

const data = [];
try {
  const expression = jsonata('$${id:{"label":label,"value":value}}');
  console.log(
    `result => ${JSON.stringify(expression.evaluate(data), null, '  ')}`
  );
} catch (err) {
  console.log(`err => ${err.message}`);
}

1.8.0 - 1.8.4

result => {}

1.8.5

err => Key in object structure must evaluate to a string; got: undefined

kexuibm added a commit to kexuibm/jsonata that referenced this issue Dec 10, 2021
kexuibm added a commit to kexuibm/jsonata that referenced this issue Jan 4, 2022
kexuibm added a commit to kexuibm/jsonata that referenced this issue Jan 4, 2022
kexuibm added a commit to kexuibm/jsonata that referenced this issue Jan 5, 2022
kexuibm added a commit to kexuibm/jsonata that referenced this issue Jan 5, 2022
andrew-coleman pushed a commit that referenced this issue Jan 6, 2022
* Fixing reported issue #547

* Fixing reported issue #547.  Added unit test coverage.

* Fixing reported issue #547.  Added unit test coverage.

* Fixing reported issue #547.  Added unit test coverage.

* Fixing reported issue #547.  Added unit test coverage.
@andrew-coleman
Copy link
Member

Resolved by #555

@asampal
Copy link

asampal commented Feb 4, 2022

Doesn't this warrant a new release?

@mattbaileyuk
Copy link
Member

@asampal 1.8.6 was released today with this fix (among others)

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

No branches or pull requests

4 participants