mirror of
https://github.com/Kong/insomnia.git
synced 2026-04-21 06:37:36 -04:00
36 lines
780 B
JavaScript
36 lines
780 B
JavaScript
const jq = require('jsonpath');
|
|
|
|
module.exports.templateTags = [{
|
|
displayName: 'JSONPath',
|
|
name: 'jsonpath',
|
|
description: 'Pull data from JSON strings with JSONPath',
|
|
args: [{
|
|
displayName: 'JSON string',
|
|
type: 'string'
|
|
}, {
|
|
displayName: 'JSONPath Filter',
|
|
type: 'string'
|
|
}],
|
|
run (context, jsonString, filter) {
|
|
let body;
|
|
try {
|
|
body = JSON.parse(jsonString);
|
|
} catch (err) {
|
|
throw new Error(`Invalid JSON: ${err.message}`);
|
|
}
|
|
|
|
let results;
|
|
try {
|
|
results = jq.query(body, filter);
|
|
} catch (err) {
|
|
throw new Error(`Invalid JSONPath query: ${filter}`);
|
|
}
|
|
|
|
if (results.length === 0) {
|
|
throw new Error(`JSONPath query returned no results: ${filter}`);
|
|
}
|
|
|
|
return results[0];
|
|
}
|
|
}];
|