Converting textanalytics result to JSON Format

KA 76 Reputation points
2022-04-26T10:05:32.993+00:00

Hello,
I am using the example provided in the Machine Learning Studio Docs for extracting Health Entities from a given string.
The code is shown below.

My question is: what is the easiest way to convert the output result into JSON format?

from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
import json

credential = AzureKeyCredential("**********************************")
endpoint="https://eastus.api.cognitive.microsoft.com/"

text_analytics_client = TextAnalyticsClient(endpoint, credential)

documents = ["Subject is taking 100mg of ibuprofen twice daily"]

poller = text_analytics_client.begin_analyze_healthcare_entities(documents)
result = poller.result()

docs = [doc for doc in result if not doc.is_error]

print("Results of Healthcare Entities Analysis:")
for idx, doc in enumerate(docs):
    for entity in doc.entities:
        print("Entity: {}".format(entity.text))
        print("...Normalized Text: {}".format(entity.normalized_text))
        print("...Category: {}".format(entity.category))
        print("...Subcategory: {}".format(entity.subcategory))
        print("...Offset: {}".format(entity.offset))
        print("...Confidence score: {}".format(entity.confidence_score))
        if entity.data_sources is not None:
            print("...Data Sources:")
            for data_source in entity.data_sources:
                print("......Entity ID: {}".format(data_source.entity_id))
                print("......Name: {}".format(data_source.name))
        if entity.assertion is not None:
            print("...Assertion:")
            print("......Conditionality: {}".format(entity.assertion.conditionality))
            print("......Certainty: {}".format(entity.assertion.certainty))
            print("......Association: {}".format(entity.assertion.association))
        for relation in doc.entity_relations:
            print("Relation of type: {} has the following roles".format(relation.relation_type))
        for role in relation.roles:
            print("...Role '{}' with entity '{}'".format(role.name, role.entity.text))
    print("------------------------------------------")
Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
2,708 questions
{count} votes

Accepted answer
  1. romungi-MSFT 43,681 Reputation points Microsoft Employee
    2022-04-26T14:18:10.653+00:00

    @KA The result does not seem to be directly serializable to JSON. I found a library JSONS that can do the heavy lifting if you are using python 3.5 or higher.

    Install jsons

    pip install jsons  
    

    Import JSONS and using jsons.dump() on docs object.

    import jsons #import in the import section  
    print(jsons.dump(docs)) #Printing the json after docs is created  
    

    This should give a file of this format in this case. Uploaded the file in .txt format since JSON files cannot be uploaded on Q&A, download the file and rename it to .json
    I hope this helps!!

    If an answer is helpful, please click on 130616-image.png or upvote 130671-image.png which might help other community members reading this thread.

    196624-health.txt


0 additional answers

Sort by: Most helpful