diff --git a/src/jq.d.ts b/src/jq.d.ts index 579172f..c5195f0 100644 --- a/src/jq.d.ts +++ b/src/jq.d.ts @@ -1,3 +1,3 @@ import { PartialOptions } from "./options" -export function run(filter: string, json: any, options?: PartialOptions, jqPath?: string, cwd?: string, detached?: boolean): Promise +export function run(filter: string, json: any, options?: PartialOptions, jqPath?: string, cwd?: string, detached?: boolean): Promise diff --git a/src/jq.js b/src/jq.js index e6a9246..1a88b5d 100644 --- a/src/jq.js +++ b/src/jq.js @@ -13,6 +13,10 @@ export const run = (filter, json, options = {}, jqPath, cwd, detached) => { exec(command, args, stdin, cwd, detached) .then((stdout) => { if (options.output === 'json') { + if (stdout === '') { + return resolve(undefined) + } + let result try { result = JSON.parse(stdout) diff --git a/src/jq.test.js b/src/jq.test.js index 41957c9..253f0d1 100644 --- a/src/jq.test.js +++ b/src/jq.test.js @@ -192,4 +192,26 @@ describe('jq core', () => { done(error) }) }) + + it('should output empty string for a match on json output', done => { + run('.foo', { foo: '' }, { input: 'json', output: 'json' }) + .then(output => { + expect(output).to.equal('') + done() + }) + .catch(error => { + done(error) + }) + }) + + it('should output undefined for no match on json output', done => { + run('select(.foo == "bar")', { foo: '' }, { input: 'json', output: 'json' }) + .then(output => { + expect(output).to.equal(undefined) + done() + }) + .catch(error => { + done(error) + }) + }) })