Failed to translate JSON in request error in Watson discovery query call

213 Views Asked by At

Using the ibm-watson Python SDK version 5.3.0 with the following code example:

from ibm_watson import DiscoveryV2, ApiException
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
import json


url = 'https://api.eu-de.discovery.watson.cloud.ibm.com'
version = '2020-08-30'

apikey = '...'
project = '...'
collection = '...'

authenticator = IAMAuthenticator(apikey)
discovery = DiscoveryV2(version=version, authenticator=authenticator)
discovery.set_service_url(url)

try:
    result = discovery.query(
        project_id=project,
        collection_ids=collection,
        query='text:searchterm',
        table_results=True
    ).get_result()


    print(json.dumps(result, indent=2))

except ApiException as ex:
    print(f'Failed. Status code {ex.code}: {ex.message}')

Returns the following error from the server.

Failed. Status code 400: Failed to translate JSON in request: Cannot deserialize value of type `scala.collection.immutable.List<java.lang.String>` from String value (token `JsonToken.VALUE_STRING`)

The error only occurs if collections_ids or table_results is specified.

1

There are 1 best solutions below

0
Simon O'Doherty On

The server is telling you that the JSON sent to it doesn't match what it expects to process.

The collections_ids and table_results are incorrectly formatted for the API request.

As per the API documentation.

  • collections_ids must be a list. So change to collections_ids=[collections]
  • table_results expects an object. To correct use table_results={ 'enabled': True }