2

So I have a dictionary as follows:

{
    'assignees': {
        'arrayValue': {
            'values': [{
                'stringValue':

                    '56ea94b3d517f047c9d680a7'
            }]
        }
    },
    'assigneesMap': {
        'mapValue': {
            'fields': {
                '56ea94b3d517f047c9d680a7': {
                    'booleanValue': True
                }
            }
        }
    },
    'closed': {
        'booleanValue': False
    },
    'color': {
        'stringValue': '#ebbdf9'
    },
    'createdDate': {
        'timestampValue': '2018-12-07T06:11:40.058Z'
    },
    'creator': {
        'stringValue': '56ea94b3d517f047c9d680a7'
    },
    'deleted': {
        'booleanValue': False
    },
    'endDate': {
        'nullValue': None
    },
    'lastUpdated': {
        'timestampValue': '2018-12-07T06:11:40.058Z'
    },
    'name': {
        'stringValue': 'Test Checklist Tasks'
    },
    'priority': {
        'integerValue': '1'
    },
    'projectId': {
        'stringValue': 'M919Bcgv0h4J76VdQHYX'
    },
    'status': {
        'stringValue': 'created'
    },
    'tags': {
        'arrayValue': {}
    },
    'users': {
        'arrayValue': {
            'values': [{
                'stringValue': '56ea94b3d517f047c9d680a7'
            }]
        }
    },
    'usersRole': {
        'arrayValue': {
            'values': [{
                'mapValue': {
                    'fields': {
                        'role': {
                            'stringValue': 'admin'
                        },
                        'userId': {
                            'stringValue': '56ea94b3d517f047c9d680a7'
                        }
                    }
                }
            }]
        }
    }
}

I need to remove unwanted keys (type information) as produce the result as follows:

{
    'assignees': ['56ea94b3d517f047c9d680a7'],
    'assigneesMap': {'56ea94b3d517f047c9d680a7': True},
    'closed': False,
    'color': '#ebbdf9',
    'createdDate': '2018-12-07T06:11:40.058Z',
    'creator': '56ea94b3d517f047c9d680a7',
    'deleted': False,
    'endDate': None,
    'lastUpdate': '2018-12-07T06:11:40.058Z',
    'name': 'Test Checklist Tasks',
    'priority': 1,
    'projectId': 'M919Bcgv0h4J76VdQHYX',
    'status': 'created',
    'tags': [],
    'users': ['56ea94b3d517f047c9d680a7'],
    'usersRole': [{'role': 'admin', 'userId': '56ea94b3d517f047c9d680a7'}]
}

One way I could think of solving this was to keep a mapping of field name and field type and act accordingly.

{
    'assignees': 'array_of_strings',
    'assigneesMap': 'map',
    'closed': 'boolean',
    .....
}

Is there a better way to do this without using any such config? Use recursion maybe?

1 Answer 1

1

It is definitely possible with recursion as it follows a pattern in the data structure and beats the need of a mapping field which may become incorrect if additional elements are introduced.

Here is a code snippet for processing the arrayValue section and can be enhanced for the rest.

def parseValue(t):
    t2 = ''
    for k in t.keys():
        v = t[k]
        if k == 'arrayValue':
            t2 = parseValue(v)
        elif k == 'values':
            t2 = []
            for k2 in v:
                t2.append(parseValue(k2))
        elif k == 'stringValue':
            t2 = v
    return t2


e = {}
for k, v in input.iteritems():
    e[k] = parseValue(v)
print e
Sign up to request clarification or add additional context in comments.

Comments

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.