0

I have this json file:

print(data)

{'entityId': 'clusterId123', 
'displayName': 'dev_cluster', 
'firstSeenTms': 1584113406351, 
'lastSeenTms': 1627524312116,  
'properties': {'detectedName': 'dev_cluster'}, 
'tags': [], 
'icon': {'primaryIconType': 'hypervisor'}, 
'toRelationships': {
    'isMemberOf': [
        {'id': 'HYPERVISOR_123', 'type': 'HYPERVISOR'}, 
        {'id': 'HYPERVISOR_234', 'type': 'HYPERVISOR'}, 
        {'id': 'HYPERVISOR_345', 'type': 'HYPERVISOR'}
        ]
    }
}

I need to create a data frame that looks like this:

clusterId,  clusterName, hypervisorId
clusterId123 dev_cluster HYPERVISOR_123
clusterId123 dev_cluster HYPERVISOR_234
clusterId123 dev_cluster HYPERVISOR_345

as you can see clusterId and clusterName repeats but the hypervisorId changes just like in the data file.

I am doing this:

#create an empty list
 `cluList=[]`
#apend elements to the list
`cluList.append([data['entityId'], data['displayName']])`

I dont know how to pull the HYPERVISOR_123, HYPERVISOR_234, HYPERVISOR_345 from this data sets. Any guidance appreciated.

1
  • 2
    That's not JSON, it's just a Python dictionary Commented Jul 29, 2021 at 2:55

2 Answers 2

1

Use dict comprehesion:

import pandas as pd

data = {'entityId': 'clusterId123', 
'displayName': 'dev_cluster', 
'firstSeenTms': 1584113406351, 
'lastSeenTms': 1627524312116,  
'properties': {'detectedName': 'dev_cluster'}, 
'tags': [], 
'icon': {'primaryIconType': 'hypervisor'}, 
'toRelationships': {
    'isMemberOf': [
        {'id': 'HYPERVISOR_123', 'type': 'HYPERVISOR'}, 
        {'id': 'HYPERVISOR_234', 'type': 'HYPERVISOR'}, 
        {'id': 'HYPERVISOR_345', 'type': 'HYPERVISOR'}
        ]
    }
}

df = pd.DataFrame({
    "clusterId": data["entityId"],
    "clusterName": data["displayName"],
    "hypervisorId": _id["id"]
} for _id in data["toRelationships"]["isMemberOf"])

df

And result:

    clusterId       clusterName   hypervisorId
0   clusterId123    dev_cluster   HYPERVISOR_123
1   clusterId123    dev_cluster   HYPERVISOR_234
2   clusterId123    dev_cluster   HYPERVISOR_345
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for the answer. Quick question, if I had a loop, bunch of data files, do I have to append to the empty data frame?
@user1471980 data files? Could you be more specific?
1

You can use a list comprehension that loops over the isMemberOf list.

clulist = [(data['entityId'], data['displayName'], hypid) 
            for hypid in data['toRelationships']['isMemberOf']
        ]

2 Comments

clulist would be a type list? can I do this to convert to data frame (cluEntitydf=pd.DataFrame(cluList)?
Yes, you should be able to do that.

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.