Guilherme, if you want to validate the structure of a JSON, I advise you to assemble a JSON Schema
for the same, you can read more about: JSON Schema
On the site above you have a list of libraries to perform the validation of JSON, in the case of Javascript, I recommend one of the three below:
- JJV: JJV JSON Validator
- z-schema Validator
- Ajv: Another JSON Schema Validator
to validate the JSON you entered the question, you can use the following Schema:
{
type: 'object',
properties: {
'Plan1': {
'type': 'array',
'minItems': 1,
'items': {
type: 'object',
properties: {
'Sequencia': {
type: 'number'
},
'Mês de aniversario': {
type: 'string',
enum: [
'janeiro', 'fevereiro', 'março', 'abril', 'maio', 'junho',
'julho', 'agosto', 'setembro', 'outubro', 'novembro', 'dezembro'
]
},
'Nome Completo': {
type: 'string'
}
},
required: ['Sequencia', 'Mês de aniversario', 'Nome Completo']
}
}
},
required: ['Plan1']
}
Note that in the above case all properties are required and Sequential must be numeric.
below follows an example of implementation using the AJV
var schema = {
type: 'object',
properties: {
'Plan1': {
'type': 'array',
'minItems': 1,
'items': {
type: 'object',
properties: {
'Sequencia': {
type: 'number'
},
'Mês de aniversario': {
type: 'string',
enum: [
'janeiro', 'fevereiro', 'março', 'abril', 'maio', 'junho',
'julho', 'agosto', 'setembro', 'outubro', 'novembro', 'dezembro'
]
},
'Nome Completo': {
type: 'string'
}
},
required: ['Sequencia', 'Mês de aniversario', 'Nome Completo']
}
}
},
required: ['Plan1']
};
var json1 = {
"Plan1": [
{
"Sequencia": 1,
"Mês de aniversario": "agosto",
"Nome Completo": "Joao da silva"
}
]
}
var json2 = {
"Plan1": [
{
"Sequencia": "2",
"Mês de aniversario": "13º mês",
"Nome Completo": "Joao da silva"
}
]
}
var ajv = new Ajv({allErrors: true});
var validate = ajv.compile(schema);
if (validate(json1))
console.log("json valido!");
else {
console.log("json invalido: " + JSON.stringify(validate.errors));
}
if (validate(json2))
console.log("json valido!");
else {
console.log("json invalido: " + JSON.stringify(validate.errors));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/4.0.5/ajv.min.js"></script>
Hello William. I could not understand the type of variation you refer to. The problem is only the position of the data in the vector?
– utluiz
Can you [Dit] the question and clarify what you need to do? You can give more examples of what you want and how the data might appear?
– Sergio
I switched to make it clearer @Sergio I need json to have this structure
– Guilherme Freire
@Guilhermefreire, if you want to validate the structure of a
JSON
, must assemble aJSON Schema
for this, a good implementation for Javascript is theJJV JSON Validator
... here is a fiddle with an example: Jsfiddle– Tobias Mesquita
@Guilhermefreire and what do you want to do if one of the Jsons doesn’t have this structure? take it out of the array, complete it, or record the error?
– Sergio
@Sergio think the schema will work, I want you to give me an error if the json doesn’t have this structure
– Guilherme Freire
@Guilhermefreire in this case is something like this that you are looking for? https://jsfiddle.net/ksj8tydd/
– Sergio