Validate array within a JSON in node.js

1

I need to validate the fields within the "Extras" array, they are really "Number" and "Country_code", I also need to be able to obtain the values. HELP PLEASE !!

"Persona": [
    {
        "Nombre":"Maria", 
        "Apellido":"Vargaz",
        "Extras":[
            {
                "Numero":6666666,
                "Codigo_Pais":506
            }
        ],
        "Direccion":{
            "Pais":"Costa Rica"
        }   
    },
    {
        "Nombre":"Maria", 
        "Apellido":"Vargaz",
        "Extras":[
            {
                "Numero":6666666,
                "Codigo_Pais":506
            }
        ],
        "Direccion":{
            "Pais":"Costa Rica"
        }   
    }
    ]
    
asked by java005 01.09.2018 в 17:36
source

1 answer

1

It is difficult to propose something without knowing how validation has to be or what you want to do in a positive or negative case. I leave you an example of how to access the values, but it would change a lot depending on what you want to do exactly:

var obj = {"Persona": [
    {
        "Nombre":"Maria", 
        "Apellido":"Vargaz",
        "Extras":[
            {
                "Numero":6666666,
                "Codigo_Pais":506
            }
        ],
        "Direccion":{
            "Pais":"Costa Rica"
        }   
    },
    {
        "Nombre":"Maria", 
        "Apellido":"Vargaz",
        "Extras":[
            {
                "Numero":6666666,
                "Codigo_Pais":506
            }
        ],
        "Direccion":{
            "Pais":"Costa Rica"
        }   
    }
    ]}
  
    obj.Persona.forEach(e => {
      e.Extras.forEach(v=>{
        console.log(v.Numero)
        console.log(v.Codigo_Pais)
      })
    });
    
answered by 02.09.2018 / 02:09
source