Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

JSON Assert for string value

draven23
draven23 Posts: 5

I have a method that returns a JSON response like this:

    {
        "content" : "{\n  \"itv\": \"On\",\n  \"vm\": {\n    \"firstName\": \"s\",\n    \"lastName\": \"a\",\n    \"prefLanguage\": \"English\",\n    \"hub\": \"\",\n    \"cos\": \"1111\",\n    \"cfn\": \"6\",\n    \"m\": \"defaultM\"\n  }\n}",
        "created" : "2018-04-02 20:36:00",
        "id" : 10069537,
        "meta" : {
            "HttpMethod" : "POST",
            "RequestURL" : "http://",
            "headers" : {
                "trans" : "2866",
                "ProductName" : "p",
        },
        "outbound" : true,
        "responded" : "2018-04-02 20:36:00",
        "responseId" : 10069542,
        "statusCode" : 400,
        "statusMessage" : "Bad Request"
    }

I need to Assert on this value \"cos\": \"1111\" that appears in content. The JSON Assertor extracts all the values from content but won't allow me to select only the one I'm interested in. How can I extract just the cos value from the string? Is the JSON Assertor the right tool for the job?

Thanks

Comments

  • OmarR
    OmarR Posts: 233 admin
    edited April 2018

    Hello draven23,

    Try adding a "String Comparison Assertion" for the JSON Assertor and selecting "contain" as the type of configuration. The configuration below asserts that the extracted element value contains "cos": "1111".

  • OmarR
    OmarR Posts: 233 admin

    An alternative approach would be to use an XML converter to convert your JSON into XML. This would allow you to use the XML transformer to extract the exact element you're interested in and assert it using a JSON assertor.

    Try the set up below and let me know if you have any questions.

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭
    edited April 2018

    I see the value of "content" being an embedded (encoded) JSON document. I would probably use a JSON Data Bank to first extract the value of "content". Doing so effectively decodes the embedded document and then saves it in a data bank column:

    Next, I would use a JSON Assertor, passing the data bank column as input:

    This will enable you to setup assertions on specific paths in the embedded document:

  • OmarR
    OmarR Posts: 233 admin

    I-think-I-prefer @benken_parasoft's solution of using a stand alone JSON assertor. Additionally, you could pass the variable using the ${} syntax.
    For example:

  • draven23
    draven23 Posts: 5

    Thanks! That's exactly what I needed.