mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 14:49:53 +00:00
615287ccfc
Co-authored-by: Dimitri Mitropoulos <dimitrimitropoulos@gmail.com>
42 lines
972 B
JavaScript
42 lines
972 B
JavaScript
const { JSONPath } = require('jsonpath-plus');
|
|
|
|
module.exports.templateTags = [
|
|
{
|
|
displayName: 'JSONPath',
|
|
name: 'jsonpath',
|
|
description: 'pull data from JSON strings with JSONPath',
|
|
args: [
|
|
{
|
|
displayName: 'JSON string',
|
|
type: 'string',
|
|
},
|
|
{
|
|
displayName: 'JSONPath Filter',
|
|
encoding: 'base64', // So it doesn't cause syntax errors
|
|
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 = JSONPath({ json: body, path: 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];
|
|
},
|
|
},
|
|
];
|