How can I select specific elements, arrays or objects from JSON?
I can't figure out how to select a specific element in a JSON object and I can't come up with a search phrase to google.
Example selecting this
{
'entities':[
{
'start':25,
'end':26,
'value':'1',
'entity':'Dosage',
'confidence':0.9871567711054905,
'extractor':'CRFEntityExtractor'
},
{
'start':27,
'end':34,
'value':'capsule',
'entity':'Form',
'confidence':0.9894495817539142,
'extractor':'CRFEntityExtractor'
},
{
'start':38,
'end':43,
'value':'Advil',
'entity':'Drug',
'confidence':0.9801160225829469,
'extractor':'CRFEntityExtractor'
},
{
'start':49,
'end':56,
'value':'6 jours',
'entity':'Duration',
'confidence':0.9675590550065555,
'extractor':'CRFEntityExtractor'
}
],
'text':'le patient a été prescrit 1 capsule de Advil pour 6 jours'
}
From this JSON
data = {
'intent':{
'name':'greet',
'confidence':0.7038594484329224
},
'entities':[
{
'start':25,
'end':26,
'value':'1',
'entity':'Dosage',
'confidence':0.9871567711054905,
'extractor':'CRFEntityExtractor'
},
{
'start':27,
'end':34,
'value':'capsule',
'entity':'Form',
'confidence':0.9894495817539142,
'extractor':'CRFEntityExtractor'
},
{
'start':38,
'end':43,
'value':'Advil',
'entity':'Drug',
'confidence':0.9801160225829469,
'extractor':'CRFEntityExtractor'
},
{
'start':49,
'end':56,
'value':'6 jours',
'entity':'Duration',
'confidence':0.9675590550065555,
'extractor':'CRFEntityExtractor'
}
],
'intent_ranking':[
{
'name':'greet',
'confidence':0.7038594484329224
},
{
'name':'bye',
'confidence':0.6136901378631592
}
],
'text':'le patient a été prescrit 1 capsule de Advil pour 6 jours'
}
I've already tried this method by deleting 'intent' and 'intent_ranking' but I'm afraid that if the JSON is empty the code will crash
if "intent" in data:
del data["intent"], data["intent_ranking"]
return data
I'm trying to select just 'entities' and append it into
result = {}
{'entities': data['entities']}.