Bug 59609 - JSONPathPostProcessor not evaluating correctly
Summary: JSONPathPostProcessor not evaluating correctly
Status: RESOLVED FIXED
Alias: None
Product: JMeter
Classification: Unclassified
Component: Main (show other bugs)
Version: 3.0
Hardware: All All
: P2 normal (vote)
Target Milestone: ---
Assignee: JMeter issues mailing list
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-05-21 03:45 UTC by Michael Chirlin
Modified: 2016-05-31 18:21 UTC (History)
1 user (show)



Attachments
Extract JSON Objects as JSON Strings (10.33 KB, patch)
2016-05-22 10:39 UTC, Felix Schumacher
Details | Diff
Extract JSON Objects as JSON Strings (11.68 KB, patch)
2016-05-22 11:29 UTC, Felix Schumacher
Details | Diff
Extract JSON Objects as JSON Strings (11.84 KB, patch)
2016-05-24 19:15 UTC, Felix Schumacher
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Chirlin 2016-05-21 03:45:24 UTC
The JSONPathPostProcessor uses the follow to turn the JSON results to string, however if toJSONString isn't used then all of the quotes will be removed.


Current Example

With this Code (JSONPostProcessor 166-167):

Object obj = extractedValues.get(0);
String objAsString = obj != null ? obj.toString() : ""; //$NON-NLS-1$

Run with:

$.context

Against:

{
  "context": {
    "#v": "abc123",
    "#t": "string"
  }
}

Results in:

{#v: abc123, #t: string}

This is incorrect, all of the quotes have been removed.

It should be replaced with something more like:

String objAsString = "";
if (extractedValues instanceof JSONArray) {
 objAsString = new JSONObject((Map) extractedValues.get(0)).toJSONString();
}

Which when tested results:

{"#t":"string","#v":"abc123"}

This obviously needs to be extended for examples where more than one response is returned.
Comment 1 Felix Schumacher 2016-05-22 10:39:47 UTC
Created attachment 33861 [details]
Extract JSON Objects as JSON Strings

Extract JSON Objects as JSON Strings.

The old implementation gave a Map instance back for the described case.
Comment 2 Felix Schumacher 2016-05-22 11:29:59 UTC
Created attachment 33862 [details]
Extract JSON Objects as JSON Strings

Keep PostProcessor clean by moving the stringification into the JSONManager.

It would be good, if the interface of JSONManager would show, that it returns a list of Strings. But as it is a public interface it seems to be to late to change it now.
Comment 3 Michael Chirlin 2016-05-22 23:13:39 UTC
There is an additional use case in which the extractedObject is an instanceof JSONArray rather than a Map (JSONObject).

I propose updating the JSONManager stringifyJSONObject the following:

private String stringifyJSONObject(Object obj) {
  if (obj instanceof JSONArray) {
    return ((JSONArray) obj).toJSONString();
  } else if (obj instanceof Map) {
    return new JSONObject((Map<String, ?>) obj).toJSONString();
  }
  return obj == null ? "" : obj.toString(); //$NON-NLS-1$
}

An example would be:

{
  "saveInto": [
    "string1"
  ]
}

using JSONPath:

$.saveInto

I'm not sure how you create the attachments or I would do it for you.
Comment 4 Felix Schumacher 2016-05-24 19:15:24 UTC
Created attachment 33882 [details]
Extract JSON Objects as JSON Strings

My old patch should have worked with your use case already. But to be on the safe side, I added the case for JSONArray.
Comment 5 Philippe Mouawad 2016-05-30 19:23:20 UTC
Ok for patch.
Thanks Felix
Comment 6 Felix Schumacher 2016-05-31 18:21:16 UTC
Date: Tue May 31 18:19:48 2016
New Revision: 1746310

URL: http://svn.apache.org/viewvc?rev=1746310&view=rev
Log:
Format extracted JSON Objects in JSON Post Processor correctly as JSON.

Bugzilla Id: 59609

Added:
    jmeter/trunk/test/src/org/apache/jmeter/extractor/TestJSONPostProcessor.java
Modified:
    jmeter/trunk/src/components/org/apache/jmeter/extractor/json/jsonpath/JSONManager.java
    jmeter/trunk/src/components/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java
    jmeter/trunk/xdocs/changes.xml