Skip to main content

extract_json_path

Function extract_json_path 

Source
pub fn extract_json_path(raw_json: &str, path: &str) -> Result<String, McpError>
Expand description

Navigate a dot-separated path into a JSON value, transparently parsing stringified JSON at each step.

Path segments are dot-separated (e.g., content.0.text.query_result.results). Numeric segments index into JSON arrays (content.0 means content[0]). When a segment resolves to a JSON string, the function automatically tries to parse that string as JSON and continues navigating into the parsed result. This handles the common MCP wrapper pattern where response payloads are double-encoded as stringified JSON.

After all segments are consumed, if the final value is still a string, one more parse attempt is made so that a terminal stringified array (e.g., "[{\"a\":1}]") is returned as the parsed array.

Returns the serialized JSON string of the value at the terminal path segment.

§Errors

Returns ErrorCode::SchemaMismatch when:

  • raw_json is not valid JSON.
  • A stringified JSON value at a traversal point cannot be re-parsed.
  • A numeric segment is out of range for the current array, or the current value is not an array.
  • A named segment is missing from the current object, or the current value is not an object.
  • The final result cannot be serialized back to a JSON string (surfaces as ErrorCode::InternalError).