question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected properties added to array result

See original GitHub issue

When evaluating the expression payload[true][] against { "payload": ["one"] } the resulting array has some unexpected properties added to it: [ 'one', sequence: true, keepSingleton: true ].

var jsonata = require("jsonata");
var data = { payload: ["one"]}
var exp1 = jsonata("payload[true][]")

exp1.evaluate(data)
=> [ 'one', sequence: true, keepSingleton: true ]

This is the root cause of an issue raised against Node-RED: https://github.com/node-red/node-red/issues/2700 where the output of this expression was passed to another expression, and the result differed to when they passed what appeared to be the same payload straight to the second expression.

The Node-RED debug sidebar fails to display array properties, so they couldn’t see the difference in the messages - that’s a separate issue - but the expectation was the result would be the vanilla array without any extra properties on it.

Is that a reasonable expectation?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
knollearycommented, Sep 18, 2020

Here is a test case for the ‘second’ part of the issue.

var jsonata = require("jsonata");

var exp = jsonata("(payload)")

var data1 = { payload: ["one"]}

var data2 = { payload: ["one"]}
data2.payload.sequence = true;

var data3 = { payload: ["one"]}
data3.payload.foo = true;


console.log(exp.evaluate(data1));
// => [ "one" ]
console.log(exp.evaluate(data2));
// => "one"
console.log(exp.evaluate(data3));
// => [ 'one', foo: true ]

In summary, if JSONata is given an array with a sequence property already set (data2 in the above example), then it treats it differently to one without that property set. This is specific to the property named sequence as it has special meaning in the internals of JSONata. (Note how data3 is handled which has a property named foo).

I would suggest that if a fix for the original issue was found that removed the need to modify the array, then it would likely also mean it would no longer be looking for a sequence property and thus this issue would also get resolved with the same fix.

1reaction
janvdacommented, Sep 18, 2020

@andrew-coleman here below the case for which I reported https://github.com/node-red/node-red/issues/2700

var jsonata = require("jsonata")
var data = { payload: ["one"]}
var exp1 = jsonata("{ 'payload' : payload[true][]}")  // first expression
var data1 = exp1.evaluate(data)
var exp2 = jsonata("(payload)[]")    // second expression
var data2 = exp2.evaluate(data1)

var data3 = exp2.evaluate(data) 

console.log(data,data1,data2,data3)

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can I add named properties to an array as if it were an ...
Arrays are for numerically indexed data - for non-numeric keys, use an Object. Here's a more concrete example why non-numeric keys don't "fit"...
Read more >
for...in - JavaScript | MDN - MDN Web Docs
A for...in loop only iterates over enumerable, non-symbol properties. Objects created from built–in constructors like Array and Object have ...
Read more >
JavaScript Arrays - W3Schools
If you use named indexes, JavaScript will redefine the array to an object. After that, some array methods and properties will produce incorrect...
Read more >
Using Array.reduce With Objects - RedBit Development
The resulting value can be of any type - it does not have to be an array. ... If you add a new...
Read more >
Unexpected result adding the Array modifier
I have tried to tweak the Array modifier properties but that obviously is not the right solution. I am looking for a way...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found