Search code examples
karate

Parsing response with multiple json objects


I have an API for which response is not a single JSON object but multiple JSON objects such as-

{"a": 1, "b": 2, "c": 3}
{"a": 4, "b": 5, "c": 6}

As the response is not a valid JSON, i am seeing an parsing error when accessing API call response in karate reports summary. I am still able to access the response using a print statement as shown in the screenshot(I have redacted the actual call/request from screenshot)

enter image description here

Is there a way to handle such response or return the response AS-IS instead of the error ?


Solution

  • Looks like you are dealing with JSONL.

    When parsing to JSON fails, the responseBytes variable will always be available. So you can do this:

    * string data = responseBytes
    

    And here is how to convert to a JSON array:

    * text data = 
    """
    {"a": 1, "b": 2, "c": 3}
    {"a": 4, "b": 5, "c": 6}
    """
    * def response = data.split('\n').map(x => karate.fromString(x))