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?

3
  • 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? Commented Feb 4, 2021 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'? Commented Feb 5, 2021 at 11:47
  • Makes sense! Could you edit your question to reflect that? Commented Feb 5, 2021 at 12:07

1 Answer 1

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.

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

2 Comments

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: '[email protected]'". how do I do that??
@guss I suggest looking at this thread for 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.