Structure of a chained JSON

Asked

Viewed 795 times

1

I was developing a web application, where I needed to use JSON, I had some problems, but I got it. Now I need to leave everything on the page of the dynamic application, so everything has to be passed by JSON, but I’ve never done a JSON with multiple threads, so I don’t know if it’s correct the way I did and I don’t know if there’s any difference when it comes to catching up with JS.

Follow the JSON I’m creating to test:

{
    "cidade1":[{
                "txtheader":{"Lorem Ipsum dolor"},
                "chart1":[{"valor1":30,"valor2":350}],
                "chart2":[{"valor1":500,"valor2":400,"valor3":170,"valor4":460}],
                "chart3":[{"valor1":1000,"valor2":400,"valor3":1170,"valor4":1460,"valor5":660,"valor6":1120,"valor7":1030,"valor8":540}],
                "tabela":[
                            {"Fornecedor": "fff","Trecho":"ttt","Data":"12/09/2015","Passageiro": "Pedro","Confirmação":"Não","Localizador":"Sim",
                            "LocalizadorGds":"Não","Numero da solicitacao":1002,"Status da Integracao": "Ativo","Identificador": "Não"}
                        ]
               }],
    "cidade2":[{
                "txtheader":{"Lorem Ipsum dolor"},
                "chart1":[{"valor1":30,"valor2":350}],
                "chart2":[{"valor1":500,"valor2":400,"valor3":170,"valor4":460}],
                "chart3":[{"valor1":1000,"valor2":400,"valor3":1170,"valor4":1460,"valor5":660,"valor6":1120,"valor7":1030,"valor8":540}],
                "tabela":[
                            {"Fornecedor": "fff","Trecho":"ttt","Data":"12/09/2015","Passageiro": "Pedro","Confirmação":"Não","Localizador":"Sim",
                            "LocalizadorGds":"Não","Numero da solicitacao":1002,"Status da Integracao": "Ativo","Identificador": "Não"}
                        ]
               }]
}

There would enter several different cities, in case I only put two, but they will have the same structure.

Illustrated example:

All the data on this page needs to come from JSON, up to the text I called txtheader and the data for the charts.

11

In this image below, it shows the <option> which will also be dynamic and when selected it loads the json and loads the data.

inserir a descrição da imagem aqui

This is the table that is linked to the chart: inserir a descrição da imagem aqui

  • 1

    Each city will have more objects within this array with its own txtheader etc? It gives me idea that you have arrays with only 1 object inside where it would be enough to have only the object... That is, for example directly "cidade1":{ unused [ to be an array.

  • @Sergio I don’t know if I understand, but each city will have 5 "variables", (txtheader/chart1/chart2/chart3/table), confusing me a little with the use of {} and [], but my idea is that txtheader is not an array, and the other 4 are. In order to fill some graphs with Charts values and download the "table", in a table.

  • @Sergio I believe I’m confusing you for some wrong syntax in JSON. As I am creating this JSON in hand to be able to test, I believe I have messed up with something.

  • face it is easier to assemble the structure in array in php, and then use the json_encode().

  • @Ivanferrer Could you show me an example? I could directly put json in the variable declaration and use json_encode(). But I have more doubt in the part of the same structure, as it will stay these arrays one inside the other.

  • Look at the answer.

  • @Giovannibernini your JSON is correct if each city has several objects inside. Otherwise you should remove these arrays. Hence my question. If you explain the data structure you want to build, we can help you create JSON. Instead of watching the JSON that you have and not really knowing where you’re going.

  • @Ivanferrer himself admitted that he is not confident in JSON. It would be interesting to know the structure of the data so that we can help him.

  • @Sergio I will edit my post with an explanation about the structure, I will illustrate to better convey my message.

  • @Sergio Editado. Thanks!

Show 5 more comments

2 answers

3


Always keep in mind that the JSON information representation follows the following rule: for each value, a name (or label) is assigned that describes its meaning.

Example:

"nome" : "José"

For example: If I needed more information about José, type age (whole) and if he has a driving license (boolean), the information would look like this:

"nome": "José",
"idade" : 52,
"habilitado": true

See how it works? So, from these basic types, it’s possible to build more complex types, like arrays and objects.

Arrays are bounded by square brackets, with their elements separated between commas.

Example:

["Maria", "João", "Joaquim"]

Objects are delimited by keys and can be composed of multiple name/value pairs, as well as arrays and other objects.

Example:

    {
        "nome": "José",
        "idade": 52,
        "habilitado": true,
        "filhos": [
            "Maria",
            "João",
            "Joaquim"
        ]
    }

This way, you can create an array of objects.

Example:

[
    {
        "nome": "José",
        "idade": 52,
        "habilitado": true,
        "filhos": [
            "Maria",
            "João",
            "Joaquim"
        ]
    },
    {
        "nome": "Sebastião",
        "idade": 50,
        "habilitado": false,
        "filhos": [
            "Pedro"
        ]
    }
]

To validate your JSON, you can use this site: http://jsonlint.com/

References: http://www.devmedia.com.br/introducao-ao-formato-json/25275

  • Again I do not agree with the above statement. json is wrong.

  • @Nikofoxxxx, I tested both examples and both are returning with error.

  • Hello @Ivanferrer, forgive me for the first example, it really was my fault. However the second JSON is valid. I’ve already edited the answer.

  • 1

    Yes, it’s true. The second I tested and it worked. I forgot to copy the last key.

  • Quiet! Thank you for noticing the error!

  • You know why the first example gave error?

  • 1

    Actually it was because I had not placed the example objects inside a delimiter, separating them by comma.

  • Now is correct.

  • I gave +1 for the correction

  • Thanks for everything! D

Show 5 more comments

1

I suggest you do the array structure, and then convert to json:

$array_data = array(
'cidade1'=>array(
                 'textheader'=>'Lorem Ipsum dolor',
                 'chart1'=>array('valor1'=>30, 'valor2'=>350),
                 'chart2'=>array('valor1'=>500,'valor2'=>400,'valor3'=>170, 'valor4'=>460),
                 'chart3'=>array('valor1'=>1000,'valor2'=>400,'valor3'=>1170, 'valor4'=>1460,'valor5'=>660,'valor6'=>1120,'valor7'=>1030,'valor8'=>540),
                 'tabela'=>array("Fornecedor"=>"fff","Trecho"=>"ttt","Data"=>"12/09/2015","Passageiro"=> "Pedro","Confirmação"=>"Não","Localizador"=>"Sim",
                            "LocalizadorGds"=>"Não","Numero da solicitacao"=>1002,"Status da Integracao"=>"Ativo","Identificador"=>"Não")
           ),
'cidade2'=>array(
                 'textheader'=>'Lorem Ipsum dolor',
                 'chart1'=>array('valor1'=>30, 'valor2'=>350),
                 'chart2'=>array('valor1'=>500,'valor2'=>400,'valor3'=>170, 'valor4'=>460),
                 'chart3'=>array('valor1'=>1000,'valor2'=>400,'valor3'=>1170, 'valor4'=>1460,'valor5'=>660,'valor6'=>1120,'valor7'=>1030,'valor8'=>540),
                 'tabela'=>array("Fornecedor"=>"fff","Trecho"=>"ttt","Data"=>"12/09/2015","Passageiro"=> "Pedro","Confirmação"=>"Não","Localizador"=>"Sim",
                            "LocalizadorGds"=>"Não","Numero da solicitacao"=>1002,"Status da Integracao"=>"Ativo","Identificador"=>"Não")
           )
);
echo json_encode($array_data);

If you are using PHP 4.5 or higher:

 $array_data = [
    'cidade1'=> [
                     'textheader'=>'Lorem Ipsum dolor',
                     'chart1'=> ['valor1'=>30, 'valor2'=>350],
                     'chart2'=> ['valor1'=>500,'valor2'=>400,'valor3'=>170, 'valor4'=>460],
                     'chart3'=> ['valor1'=>1000,'valor2'=>400,'valor3'=>1170, 'valor4'=>1460,'valor5'=>660,'valor6'=>1120,'valor7'=>1030,'valor8'=>540],
                     'tabela'=> ["Fornecedor"=>"fff","Trecho"=>"ttt","Data"=>"12/09/2015","Passageiro"=> "Pedro","Confirmação"=>"Não","Localizador"=>"Sim",
                                "LocalizadorGds"=>"Não","Numero da solicitacao"=>1002,"Status da Integracao"=>"Ativo","Identificador"=>"Não"]
               ],
    'cidade2'=> [
                     'textheader'=>'Lorem Ipsum dolor',
                     'chart1'=> ['valor1'=>30, 'valor2'=>350],
                     'chart2'=> ['valor1'=>500,'valor2'=>400,'valor3'=>170, 'valor4'=>460],
                     'chart3'=> ['valor1'=>1000,'valor2'=>400,'valor3'=>1170, 'valor4'=>1460,'valor5'=>660,'valor6'=>1120,'valor7'=>1030,'valor8'=>540],
                     'tabela'=> ["Fornecedor"=>"fff","Trecho"=>"ttt","Data"=>"12/09/2015","Passageiro"=> "Pedro","Confirmação"=>"Não","Localizador"=>"Sim",
                                "LocalizadorGds"=>"Não","Numero da solicitacao"=>1002,"Status da Integracao"=>"Ativo","Identificador"=>"Não"]
               ]
    ];
 echo json_encode($array_data);

Converted would be this:

  {
   "cidade1":
       {
        "textheader":"Lorem Ipsum dolor",
        "chart1":{"valor1":30,"valor2":350},
        "chart2":{"valor1":500,"valor2":400,"valor3":170,"valor4":460},
        "chart3":{"valor1":1000,"valor2":400,"valor3":1170,"valor4":1460,"valor5":660,"valor6":1120,"valor7":1030,"valor8":540},
        "tabela":{"Fornecedor":"fff","Trecho":"ttt","Data":"12\/09\/2015","Passageiro":"Pedro","Confirma\u00e7\u00e3o":"N\u00e3o","Localizador":"Sim","LocalizadorGds":"N\u00e3o","Numero da solicitacao":1002,"Status da Integracao":"Ativo","Identificador":"N\u00e3o"}
        },
   "cidade2":
       {
       "textheader":"Lorem Ipsum dolor",
        "chart1":{"valor1":30,"valor2":350},
        "chart2":{"valor1":500,"valor2":400,"valor3":170,"valor4":460},
        "chart3":{"valor1":1000,"valor2":400,"valor3":1170,"valor4":1460,"valor5":660,"valor6":1120,"valor7":1030,"valor8":540},
        "tabela":{"Fornecedor":"fff","Trecho":"ttt","Data":"12\/09\/2015","Passageiro":"Pedro","Confirma\u00e7\u00e3o":"N\u00e3o","Localizador":"Sim","LocalizadorGds":"N\u00e3o","Numero da solicitacao":1002,"Status da Integracao":"Ativo","Identificador":"N\u00e3o"}
       }
  }

Now let’s assume that you want to get the status of the first table request only through javascript.

Run this on your console:

var col =  {
       "cidade1":
           {
            "textheader":"Lorem Ipsum dolor",
            "chart1":{"valor1":30,"valor2":350},
            "chart2":{"valor1":500,"valor2":400,"valor3":170,"valor4":460},
            "chart3":{"valor1":1000,"valor2":400,"valor3":1170,"valor4":1460,"valor5":660,"valor6":1120,"valor7":1030,"valor8":540},
            "tabela":{"Fornecedor":"fff","Trecho":"ttt","Data":"12\/09\/2015","Passageiro":"Pedro","Confirma\u00e7\u00e3o":"N\u00e3o","Localizador":"Sim","LocalizadorGds":"N\u00e3o","Numero da solicitacao":1002,"Status da Integracao":"Ativo","Identificador":"N\u00e3o"}
            },
       "cidade2":
           {
           "textheader":"Lorem Ipsum dolor",
            "chart1":{"valor1":30,"valor2":350},
            "chart2":{"valor1":500,"valor2":400,"valor3":170,"valor4":460},
            "chart3":{"valor1":1000,"valor2":400,"valor3":1170,"valor4":1460,"valor5":660,"valor6":1120,"valor7":1030,"valor8":540},
            "tabela":{"Fornecedor":"fff","Trecho":"ttt","Data":"12\/09\/2015","Passageiro":"Pedro","Confirma\u00e7\u00e3o":"N\u00e3o","Localizador":"Sim","LocalizadorGds":"N\u00e3o","Numero da solicitacao":1002,"Status da Integracao":"Ativo","Identificador":"N\u00e3o"}
           }
      };

console.log(col.cidade1.tabela['Numero da solicitacao']);

Here an example of use with array:

var seu_json = {
                "cidade1":
                    [{
                      "id":1,
                      "textheader":"Lorem Ipsum dolor",
                      "chart1":[{"valor1":30,"valor2":350 }],
                      "chart2":[{"valor1":500,"valor2":400,"valor3":170, "valor4":460 }],
                      "chart3":[{"valor1":1000,"valor2":400,"valor3":1170, "valor4":1460, "valor5":660,"valor6":1120,"valor7":1030,"valor8":540 }],
                      "tabela":[{"Fornecedor":"fff","Trecho":"ttt","Data":"12/09/2015","Passageiro":"Pedro","Confirmação":"Não","Localizador":"Sim",
                                "LocalizadorGds":"Não","Numero da solicitacao":1002,"Status da Integracao":"Ativo","Identificador":"Não"}]
                    }],
            "cidade2":
                    [{
                      "id":2,
                      "textheader":"Lorem Ipsum dolor",
                      "chart1":[{"valor1":30,"valor2":350 }],
                      "chart2":[{"valor1":500,"valor2":400,"valor3":170, "valor4":460 }],
                      "chart3":[{"valor1":1000,"valor2":400,"valor3":1170, "valor4":1460, "valor5":660,"valor6":1120,"valor7":1030,"valor8":540 }],
                      "tabela":[{"Fornecedor":"fff","Trecho":"ttt","Data":"12/09/2015","Passageiro":"Pedro","Confirmação":"Não","Localizador":"Sim",
                                "LocalizadorGds":"Não","Numero da solicitacao":1002,"Status da Integracao":"Ativo","Identificador":"Não"}]
                    }]
           };
console.log(seu_json);
  • I understand, but Ivan, this 'table' will have more data, to really fill out a table, how would I do it? if it was just a table in json I would do [ { "id": "1", "name": "Peter", }, { "id": "2", "name": "Val", }] for example, but inside the 'table' I don’t know how to structure it anymore.

  • I believe I got it, I put it like this: "table": [ { "Supplier": "fff"}] with [, Validei in Jsonlint and it worked, I will finish writing and I will test, thanks!

Browser other questions tagged

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