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

Implement custom function for parsing embedded JSON (nested JSON docs) #6

Closed
lowell80 opened this issue Nov 9, 2018 · 1 comment
Closed
Labels
custom function enhancement New feature or request
Milestone

Comments

@lowell80
Copy link
Member

lowell80 commented Nov 9, 2018

Come up with a mechanism that allows a nested JSON document to be parsed as a custom function within the JMSEPath language.

Essentially this would just call json.loads().

If this fails, then just assume the object wasn't a JSON string and return the original value unmodified. That seems like a nice safety mechanism in case the there's a mixture of JSON strings and other strings that have to be processed at the same time.

@lowell80 lowell80 added the enhancement New feature or request label Nov 9, 2018
@lowell80 lowell80 added this to the Release 2.0 milestone Nov 9, 2018
@lowell80
Copy link
Member Author

lowell80 commented Nov 9, 2018

Proof-of-concept implementation:

from jmespath import functions

class JmesPathSplunkExtraFunctions(functions.Functions):
    @functions.signature({'types': ['string', 'array']})
    def _func_jsonstr(self, s):
        if s is None:
            return None
        if isinstance(s, (list,tuple)):
            return [ json.loads(i) for i in s ]
        try:
            return json.loads(s)
        except:
            return s

I tried, unsuccessfully to make this handle null types as well, but couldn't get it right. This resulted in runtime errors.

lowell80 added a commit that referenced this issue Nov 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
custom function enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant