pythonibm-cloudibm-watsonwatson-nlu

TypeError: Object of type 'Entities' is not JSON serializable IBM Cloud natural language understanding


import json
from watson_developer_cloud import NaturalLanguageUnderstandingV1
import watson_developer_cloud.natural_language_understanding.features.v1 \
as Features

natural_language_understanding = NaturalLanguageUnderstandingV1(
username="username",
password="password",
version="2017-02-27")

response = natural_language_understanding.analyze(
text="IBM is an American multinational technology company headquartered \
in Armonk, New York, United States, with operations in over 170 \
countries.",
features=[
Features.Entities(
  emotion=True,
  sentiment=True,
  limit=2
),
Features.Keywords(
  emotion=True,
  sentiment=True,
  limit=2
)
 ]
 )

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

I am new to IBM watson API .....i was trying this sample code provided by them by I was getting this error

TypeError: Object of type 'Entities' is not JSON serializable


Solution

  • Got the solution from the IBM developer works here is the link

    just replace

    features=[
       Features.Entities(
              emotion=True,
              sentiment=True,
               limit=2
        ),
       Features.Keywords(
               emotion=True,
               sentiment=True,
               limit=2
        )
    ]
    

    with :

    features=Features(entities=EntitiesOptions(
                          emotion=True, sentiment=True,limit=2), 
                   keywords=KeywordsOptions(
                          emotion=True, sentiment=True,limit=2
                                    ))
    

    this is due to the changes done in v 1 python sdk Here is the link showing the changes made in v 1 python sdk