How to check if JSON has this structure?

Asked

Viewed 1,418 times

3

I have a json file that varies its structure

{
    "Plan1": [
        {
            "Sequencia": "1",
            "Mês de aniversario": "agosto",
            "Nome Completo": "Joao da silva"
         }
     ]
}

And you wanted to see if these indices have those values? what’s the best way to do this with jquery?

I want to check if json has this structure always, so the indices have to be these Sequence Birthday month Full Name

  • 3

    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?

  • 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?

  • I switched to make it clearer @Sergio I need json to have this structure

  • 1

    @Guilhermefreire, if you want to validate the structure of a JSON, must assemble a JSON Schema for this, a good implementation for Javascript is the JJV JSON Validator... here is a fiddle with an example: Jsfiddle

  • 2

    @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 think the schema will work, I want you to give me an error if the json doesn’t have this structure

  • 1

    @Guilhermefreire in this case is something like this that you are looking for? https://jsfiddle.net/ksj8tydd/

Show 2 more comments

1 answer

1


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:

  1. JJV: JJV JSON Validator
  2. z-schema Validator
  3. 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>

  • It worked perfectly, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.