MCPcopy Create free account
hub / github.com/cloudflare/agents / parseJsonPath

Function parseJsonPath

packages/shell/src/extras.ts:220–248  ·  view source on GitHub ↗
(query: string)

Source from the content-addressed store, hash-verified

218 };
219
220function parseJsonPath(query: string): PathToken[] {
221 const trimmed = query.trim();
222 if (!trimmed || trimmed === ".") {
223 return [];
224 }
225 let cursor = trimmed.startsWith(".") ? trimmed.slice(1) : trimmed;
226 const tokens: PathToken[] = [];
227 while (cursor.length > 0) {
228 if (cursor[0] === "[") {
229 const close = cursor.indexOf("]");
230 if (close === -1) {
231 throw new Error(`Invalid JSON path: ${query}`);
232 }
233 tokens.push(Number(cursor.slice(1, close)));
234 cursor = cursor.slice(close + 1);
235 if (cursor.startsWith(".")) cursor = cursor.slice(1);
236 continue;
237 }
238 const dot = cursor.search(/[.[\]]/);
239 if (dot === -1) {
240 tokens.push(cursor);
241 break;
242 }
243 tokens.push(cursor.slice(0, dot));
244 cursor = cursor.slice(dot);
245 if (cursor.startsWith(".")) cursor = cursor.slice(1);
246 }
247 return tokens.filter((token) => token !== "");
248}
249
250function setJsonPathValue(
251 root: unknown,

Callers 2

queryJsonValueFunction · 0.85
updateJsonValueFunction · 0.85

Calls 1

searchMethod · 0.65

Tested by

no test coverage detected