Skip to content

Commit d6fbe1f

Browse files
committed
- Fix: Allow empty string key (without parent selector)
1 parent b7673ca commit d6fbe1f

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/jsonpath.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ JSONPath.prototype.evaluate = function (
273273
json = json || this.json;
274274
expr = expr || this.path;
275275
if (expr && typeof expr === 'object' && !Array.isArray(expr)) {
276-
if (!expr.path) {
276+
if (!expr.path && expr.path !== '') {
277277
throw new TypeError(
278278
'You must supply a "path" property when providing an object ' +
279279
'argument to JSONPath.evaluate().'
@@ -313,7 +313,7 @@ JSONPath.prototype.evaluate = function (
313313
if (Array.isArray(expr)) {
314314
expr = JSONPath.toPathString(expr);
315315
}
316-
if (!expr || !json) {
316+
if ((!expr && expr !== '') || !json) {
317317
return undefined;
318318
}
319319
this._obj = json;

test/test.path_expressions.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,13 @@ describe('JSONPath - Path expressions', function () {
7979
const result = jsonpath({json, path: "$.store.book[*]['application/vnd.wordperfect']"});
8080
assert.deepEqual(result, expected);
8181
});
82+
83+
it('empty string key', () => {
84+
const jsonSimple = {
85+
'': null
86+
};
87+
const expected = null;
88+
const result = jsonpath({json: jsonSimple, path: '', wrap: false});
89+
assert.deepEqual(result, expected);
90+
});
8291
});

0 commit comments

Comments
 (0)