JSON Assert for string value

draven23draven23 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?



  • OmarROmarR Posts: 207 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".

  • OmarROmarR Posts: 207 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_parasoftbenken_parasoft Posts: 588 ✭✭✭
    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:

  • OmarROmarR Posts: 207 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:

  • draven23draven23 Posts: 5

    Thanks! That's exactly what I needed.

Sign In or Register to comment.