0

I received this error after running the script below. I would like to create a json file through the result that is generated in this script. What can I do to correct this problem?

I tried to do it via API but I couldn't get the fields I need from this DevOps table.

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v5_1.work_item_tracking.models import Wiql
import json

token = 'xxx'
team_instance = 'https://dev.azure.com/xxx'

credentials = BasicAuthentication("", token)
connection = Connection(base_url=team_instance, creds=credentials)

def print_work_items(work_items):
    for work_item in work_items:
        with open('teste_project.json', 'w') as json_file:
            json.dump(work_items, json_file)

wit_client = connection.clients.get_work_item_tracking_client()

def get_TC_from_query(query):
    query_wiql = Wiql(query=query)
    results = wit_client.query_by_wiql(query_wiql).work_items
    # WIQL query gives a WorkItemReference => we get the corresponding WorkItem from id
    work_items = (wit_client.get_work_item(int(result.id)) for result in results)
    print_work_items(work_items)

get_TC_from_query(
    """\
SELECT *
FROM workitems
WHERE
        [System.TeamProject] = 'xxx'
        and [System.WorkItemType] = 'Product Backlog Item'
        and [System.State] = 'Done'
ORDER BY [System.ChangedDate] DESC
"""
)    

TypeError: Object of type WorkItem is not JSON serializable

2 Answers 2

2

The following produces a generator, which JSON can not serialize:

work_items = (wit_client.get_work_item(int(result.id)) for result in results)

You can instead make work_items a list, which JSON can serialize:

work_items = [wit_client.get_work_item(int(result.id)) for result in results]
Sign up to request clarification or add additional context in comments.

2 Comments

I continue with error:"TypeError: Object of type WorkItem is not JSON serializable". I will update the script
@FelipeFB either see this question or abstract the data you want out of the WorkItem objects. JSON only supports a handful of basic types.
1

Inspired by the thread provided by @jordanm. You can override the default() method to serialize additional types.

I made below changes to your code, and it worked when i tested.

First make work_items a list:

work_items = [wit_client.get_work_item(int(result.id)) for result in results]

Then in print_work_items method, i override the default method in json.dump() with default = lambda o: o.__dict__. Please check below:

def print_work_items(work_items):
    for work_item in work_items:
        with open('teste_project.json', 'w') as json_file:
            json.dump(work_items, json_file, default = lambda o: o.__dict__, sort_keys=True, indent=4)

1 Comment

I am not able to read this json generated in a databricks table. I tried to apply StringIO but I was unsuccessful, what can I do to solve this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.