REST API JSON Response Parsing Error

REST API JSON Response Parsing Error

  

Hi, I encountered this error when using REST API using JSON response...

Failed to parse response of the method 'GetSearch' of the 'Marklogic' REST API:
Can not instantiate value of type [simple type, class osmarklogicsearch.ccmarklogic.RESTMatchtextItem] from String value (' McGarr'); no single-String constructor/factory method
 at [Source: {"snippet-format":"snippet", "total":50, "start":1, "page-length":10, "results":[{"index":1, "uri":"/zurich/sample_zurich.csv-0-854", "path":"fn:doc(\"/zurich/sample_zurich.csv-0-854\")", "score":86016, "confidence":0.5085448, "fitness":0.6757176, "href":"/v1/documents?uri=%2Fzurich%2Fsample_zurich.csv-0-854&database=zurich-db", "mimetype":"application/xml", "format":"xml", "matches":[{"path":"fn:doc(\"/zurich/sample_zurich.csv-0-854\")/*:policy/*:Customer_Name", "match-text":[{"highlight":"Gary"}, " McGarr"]}]}, ....


This is the JSON response:

{
  "snippet-format": "snippet",
  "total": 50,
  "start": 1,
  "page-length": 10,
  "results": [
    {
      "index": 1,
      "uri": "/zurich/sample_zurich.csv-0-854",
      "path": "fn:doc(\"/zurich/sample_zurich.csv-0-854\")",
      "score": 86016,
      "confidence": 0.5085448,
      "fitness": 0.6757176,
      "href": "/v1/documents?uri=%2Fzurich%2Fsample_zurich.csv-0-854&database=zurich-db",
      "mimetype": "application/xml",
      "format": "xml",
      "matches": [
        {
          "path": "fn:doc(\"/zurich/sample_zurich.csv-0-854\")/*:policy/*:Customer_Name",
          "match-text": [
            {
              "highlight": "Gary"
            },
            " McGarr"
          ]
        }
      ]
    },
    {
      "index": 2,
      "uri": "/zurich/sample_zurich.csv-0-855",
      "path": "fn:doc(\"/zurich/sample_zurich.csv-0-855\")",
      "score": 86016,
      "confidence": 0.5085448,
      "fitness": 0.6757176,
      "href": "/v1/documents?uri=%2Fzurich%2Fsample_zurich.csv-0-855&database=zurich-db",
      "mimetype": "application/xml",
      "format": "xml",
      "matches": [
        {
          "path": "fn:doc(\"/zurich/sample_zurich.csv-0-855\")/*:policy/*:Customer_Name",
          "match-text": [
            {
              "highlight": "Gary"
            },
            " McGarr"
          ]
        }
      ]
    },
    {
      "index": 3,
      "uri": "/zurich/sample_zurich.csv-0-358",
      "path": "fn:doc(\"/zurich/sample_zurich.csv-0-358\")",
      "score": 86016,
      "confidence": 0.5085448,
      "fitness": 0.6757176,
      "href": "/v1/documents?uri=%2Fzurich%2Fsample_zurich.csv-0-358&database=zurich-db",
      "mimetype": "application/xml",
      "format": "xml",
      "matches": [
        {
          "path": "fn:doc(\"/zurich/sample_zurich.csv-0-358\")/*:policy/*:Customer_Name",
          "match-text": [
            {
              "highlight": "Gary"
            },
            " Zandusky"
          ]
        }
      ]
    },
    {
      "index": 4,
      "uri": "/zurich/sample_zurich.csv-0-1904",
      "path": "fn:doc(\"/zurich/sample_zurich.csv-0-1904\")",
      "score": 86016,
      "confidence": 0.5085448,
      "fitness": 0.6757176,
      "href": "/v1/documents?uri=%2Fzurich%2Fsample_zurich.csv-0-1904&database=zurich-db",
      "mimetype": "application/xml",
      "format": "xml",
      "matches": [
        {
          "path": "fn:doc(\"/zurich/sample_zurich.csv-0-1904\")/*:policy/*:Customer_Name",
          "match-text": [
            {
              "highlight": "Gary"
            },
            " Zandusky"
          ]
        }
      ]
    },
    {
      "index": 5,
      "uri": "/zurich/sample_zurich.csv-0-1944",
      "path": "fn:doc(\"/zurich/sample_zurich.csv-0-1944\")",
      "score": 86016,
      "confidence": 0.5085448,
      "fitness": 0.6757176,
      "href": "/v1/documents?uri=%2Fzurich%2Fsample_zurich.csv-0-1944&database=zurich-db",
      "mimetype": "application/xml",
      "format": "xml",
      "matches": [
        {
          "path": "fn:doc(\"/zurich/sample_zurich.csv-0-1944\")/*:policy/*:Customer_Name",
          "match-text": [
            {
              "highlight": "Gary"
            },
            " Zandusky"
          ]
        }
      ]
    },
    {
      "index": 6,
      "uri": "/zurich/sample_zurich.csv-0-1945",
      "path": "fn:doc(\"/zurich/sample_zurich.csv-0-1945\")",
      "score": 86016,
      "confidence": 0.5085448,
      "fitness": 0.6757176,
      "href": "/v1/documents?uri=%2Fzurich%2Fsample_zurich.csv-0-1945&database=zurich-db",
      "mimetype": "application/xml",
      "format": "xml",
      "matches": [
        {
          "path": "fn:doc(\"/zurich/sample_zurich.csv-0-1945\")/*:policy/*:Customer_Name",
          "match-text": [
            {
              "highlight": "Gary"
            },
            " Zandusky"
          ]
        }
      ]
    },
    {
      "index": 7,
      "uri": "/zurich/sample_zurich.csv-0-1946",
      "path": "fn:doc(\"/zurich/sample_zurich.csv-0-1946\")",
      "score": 86016,
      "confidence": 0.5085448,
      "fitness": 0.6757176,
      "href": "/v1/documents?uri=%2Fzurich%2Fsample_zurich.csv-0-1946&database=zurich-db",
      "mimetype": "application/xml",
      "format": "xml",
      "matches": [
        {
          "path": "fn:doc(\"/zurich/sample_zurich.csv-0-1946\")/*:policy/*:Customer_Name",
          "match-text": [
            {
              "highlight": "Gary"
            },
            " Zandusky"
          ]
        }
      ]
    },
    {
      "index": 8,
      "uri": "/zurich/sample_zurich.csv-0-3814",
      "path": "fn:doc(\"/zurich/sample_zurich.csv-0-3814\")",
      "score": 86016,
      "confidence": 0.5085448,
      "fitness": 0.6757176,
      "href": "/v1/documents?uri=%2Fzurich%2Fsample_zurich.csv-0-3814&database=zurich-db",
      "mimetype": "application/xml",
      "format": "xml",
      "matches": [
        {
          "path": "fn:doc(\"/zurich/sample_zurich.csv-0-3814\")/*:policy/*:Customer_Name",
          "match-text": [
            {
              "highlight": "Gary"
            },
            " Zandusky"
          ]
        }
      ]
    },
    {
      "index": 9,
      "uri": "/zurich/sample_zurich.csv-0-3815",
      "path": "fn:doc(\"/zurich/sample_zurich.csv-0-3815\")",
      "score": 86016,
      "confidence": 0.5085448,
      "fitness": 0.6757176,
      "href": "/v1/documents?uri=%2Fzurich%2Fsample_zurich.csv-0-3815&database=zurich-db",
      "mimetype": "application/xml",
      "format": "xml",
      "matches": [
        {
          "path": "fn:doc(\"/zurich/sample_zurich.csv-0-3815\")/*:policy/*:Customer_Name",
          "match-text": [
            {
              "highlight": "Gary"
            },
            " Zandusky"
          ]
        }
      ]
    },
    {
      "index": 10,
      "uri": "/zurich/sample_zurich.csv-0-3816",
      "path": "fn:doc(\"/zurich/sample_zurich.csv-0-3816\")",
      "score": 86016,
      "confidence": 0.5085448,
      "fitness": 0.6757176,
      "href": "/v1/documents?uri=%2Fzurich%2Fsample_zurich.csv-0-3816&database=zurich-db",
      "mimetype": "application/xml",
      "format": "xml",
      "matches": [
        {
          "path": "fn:doc(\"/zurich/sample_zurich.csv-0-3816\")/*:policy/*:Customer_Name",
          "match-text": [
            {
              "highlight": "Gary"
            },
            " Zandusky"
          ]
        }
      ]
    }
  ],
  "qtext": "gary",
  "metrics": {
    "query-resolution-time": "PT0.008002S",
    "snippet-resolution-time": "PT0.010862S",
    "total-time": "PT0.026394S"
  }
}


Is this a bug? Should I raise support ticket for this?

Can I retrieve unparsed JSON from REST API for a workaround?

Thanks a lot...

I'm not entirely sure, but I think the problem is that " McGarr" and " Zandusky" are loose values without a name? They look odd anyway, with the space in front of the name.

That said, if you need to look at the raw JSON, use the On After Response property of the consumed REST service. It allows you to inspect and change the JSON.

Hi Kilian, thanks for the heads up...

          "match-text": [
            {
              "highlight": "Gary"
            },
            " McGarr"
          ]

This supposed to be a name, "Gary McGarr".

But it tells me to highlight the word Gary.

Is it an invalid JSON structure?

Solution

Hi Harlin, 

In fact, your JSON is considered valid for any JSON Validator. 

Therefore, as Kilian said, that " McGarr" and " Zandusky" are causing the problem but not because you don't have a "key:value" pair.

There you have the "match-text" : [ ... ]
Now, you have the key (match-text) and witch data type corresponds this key?
Checking your response, it could be either Structure or Text, right?

So, the parser, parses the first one and see that you will have a List of Structure ( started with JSON object, identified with the {}), then, the platform see a simple Text and this doesn't match with the structure that the platform saw before.


Bottom line, you have a valid json to be consumed in a language that doesn't care about the Type but OutSystems cares about the Type. 


Pointing this, you are not able to have a List of 2 different types (such as structure and text at the same time).


Hope it helps.

If I could help with this, just let me know.

Cheers

Solution

Thanks for your detailed explanation...

I will try to alter this JSON response using On After Response, so it can be parsed successfully.


UPDATE:

I have been able to successfully parsed this JSON by doing some Regular Expression replaces using On After Response, and change response structure a bit in match-text to list of text type...


Modified JSON Snippet:

"match-text":["<span class="highlight">Gary</span>", " McGarr"]


Thanks a lot...


That's great news Harlin.

That's exactly the main use case for OnAfterResponse callback.

Keep up the good work and, if you have any other question, feel free to post it.

Cheers.