-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonpath.js
62 lines (56 loc) · 1.97 KB
/
jsonpath.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { html, LitElement, property } from 'https://cdn.jsdelivr.net/gh/lit/dist@2/all/lit-all.min.js';
import jsonpath from 'https://cdn.jsdelivr.net/npm/jsonpath@1/jsonpath.min.js';
export class JsonPathViewer extends LitElement {
@property({ type: String }) jsonData = '{}';
@property({ type: String }) jsonPath = '$';
@property({ type: String }) result = '';
static getMetaConfig() {
return {
controlName: 'JSON Path',
description: 'Query JSON data using JSON Path',
fallbackDisableSubmit: false,
pluginAuthor: 'Jordan',
pluginVersion: '1.0.0',
groupName: 'Custom PMA Controls',
version: '1.2',
properties: {
jsonData: {
type: 'string',
title: 'JSON Data',
description: 'The JSON data to query'
},
jsonPath: {
type: 'string',
title: 'JSON Path',
description: 'The JSON path to query'
}
}
};
}
updated(changedProperties) {
if (changedProperties.has('jsonData') || changedProperties.has('jsonPath')) {
this.evaluateJsonPath();
}
}
evaluateJsonPath() {
let parsedData;
try {
parsedData = JSON.parse(this.jsonData);
} catch (error) {
console.error("Error parsing JSON:", error);
this.result = 'Error parsing JSON.';
}
try {
const result = jsonpath.query(parsedData, this.jsonPath);
this.result = JSON.stringify(result, null, 2);
} catch (error) {
console.error("Error evaluating JSON Path:", error);
this.result = 'Error evaluating JSON Path.';
}
}
render() {
return html`<p>${this.result}</p>`;
}
}
// Registering the web component
customElements.define('pma-jsonpath', JsonPathViewer);