How I can get the value of this json key called 'sentence'

Question:

I want to extract the values of the key called ‘sentence’ of this json:

{"title": "llamar | Definiciu00f3n | Diccionario de la lengua espau00f1ola | RAE - ASALE", "articles": [{"id": "NTReP1j", "lema": {"lema": "llamar", "index": 0, "female_suffix": ""}, "supplementary_info": [{"text": "Del lat. (latu00edn) clamu0101re."}], "is": {"verb": true}, "definitions": [{"index": 1, "category": {"abbr": "tr.", "text": "verbo transitivo"}, "is": {"adjective": false, "adverb": false, "interjection": false, "noun": false, "pronoun": false, "verb": true}, "abbreviations": [], "sentence": {"text": "Intentar captar la atenciu00f3n de alguien mediante voces, ruidos o gestos."}, "examples": []}, {"index": 2, "category": {"abbr": "tr.", "text": "verbo transitivo"}, "is": {"adjective": false, "adverb": false, "interjection": false, "noun": false, "pronoun": false, "verb": true}, "abbreviations": [{"abbr": "U. t. c. intr.", "text": "Usado tambiu00e9n como intransitivo"}], "sentence": {"text": "Realizar las operaciones necesarias para establecer comunicaciu00f3n telefu00f3nica con alguien."}, "examples": [{"text": "La llamu00e9, pero no estaba en casa."}, {"text": "Llama a su oficina."}]}, {"index": 3, "category": {"abbr": "tr.", "text": "verbo transitivo"}, "is": {"adjective": false, "adverb": false, "interjection": false, "noun": false, "pronoun": false, "verb": true}, "abbreviations": [], "sentence": {"text": "Invocar, pedir auxilio a alguien."}, "examples": []}, {"index": 4, "category": {"abbr": "tr.", "text": "verbo transitivo"}, "is": {"adjective": false, "adverb": false, "interjection": false, "noun": false, "pronoun": false, "verb": true}, "abbreviations": [{"abbr": "U. t. c. intr.", "text": "Usado tambiu00e9n como intransitivo"}], "sentence": {"text": "Pedir a alguien que vaya a un lugar."}, "examples": [{"text": "Llamar al mu00e9dico, a los refuerzos."}, {"text": "Llamar a reuniu00f3n."}]}, {"index": 5, "category": {"abbr": "tr.", "text": "verbo transitivo"}, "is": {"adjective": false, "adverb": false, "interjection": false, "noun": false, "pronoun": false, "verb": true}, "abbreviations": [], "sentence": {"text": "Despertar a alguien."}, "examples": []}, {"index": 6, "category": {"abbr": "tr.", "text": "verbo transitivo"}, "is": {"adjective": false, "adverb": false, "interjection": false, "noun": false, "pronoun": false, "verb": true}, "abbreviations": [{"abbr": "U. t. c. intr.", "text": "Usado tambiu00e9n como intransitivo"}], "sentence": {"text": "Incitar a alguien a que se comporte de una determinada manera."}, "examples": [{"text": "Llamar a la desobediencia civil."}]}, {"index": 7, "category": {"abbr": "tr.", "text": "verbo transitivo"}, "is": {"adjective": false, "adverb": false, "interjection": false, "noun": false, "pronoun": false, "verb": true}, "abbreviations": [], "sentence": {"text": "Dar a alguien o algo como denominaciu00f3n o calificativo la palabra o enunciado que se expresa."}, "examples": [{"text": "Acu00e1 llamamos celular a lo que allu00e1 llaman mu00f3vil."}, {"text": "Ahora llaman dou00f1a Ana a Anita."}, {"text": "Lo llaman orgulloso."}]}, {"index": 8, "category": {"abbr": "tr.", "text": "verbo transitivo"}, "is": {"adjective": false, "adverb": false, "interjection": false, "noun": false, "pronoun": false, "verb": true}, "abbreviations": [], "sentence": {"text": "Dar a alguien el tratamiento que se expresa."}, "examples": [{"text": "Llu00e1mame de tu00fa."}]}, {"index": 9, "category": {"abbr": "tr.", "text": "verbo transitivo"}, "is": {"adjective": false, "adverb": false, "interjection": false, "noun": false, "pronoun": false, "verb": true}, "abbreviations": [], "sentence": {"text": "Designar a alguien para ocupar un puesto, desempeu00f1ar un cargo o ejercer un derecho."}, "examples": [{"text": "Fue llamada a suceder a su hermano."}]}, {"index": 10, "category": {"abbr": "tr.", "text": "verbo transitivo"}, "is": {"adjective": false, "adverb": false, "interjection": false, "noun": false, "pronoun": false, "verb": true}, "abbreviations": [], "sentence": {"text": "Atraer a alguien o algo."}, "examples": [{"text": "El chocolate no me llama en absoluto."}]}, {"index": 11, "category": {"abbr": "intr.", "text": "verbo intransitivo"}, "is": {"adjective": false, "adverb": false, "interjection": false, "noun": false, "pronoun": false, "verb": true}, "abbreviations": [], "sentence": {"text": "Hacer una seu00f1al sonora en una puerta, golpeu00e1ndola o accionando un instrumento sonoro, para que alguien la abra."}, "examples": []}, {"index": 12, "category": {"abbr": "prnl.", "text": "verbo pronominal"}, "is": {"adjective": false, "adverb": false, "interjection": false, "noun": false, "pronoun": false, "verb": true}, "abbreviations": [], "sentence": {"text": "Tener el nombre o la denominaciu00f3n que se expresa."}, "examples": []}, {"index": 13, "category": {"abbr": "prnl.", "text": "verbo pronominal"}, "is": {"adjective": false, "adverb": false, "interjection": false, "noun": false, "pronoun": false, "verb": true}, "abbreviations": [{"abbr": "Mar.", "text": "Marina"}, {"abbr": "desus.", "text": "desusado"}], "sentence": {"text": "Dicho del viento: Cambiar de direcciu00f3n hacia la parte que se expresa."}, "examples": []}], "complex_forms": [], "other_entries": [{"text": "treta del llamar", "link": "https://dle.rae.es/?id=abLU9KP#40fzk3z"}], "conjugations": {"verb": "llamar", "conjugations": {"Formas no personales": {"Infinitivo": "", "Gerundio": "", "Participio": "", "": "llamado"}, "Indicativo": {"Presente": {"yo": "llamo", "tu00fa / vos": ["llamas", "llamu00e1s"], "usted": "llama", "u00e9l, ella": "llama", "nosotros, nosotras": "llamamos", "vosotros, vosotras": "llamu00e1is", "ustedes": "llaman", "ellos, ellas": "llaman"}, "Copretu00e9rito": {"yo": "llamaba", "tu00fa / vos": "llamabas", "usted": "llamaba", "u00e9l, ella": "llamaba", "nosotros, nosotras": "llamu00e1bamos", "vosotros, vosotras": "llamabais", "ustedes": "llamaban", "ellos, ellas": "llamaban"}, "Pretu00e9rito": {"yo": "llamu00e9", "tu00fa / vos": "llamaste", "usted": "llamu00f3", "u00e9l, ella": "llamu00f3", "nosotros, nosotras": "llamamos", "vosotros, vosotras": "llamasteis", "ustedes": "llamaron", "ellos, ellas": "llamaron"}, "Futuro": {"yo": "llamaru00e9", "tu00fa / vos": "llamaru00e1s", "usted": "llamaru00e1", "u00e9l, ella": "llamaru00e1", "nosotros, nosotras": "llamaremos", "vosotros, vosotras": "llamaru00e9is", "ustedes": "llamaru00e1n", "ellos, ellas": "llamaru00e1n"}, "Pospretu00e9rito": {"yo": "llamaru00eda", "tu00fa / vos": "llamaru00edas", "usted": "llamaru00eda", "u00e9l, ella": "llamaru00eda", "nosotros, nosotras": "llamaru00edamos", "vosotros, vosotras": "llamaru00edais", "ustedes": "llamaru00edan", "ellos, ellas": "llamaru00edan"}}, "Subjuntivo": {"Presente": {"yo": "llame", "tu00fa / vos": "llames", "usted": "llame", "u00e9l, ella": "llame", "nosotros, nosotras": "llamemos", "vosotros, vosotras": "llamu00e9is", "ustedes": "llamen", "ellos, ellas": "llamen"}, "Futuro": {"yo": "llamare", "tu00fa / vos": "llamares", "usted": "llamare", "u00e9l, ella": "llamare", "nosotros, nosotras": "llamu00e1remos", "vosotros, vosotras": "llamareis", "ustedes": "llamaren", "ellos, ellas": "llamaren"}, "Copretu00e9rito": {}, "": {"yo": ["llamara", "llamase"], "tu00fa / vos": ["llamaras", "llamases"], "usted": ["llamara", "llamase"], "u00e9l, ella": ["llamara", "llamase"], "nosotros, nosotras": ["llamu00e1ramos", "llamu00e1semos"], "vosotros, vosotras": ["llamarais", "llamaseis"], "ustedes": ["llamaran", "llamasen"], "ellos, ellas": ["llamaran", "llamasen"]}}, "Imperativo": {"": {"tu00fa / vos": ["llama", "llamu00e1"], "usted": "llame", "vosotros, vosotras": "llamad", "ustedes": "llamen"}}}}}]}

enter image description here

By the moment I only was able to convert the dic to json:

from pyrae import dle
import json

res = dle.search_by_word(word='llamar')
res = res.to_dict()

json_string = json.dumps(res)

data = json.loads(json_string)

print(data['sentence']) # throws error

This library pyrae allows me to get meaning of a spanish word.

I will appreciate any idea guys to solve it,
thanks so much.

Asked By: FreddicMatters

||

Answers:

JSON read the json string to python’s dict and You need to go through every indirect object.

example = {
    "A"{
        "B":[{"val":1,
            "other_val":2},
            {"val":3,
            "other_val":4}
        ]
    }
}

If you want get every value in above example u need to iterate over every obj in B arra/list just lik:

for b in example["A"]["B"]:
    v, ov = b["val"], b["other_val"]
    # do something with values or store it somewhere

Your example is too large so i show you the answer on my own, smaller one.

Answered By: Paweł Pietraszko
import json

# open json
f = open('data_in.json')

# load json
data = json.load(f)

# print sentences
for i in data['articles']:
    for j in i['definitions']:
        print(j['sentence']['text'])

# close file
f.close()

Output:

Intentar captar la atención de alguien mediante voces, ruidos o gestos.
Realizar las operaciones necesarias para establecer comunicación telefónica con alguien.
Invocar, pedir auxilio a alguien.
Pedir a alguien que vaya a un lugar.
Despertar a alguien.
Incitar a alguien a que se comporte de una determinada manera...
Answered By: Captain Caveman

Since I assume the depth of the dict is unknown and it and the values can be text,dict,list , you will have to use a recursion function

def myprint(data):
    for keys,values in data.items():
        if isinstance(values, dict):
            myprint(values)
        if isinstance(values, list):
            for items in values:
                myprint(items)
        else:
            if keys == "sentence":
                stop=1
myprint(data)
Answered By: Nir Elbaz

Directly from the dictionary it can be extracted like this:

    from pyrae import dle
    res = dle.search_by_word(word='llamar')
    resultado=res.to_dict()
    print(resultado['articles'][0]["definitions"][0]["sentence"]["text"])

Result:

 Intentar captar la atención de alguien mediante voces, ruidos o gestos. 

If more than one "sentence" it would be necessary to go through it with a loop

    for i in range(len(resultado['articles'][0])-1):
        print(i, resultado['articles'][0]["definitions"] [i]["sentence"]["text"])

Resultado:

0 Intentar captar la atención de alguien mediante voces, ruidos o gestos.
1 Realizar las operaciones necesarias para establecer comunicación telefónica con alguien.
2 Invocar, pedir auxilio a alguien.
3 Pedir a alguien que vaya a un lugar.
4 Despertar a alguien.
5 Incitar a alguien a que se comporte de una determinada manera.
6 Dar a alguien o algo como denominación o calificativo la palabra o enunciado que se expresa.
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.