forked from JSONPath-Plus/JSONPath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (39 loc) · 1.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* globals JSONPath */
/* eslint-disable import/unambiguous */
// Todo: Extract testing example paths/contents and use for a
// pulldown that can populate examples
// Todo: Make configurable with other JSONPath options
// Todo: Allow source to be treated as an (evaled) JSON object
// Todo: Could add JSON/JS syntax highlighting in sample and result,
// ideally with a jsonpath-plus parser highlighter as well
const $ = (s) => document.querySelector(s);
const updateResults = () => {
const jsonSample = $('#jsonSample');
const reportValidity = () => {
// Doesn't work without a timeout
setTimeout(() => {
jsonSample.reportValidity();
});
};
let json;
try {
json = JSON.parse(jsonSample.value);
jsonSample.setCustomValidity('');
reportValidity();
} catch (err) {
jsonSample.setCustomValidity('Error parsing JSON: ' + err.toString());
reportValidity();
return;
}
const result = JSONPath.JSONPath({
path: $('#jsonpath').value,
json
});
$('#results').value = JSON.stringify(result, null, 2);
};
$('#jsonpath').addEventListener('input', () => {
updateResults();
});
$('#jsonSample').addEventListener('input', () => {
updateResults();
});