Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/main/java/com/dashjoin/jsonata/Jsonata.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,10 @@ Frame createFrameFromTuple(Frame environment, Map<String, Object> tuple) {
if( input instanceof JList && ((JList)input).tupleStream) {
((JList)results).tupleStream = true;
}
if (!(input instanceof List)) { // isArray
if (input == null) {
// undefined input yields undefined output; skip filtering entirely
input = Utils.createSequence();
} else if (!(input instanceof List)) { // isArray
input = Utils.createSequence(input);
}
if (predicate.type.equals("number")) {
Expand All @@ -511,8 +514,12 @@ Frame createFrameFromTuple(Frame environment, Map<String, Object> tuple) {
// count in from end of array
index = ((List)input).size() + index;
}
var item = 0<=index && index<((List)input).size() ? ((List)input).get(index) : null;
if(item != null) {
if (0<=index && index<((List)input).size()) {
var item = ((List)input).get(index);
// Preserve JSON null at this index (vs. out-of-bounds, which is undefined)
if (item == null) {
item = NULL_VALUE;
}
if(item instanceof List) {
results = (List)item;
} else {
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/dashjoin/jsonata/NullSafetyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ public void testSingleNull() {
Assertions.assertEquals(1, x);
}

@Test
public void testArrayIndexPreservesNull() {
// Indexing into an array element that is JSON null must yield null,
// not be filtered out as if it were undefined.
Map<String, Object> data = Map.of("data", List.of(
Arrays.asList(1, null, 3),
Arrays.asList(2, null, 4),
Arrays.asList(3, null, 5)));
Object res = jsonata("[$map(data, function($row) { $row[1] })]").evaluate(data);
Assertions.assertEquals(Arrays.asList(null, null, null), res);
}

@Test
public void testFilterNullLookup() {
var x = Jsonata.jsonata("$filter($, function($v, $i, $a){$lookup($v, 'content')})").evaluate(
Expand Down
Loading