Search code examples
xmljsonneoload

Changing JSON transformation to XML in neoload


Given the following HTTP response :

{"id":"1", "name":test"}

At the moment, when you check "see JSON requests/responses as xml content" in "Edition > Preferences > Project settings > Modules Manager > JSON" (in order to be able to use xpath instead of regexp), you have a completely useless stuff like :

<ObjectNode>
  <__children>
    <entry>
      <string>id</string>
      <__value>1</__value>
    </entry>
      etc...

Note : this is useless because when you have deep JSON response, you have XPath like : ArrayNode/__children/child::ObjectNode/__children/entry[child::string/text()='data']/ObjectNode/__children/entry[child::string/text()='id']/IntNode/__value

I have managed to create an extension where I parse the JSON inside a JSONObject or JSONArray and then I call

return "<root>"+XML.toString(json)+"</root>";

which gives me a beautifull

<root><id>1</id><name>test</name></root>

Note : that I could process easily with xpath

The problem is that then Neoload converts this to

<String>&lt;root&gt;&lt;id&gt;etc... 

which is as useless as the first stuff (maybe more useless because XPath is not an option anymore as I only have one node).

So my question is how can I change the way Neoload is transforming my Decoder return value ?

I have tried to return JSONObject or JSONArray in the Decoder directly and then to use a Namer, but the Namer seems to be used only with requests, not with responses.

Any hint is appreciated !


Solution

  • The "easier" way I found to simulate real JSON is to do the following.

    First, do a normal web page call and add a variable via the advanced panel to capture all the response (the regexp to use is (.*)).

    Then, use a javascript with the following content (here, EntryResponse is the captured variable name)

    //get the previous http response and eval it
    var resp_ = context.variableManager.getValue("EntryResponse");
    if (resp_ == null) {
            context.fail("Variable 'EntryResponse' not found");
    }
    logger.debug("EntryResponse="+resp_);
    var resp = eval("("+resp_+")")
    
    //then used resp as real json
    logger.debug("Logged User Id="+resp.data.id);
    
    // Inject a value in a runtime variable
    context.variableManager.setValue("UserName", resp.data.id);
    

    This is not as straightforward as it could be but this is far more powerful and easier than the classical XPath