0

I have a nested JSON object, and I want to extract 2 items ('id' and 'name') from the nested item. I can do this with a loop, but my approach feels very clunky- even as a newbie. Can someone suggest a more elegant approach?

groups_requests ={'@odata.context': 'http://wabi-australia-southeast-redirect.analysis.windows.net/v1.0/myorg/$metadata#groups', '@odata.count': 2, 'value': [{'id': '53d06b2423-690f-4923-8f65-db710c038566', 'isReadOnly': False, 'isOnDedicatedCapacity': False, 'name': 'Steves_Test_App_workSpace'}, {'id': '988f6d4ea-14b2-4ad7-a899-ae4v0d974c9139', 'isReadOnly': False, 'isOnDedicatedCapacity': False, 'name': 'DataflowsTest'}]}

import pandas as pd
p=(groups_requests['value'])
mlis = []
for i in p:
    lis =[]
    lis.append(i['id'])
    lis.append(i['name'])
    mlis.append(lis)
df = pd.DataFrame(mlis)
df.columns = ['Workspaceid', 'Workspacename']
df

1 Answer 1

2

You can use list comprehension to get rid of loop:

df = pd.DataFrame(
    [(e['id'], e['name']) for e in groups_requests['value']],
    columns=('Workspaceid', 'Workspacename')
)
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was after. Will invest time in learning comprehensions!

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.