skip to Main Content

First of all – I am not a Python Guru, just learning.

I have a code, which parse all the issues from my personal Jira space into one big JSON variable.
I would like to remove unnecessary nested fields, like createdAt, etc.

Do you have a solution, how to do that?

That is a sample of the output I have:

{
    "expand": "names,schema",
    "issues": [
        {
            "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
            "fields": {
                "assignee": {
                    "active": true,
                    "avatarUrls": {
                        "16x16": "https://jira.link.se/secure/useravatar?size=xsmall&avatarId=6559",
                        "24x24": "https://jira.link.se/secure/useravatar?size=small&avatarId=6559",
                        "32x32": "https://jira.globalrelay.net/secure/useravatar?size=medium&avatarId=6559",
                        "48x48": "https://jira.link.se/secure/useravatar?avatarId=6559"
                    },
                    "displayName": "Vasiliy Utkin",
                    "emailAddress": "[email protected]",
                    "key": "JIRAHUIRA221",
                    "name": "[email protected]",
                    "self": "https://jira.link.se/rest/api/2/[email protected]",
                    "timeZone": "America/Toronto"
                },
                "status": {
                    "description": "",
                    "iconUrl": "https://jira.link.se/images/icons/statuses/open.png",
                    "id": "6559",
                    "name": "To Do",
                    "self": "https://jira.link.se/rest/api/2/status/6559",
                    "statusCategory": {
                        "colorName": "default",
                        "id": 2,
                        "key": "new",
                        "name": "To Do",
                        "self": "https://jira.link.se/rest/api/2/statuscategory/2"
                    }
                },
                **"summary": "Jumping over the tree"**
            },
            "id": "2234",
            "key": "XXX-1",
            "self": "https://jira.link.se/rest/api/2/issue/6559"
        }
    ],
    "maxResults": 50,
    "startAt": 0,
    "total": 1
}

What I tried – there is my code:

json_obj = json.loads(printInfo)

del json_obj["expand"]
del json_obj["maxResults"]
del json_obj["startAt"]
del json_obj["total"]

These lines could delete all the first level fields, but not nested. I tried to find the way how to delete the nested values, but unsuccessfully.

I tried to do some weird code like that:

del json_obj[{'issues': {'expand'}}]

But it wasn’t working as well.

Expected output is a new JSON with data from sample input:

{
    "active": true,
    "displayName": "Vasiliy Utkin",
    "emailAddress": "[email protected]",
    "name": "To Do",**
    "key": "new",
    "name": "To Do",
    "summary": "Jumping over the tree"
    "key": "XXX-1",
}

And also I need to have a key value in an additional global variable.

2

Answers


  1. issues is a list, so loop over it and delete the nested keys.

    for issue in json_obj['issues']:
        del issue['expand']
    
    Login or Signup to reply.
  2. Here’s an example of extracting the information you want into a separate list. Deleting is not enough for what you want, because you need to reorganize as well. Notice that I have tweaked the keys to avoid duplicates.

    import json
    
    data = json.load(open('x.json'))
    
    info = []
    for row in data['issues']:
        rec = {
            'key': row['key'],
            'active': row['fields']['assignee']['active'],
            'displayName': row['fields']['assignee']['displayName'],
            'emailAddress': row['fields']['assignee']['emailAddress'],
            'status-name': row['fields']['status']['statusCategory']['name'],
            'status-key': row['fields']['status']['statusCategory']['key'],
            'summary': row['fields']['summary']
        }
        info.append( rec )
    
    from pprint import pprint
    pprint(info)
    

    Output:

    [{'active': True,
      'displayName': 'Vasiliy Utkin',
      'emailAddress': '[email protected]',
      'key': 'XXX-1',
      'status-key': 'new',
      'status-name': 'To Do',
      'summary': 'Jumping over the tree'}]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search