Forum Discussion

kralston's avatar
kralston
Occasional Contributor
11 years ago

[Resolved] JSonPath vs XPath

Version - 4.6.0

I have been using Rest with XML requests and responses. During that time I have been using XPath in Property Transfers to copy response data to properties to be used in later test steps.

We are looking into moving to JSON for our requests and responses and in our early tests this of course breaks my Property Transfers that are using XPath.

Is JSONPath or some equivalent supported by SOAPUI? If there is no equivalent, what is the recommended way to grab data from the JSON response?

Thank you for any assistance.

8 Replies

  • GiscardN's avatar
    GiscardN
    Frequent Contributor
    This is an update that our 5.1 Release of SoapUI Pro has introduced JSONPath assertions when the response is JSON.
    This is a Pro only release.

    Regards,

    Giscard
    SmartBear Support
  • SiKing's avatar
    SiKing
    Community Expert
    You would use "ResponseAsXml" instead of the previous "Response".
  • kralston's avatar
    kralston
    Occasional Contributor
    Thanks for the response SiKing. Unfortunately, it didn't entirely solve my problem.

    In the Property Transfer step when I had this set to Response with an XML Response I had //Document/DocumentId to grab the data from the node.

    As you suggested in the Property Transfer step I changed the Source Property to "ResponseAsXml" with a JSON response.

    I didn't expect the //Document/DocumentId to work and it didn't with the JSON response.

    I am fairly knew to XPath and am not sure what the correct XPath reference should be to access the same node I was using with the XML response.
  • kralston's avatar
    kralston
    Occasional Contributor
    I've figured out the syntax.

    //*:Documents/*:Document[1]/*:DocumentId

    Thanks for your help.
  • SiKing's avatar
    SiKing
    Community Expert
    kralston wrote:
    I didn't expect the //Document/DocumentId to work and it didn't with the JSON response.

    Based on absolutely nothing, my guess is something like this will work:
    //*:Document//*:DocumentId


    kralston wrote:
    I am fairly knew to XPath and am not sure what the correct XPath reference should be to access the same node I was using with the XML response.

    Try this: http://www.zvon.org/xxl/XPathTutorial/G ... mples.html
  • kralston's avatar
    kralston
    Occasional Contributor
    Thanks Michael

    I found quite a bit of info on JSONSlurper and have been using it in a number of places in my tests.
  • You can use DefiantJS - which enables XPath queries on JSON structures.
    http://defiantjs.com
    https://github.com/hbi99/defiant.js

    Try the XPath evaluator to become an expert on XPath;
    http://defiantjs.com/#xpath_evaluator


    var data = {
    "movie": [{"id": 1, "title": "The Usual Suspects"},
    {"id": 2, "title": "Pulp Fiction"},
    {"id": 3, "title": "Independence Day"}]
    },
    res = JSON.search(data, '//*[id=2]');

    console.log( res[0].title );
    // Pulp fiction


    As you can see, DefiantJS extends the global object JSON with the search method. Check out the documentation for more details.