0

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 = {}
1
  • I'd say the simplest solution would be: {'entities': data['entities']}. Commented Sep 6, 2019 at 10:33

3 Answers 3

1

Doesn't just selecting what you whant work?

data = data['entities']

and if you needed exactly as you stated you can build it,

new_data = {
 'entities' : data['entities'],
 'text' : data['text']
}
data = new_data 

you keep the name if needed

Hope it helped, not sur I have understood your question but if it did help, let me know.

Sign up to request clarification or add additional context in comments.

Comments

0

Just for information, doing

if "key" in data:
    del data["key"]

will not crash if data is empty.

However, the answers above are more suitable imo.

Comments

0

In order to get the part you need, you can just create a new dictionary called result, and add the entities and text content of the data variable. The code will not crash if the data['entities'] and data['text'] always exit:

result = data['entities']
result.append(data['text'])
print (result)

1 Comment

Can you expand your answer to include descriptive prose to explain why this addressed the original asker's question?

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.