Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract_jsonpath give an error if filters are used in records_jsonpath #379

Closed
MeltyBot opened this issue May 11, 2022 · 1 comment
Closed

Comments

@MeltyBot
Copy link
Contributor

Migrated from GitLab: https://gitlab.com/meltano/sdk/-/issues/382

Originally created by @andrey.shalitkin on 2022-05-11 21:59:09


Summary

extract_jsonpath give an error if filters are used in records_jsonpath

Steps to reproduce

Add filter records_jsonpath in RESTStream, e.g.
records_jsonpath = '$.somenode[?(@.attribute=="Value")]'

What is the current bug behavior?

JsonPathLexerError: Error on line 1, col 15: Unexpected character: ?

What is the expected correct behavior?

No errors

Possible fixes

The fix is described over here: h2non/jsonpath-ng#8

Here is the jsonpath.py that would fix it:

"""JSONPath helpers."""

from typing import Any, Generator, Union

import jsonpath_ng
import memoization
from jsonpath_ng.ext import parse

def extract_jsonpath(
    expression: str, input: Union[dict, list]
) -> Generator[Any, None, None]:
    """Extract records from an input based on a JSONPath expression.

    Args:
        expression: JSONPath expression to match against the input.
        input: JSON object or array to extract records from.

    Yields:
        Records matched with JSONPath expression.
    """
    compiled_jsonpath = _compile_jsonpath(expression)

    match: jsonpath_ng.DatumInContext
    for match in compiled_jsonpath.find(input):
        yield match.value


@memoization.cached
def _compile_jsonpath(expression: str) -> jsonpath_ng.JSONPath:
    """Parse a JSONPath expression and cache the result.

    Args:
        expression: A string representing a JSONPath expression.

    Returns:
        A compiled JSONPath object.
    """
    return parse(expression)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants