MCPcopy Index your code
hub / github.com/loggerhead/json4u / jsonPath

Function jsonPath

src/lib/worker/command/jsonPath.ts:10–35  ·  view source on GitHub ↗
(path: string)

Source from the content-addressed store, hash-verified

8}
9
10export function jsonPath(path: string): Result {
11 const tree = getViewState().tree;
12 const json = tree.toJSON();
13
14 try {
15 const pointers: string[] =
16 JSONPath({
17 path,
18 json,
19 resultType: "pointer",
20 wrap: true,
21 }) ?? [];
22
23 const values = pointers.map((p) => {
24 const id = rootMarker + p;
25 return tree.stringifyNode(tree.node(id));
26 });
27
28 // flatten result to a single value
29 const output = values.length <= 1 ? values[0] : `[${values.join(",")}]`;
30
31 return { output, error: undefined };
32 } catch (e) {
33 return { output: undefined, error: e };
34 }
35}

Callers 1

jsonpath.test.tsFile · 0.90

Calls 3

toJSONMethod · 0.80
stringifyNodeMethod · 0.80
nodeMethod · 0.80

Tested by

no test coverage detected