UNPKG

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