0

I'm doing a flask application. I need to get the elements of the session that are inside an array in mongodb. I tried to do everything, but I was unsuccessful.

I need to get the 'password' inside 'contatos'. To do a validation within my application. I know that if you do the following code: print(session['company'].Get("contatos")), it shows all the content of the object, but I can't access the elements inside that object.

    {
    "nome": "Empresa Teste",
    "cnpj": "00.000.000/0001-00",
    "endereco": {
        "endereco": "Rua, 1",
        "cidade": "Cidade Teste",
        "estado": "Estado Teste",
        "uf": "AA"
    },
    "idMatriz": 1,
    "dtCadastro": "01/01/2020",
    "plano": {
        "id": 1,
        "nome": "Plano de Teste",
        "validade": "01/01/2020"
    },
    "contatos": [{
        "nome": "Fabio",
        "email": "Teste",
        "password": "pass",
        "telefone": "Teste"
    }],
    "contasCadastradas ": [{
        "concessionaria": {
            "id": 1,
            "nome": "EnelSP"
        },
        "identificador": "Numero da Instalacao",
        "cronogramaBusca": {
            "inicio": 1,
            "vencimento": 1,
            "fim": 1
        },
        "metodosCaptura": [{
            "tipoCaptura": "site",
            "ordem": 1,
            "usuario": "user",
            "senha": "pwd",
            "destino": "endereco do site"
        }, {
            "tipoCaptura": "email",
            "ordem": 2,
            "usuario": "user",
            "senha": "pwd",
            "destino": ""
        }]
    }, {
        "concessionaria": {
            "id": 2,
            "nome": "TIM"
        },
        "identificador": "Numero da Instalacao",
        "cronogramaBusca": {
            "inicio": 1,
            "vencimento": 1,
            "fim": 1
        },
        "metodosCaptura   ": [{
            "tipoCaptura": "site",
            "ordem": 1,
            "usuario": "user",
            "senha": "pwd",
            "destino": "endereco do site"
        }, {
            "tipoCaptura": "email",
            "ordem": 2,
            "usuario": "user",
            "senha": "pwd",
            "destino": ""
        }]
    }]
}

can you help me?

dejanualex
  • 3,872
  • 6
  • 22
  • 37
guss
  • 1
  • 2
  • Welcome to Stack Overflow! It doesn't seem that there's a `password` property inside of `contatos` or anywhere else in the object you provided. Could you clarify what is this object and what piece of data do you expect to access? – Nikolay Shebanov Feb 04 '21 at 17:47
  • 1
    sorry, I ended up showing the previous collection. 'password' would be in 'contact'. looking at this collection I sent, how would you get the 'nome' within 'contatos'? – guss Feb 05 '21 at 11:47
  • Makes sense! Could you edit your question to reflect that? – Nikolay Shebanov Feb 05 '21 at 12:07

1 Answers1

1

So if I've read correctly, print(session['company'].Get("contatos")) gives you the output which you have included in the question.

So if you assign that to a variable called data:

data = session['company'].Get("contatos")

You could then access:

>>> data['contatos']
[{'nome': 'Fabio', 'email': 'Teste', 'telefone': 'Teste'}]

That's a list containting a single dictionary, so you could access the first (and only) dictionary in that list:

>>> data['contatos'][0]
{'nome': 'Fabio', 'email': 'Teste', 'telefone': 'Teste'}

And to get an individual field:

>>> data['contatos'][0]['nome']
'Fabio'

I can't see the password field you mention.

v25
  • 7,096
  • 2
  • 20
  • 36
  • thank you very much, it worked. but one thing, if I put '[0]' it will take the first object in an array. how do I make a dynamic method for him? for example: suppose I have 3 objects inside the 'contatos' array, I want to get 'nome' from the object that contains the "email: 'gus@gmail.com'". how do I do that?? – guss Feb 05 '21 at 11:54
  • @guss I suggest looking at [this thread](https://stackoverflow.com/questions/8653516/python-list-of-dictionaries-search) for that. – v25 Feb 05 '21 at 17:26