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

extracting json using extract_binds returns unicode string #77

Closed
night2tl opened this issue Sep 30, 2015 · 3 comments
Closed

extracting json using extract_binds returns unicode string #77

night2tl opened this issue Sep 30, 2015 · 3 comments

Comments

@night2tl
Copy link

when using something like this:
- extract_binds:
- 'myJson': {'jsonpath_mini': 'json_field'}

Instead of returning an escaped answer like {"access":"asdasd"},
it returns a unicode one : {u'access': u'asdasd'}

when referred to in other context as $myJson.

Which makes it incorrect for further usage in latter requests.

@svanoort
Copy link
Owner

Ah, I think I understand your confusion here. The MiniJsonExtractor parses JSON before doing extraction, so if you're extracting a full data structure (a list or dictionary), it returns a python object, not a text string. When used in a template, this may give unexpected results.

There's a couple solutions to this problem:

A little clunkier I know, but usable.

  • More powerful, but more complex: define a custom extractor (possible extending the JsonPathMini extractor) that converts the python object back to JSON strings. See extensions documentation. It's perfectly safe to go in core if it doesn't add additional libraries, but does need unit testing. Would suggest extractor named jsonpath_mini_string. Another option would be a regex-based extractor (also valid, maybe more broadly useful). If you have the time, this would make a wonderful PR since it shouldn't be more than a couple hours work (otherwise I can add it as a future enhancement).
  • More powerful but harder option define a custom extractor extension using jsonpath-rw that offers even more power (on my radar as a future enhancement already). This may be set to autoload on start if the library is installed, much like the json schema validator.
  • Superman mode: add Jinja2 templating support as a optional extension (same mode as above). This one is a future goal after the next major set of planned changes unless I can find a clever and back-compatible way to insert it. It will take a long time before it's an option though (read: probably months).

Does that both answer your question and give some idea of where future options are going?

@svanoort
Copy link
Owner

svanoort commented Oct 9, 2015

@night2tl Your voice has been heard: this is addressed explicitly in the next release by enhanced templating support in #93 (scoped/dynamic templating... dynamic, in this case).

Since I haven't heard any more feedback on this one, I'm going to go ahead and close it as a duplicate of the larger featureset.

@JohnnyWuMigme
Copy link

JohnnyWuMigme commented Jun 2, 2017

I tried creating a custom extractor named jsonpath_mini_string in validators.py. So you can just call

- extract_binds:
    - 'sessionId': {'jsonpath_mini_string': 'sessionId'}

and variable $sessionId refers later in template will be pure json string but not unicode.

Rename this attachment to validators.py and replace with original file in pyresttest install path.
(ie: my path: /Library/Python/2.7/site-packages/pyresttest-1.7.2.dev-py2.7.egg/pyresttest )

validators.add_jsonpath_mini_string.py.txt

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

3 participants