Zig
Zig
Loading and Parsing a Complex JSON Array
See more JSON Examples
This example loads a JSON array containing more complex data. It shows how to parse (access) various values contained within the JSON.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This is the JSON we'll be loading:
// [
// {
// "telefones": [
// {
// "numero": "19995555555",
// "tipo": "T",
// "id": 2541437
// }
// ],
// "cnpj": "11395551000164",
// "rua": "R XAVIER AUGUSTO ROGGE, 22",
// "complemento": "",
// "contatos": [
// ],
// "tipo": "J",
// "razao_social": "SOUP BRASIL LTDA - ME",
// "nome_fantasia": "SOUP BRASIL",
// "bairro": "ABC DOS COLIBRIS",
// "cidade": "TEST",
// "inscricao_estadual": "222.102.222.116",
// "observacao": "",
// "id": 2209595,
// "ultima_alteracao": "2016-12-26 16:22:34",
// "cep": "13555000",
// "suframa": "",
// "estado": "SP",
// "emails": [
// {
// "email": "somebody@terra.com.br",
// "tipo": "T",
// "id": 1065557
// }
// ],
// "excluido": false
// },
// {
// "telefones": [
// ],
// "cnpj": "12496555500180",
// "rua": "AV ROLF WIEST, 100",
// "complemento": "ANDAR 7 SALA 612 A 620",
// "contatos": [
// ],
// "tipo": "J",
// "razao_social": "SIMPLE SOFTWARE LTDA",
// "nome_fantasia": "",
// "bairro": "DOM ZETIRO",
// "cidade": "APARTVILLE",
// "inscricao_estadual": "",
// "observacao": "",
// "id": 2255594,
// "ultima_alteracao": "2016-12-26 16:28:31",
// "cep": "89255505",
// "suframa": "",
// "estado": "SC",
// "emails": [
// ],
// "excluido": false
// },
// {
// "telefones": [
// {
// "numero": "1938655556",
// "tipo": "T",
// "id": 2555438
// }
// ],
// "cnpj": "00003555500153",
// "rua": "AV ABCDEF PINTO CATAO, 18",
// "complemento": "",
// "contatos": [
// {
// "telefones": [
// {
// "numero": "1999655554",
// "tipo": "T",
// "id": 2555559
// }
// ],
// "cargo": "zzz de compras",
// "nome": "Gerard",
// "emails": [
// {
// "email": "gerard@terra.com.br",
// "tipo": "T",
// "id": 1065559
// }
// ],
// "id": 844485,
// "excluido": false
// }
// ],
// "tipo": "J",
// "razao_social": "TIDY TECNOLOGIA LTDA - EPP",
// "nome_fantasia": "TIDY",
// "bairro": "TUNA",
// "cidade": "JAGUAR",
// "inscricao_estadual": "395.222.441.222",
// "observacao": "ligar sempre depois das 14hs",
// "id": 2255597,
// "ultima_alteracao": "2016-12-28 07:31:52",
// "cep": "13555500",
// "suframa": "",
// "estado": "SP",
// "emails": [
// {
// "email": "xi@tidy.com.br",
// "tipo": "T",
// "id": 10655558
// }
// ],
// "excluido": false
// }
// ]
//
// Construct a StringBuilder containing the above JSON array.
const sb = try chilkat.StringBuilder.init();
defer sb.deinit();
const b_crlf = true;
sb.appendLine("[", b_crlf) catch {};
sb.appendLine(" {", b_crlf) catch {};
sb.appendLine(" \"telefones\": [", b_crlf) catch {};
sb.appendLine(" {", b_crlf) catch {};
sb.appendLine(" \"numero\": \"19995555555\",", b_crlf) catch {};
sb.appendLine(" \"tipo\": \"T\",", b_crlf) catch {};
sb.appendLine(" \"id\": 2541437", b_crlf) catch {};
sb.appendLine(" }", b_crlf) catch {};
sb.appendLine(" ],", b_crlf) catch {};
sb.appendLine(" \"cnpj\": \"11395551000164\",", b_crlf) catch {};
sb.appendLine(" \"rua\": \"R XAVIER AUGUSTO ROGGE, 22\",", b_crlf) catch {};
sb.appendLine(" \"complemento\": \"\",", b_crlf) catch {};
sb.appendLine(" \"contatos\": [", b_crlf) catch {};
sb.appendLine(" ],", b_crlf) catch {};
sb.appendLine(" \"tipo\": \"J\",", b_crlf) catch {};
sb.appendLine(" \"razao_social\": \"SOUP BRASIL LTDA - ME\",", b_crlf) catch {};
sb.appendLine(" \"nome_fantasia\": \"SOUP BRASIL\",", b_crlf) catch {};
sb.appendLine(" \"bairro\": \"ABC DOS COLIBRIS\",", b_crlf) catch {};
sb.appendLine(" \"cidade\": \"TEST\",", b_crlf) catch {};
sb.appendLine(" \"inscricao_estadual\": \"222.102.222.116\",", b_crlf) catch {};
sb.appendLine(" \"observacao\": \"\",", b_crlf) catch {};
sb.appendLine(" \"id\": 2209595,", b_crlf) catch {};
sb.appendLine(" \"ultima_alteracao\": \"2016-12-26 16:22:34\",", b_crlf) catch {};
sb.appendLine(" \"cep\": \"13555000\",", b_crlf) catch {};
sb.appendLine(" \"suframa\": \"\",", b_crlf) catch {};
sb.appendLine(" \"estado\": \"SP\",", b_crlf) catch {};
sb.appendLine(" \"emails\": [", b_crlf) catch {};
sb.appendLine(" {", b_crlf) catch {};
sb.appendLine(" \"email\": \"somebody@terra.com.br\",", b_crlf) catch {};
sb.appendLine(" \"tipo\": \"T\",", b_crlf) catch {};
sb.appendLine(" \"id\": 1065557", b_crlf) catch {};
sb.appendLine(" }", b_crlf) catch {};
sb.appendLine(" ],", b_crlf) catch {};
sb.appendLine(" \"excluido\": false", b_crlf) catch {};
sb.appendLine(" },", b_crlf) catch {};
sb.appendLine(" {", b_crlf) catch {};
sb.appendLine(" \"telefones\": [", b_crlf) catch {};
sb.appendLine(" ],", b_crlf) catch {};
sb.appendLine(" \"cnpj\": \"12496555500180\",", b_crlf) catch {};
sb.appendLine(" \"rua\": \"AV ROLF WIEST, 100\",", b_crlf) catch {};
sb.appendLine(" \"complemento\": \"ANDAR 7 SALA 612 A 620\",", b_crlf) catch {};
sb.appendLine(" \"contatos\": [", b_crlf) catch {};
sb.appendLine(" ],", b_crlf) catch {};
sb.appendLine(" \"tipo\": \"J\",", b_crlf) catch {};
sb.appendLine(" \"razao_social\": \"SIMPLE SOFTWARE LTDA\",", b_crlf) catch {};
sb.appendLine(" \"nome_fantasia\": \"\",", b_crlf) catch {};
sb.appendLine(" \"bairro\": \"DOM ZETIRO\",", b_crlf) catch {};
sb.appendLine(" \"cidade\": \"APARTVILLE\",", b_crlf) catch {};
sb.appendLine(" \"inscricao_estadual\": \"\",", b_crlf) catch {};
sb.appendLine(" \"observacao\": \"\",", b_crlf) catch {};
sb.appendLine(" \"id\": 2255594,", b_crlf) catch {};
sb.appendLine(" \"ultima_alteracao\": \"2016-12-26 16:28:31\",", b_crlf) catch {};
sb.appendLine(" \"cep\": \"89255505\",", b_crlf) catch {};
sb.appendLine(" \"suframa\": \"\",", b_crlf) catch {};
sb.appendLine(" \"estado\": \"SC\",", b_crlf) catch {};
sb.appendLine(" \"emails\": [", b_crlf) catch {};
sb.appendLine(" ],", b_crlf) catch {};
sb.appendLine(" \"excluido\": false", b_crlf) catch {};
sb.appendLine(" },", b_crlf) catch {};
sb.appendLine(" {", b_crlf) catch {};
sb.appendLine(" \"telefones\": [", b_crlf) catch {};
sb.appendLine(" {", b_crlf) catch {};
sb.appendLine(" \"numero\": \"1938655556\",", b_crlf) catch {};
sb.appendLine(" \"tipo\": \"T\",", b_crlf) catch {};
sb.appendLine(" \"id\": 2555438", b_crlf) catch {};
sb.appendLine(" }", b_crlf) catch {};
sb.appendLine(" ],", b_crlf) catch {};
sb.appendLine(" \"cnpj\": \"00003555500153\",", b_crlf) catch {};
sb.appendLine(" \"rua\": \"AV ABCDEF PINTO CATAO, 18\",", b_crlf) catch {};
sb.appendLine(" \"complemento\": \"\",", b_crlf) catch {};
sb.appendLine(" \"contatos\": [", b_crlf) catch {};
sb.appendLine(" {", b_crlf) catch {};
sb.appendLine(" \"telefones\": [", b_crlf) catch {};
sb.appendLine(" {", b_crlf) catch {};
sb.appendLine(" \"numero\": \"1999655554\",", b_crlf) catch {};
sb.appendLine(" \"tipo\": \"T\",", b_crlf) catch {};
sb.appendLine(" \"id\": 2555559", b_crlf) catch {};
sb.appendLine(" }", b_crlf) catch {};
sb.appendLine(" ],", b_crlf) catch {};
sb.appendLine(" \"cargo\": \"zzz de compras\",", b_crlf) catch {};
sb.appendLine(" \"nome\": \"Gerard\",", b_crlf) catch {};
sb.appendLine(" \"emails\": [", b_crlf) catch {};
sb.appendLine(" {", b_crlf) catch {};
sb.appendLine(" \"email\": \"gerard@terra.com.br\",", b_crlf) catch {};
sb.appendLine(" \"tipo\": \"T\",", b_crlf) catch {};
sb.appendLine(" \"id\": 1065559", b_crlf) catch {};
sb.appendLine(" }", b_crlf) catch {};
sb.appendLine(" ],", b_crlf) catch {};
sb.appendLine(" \"id\": 844485,", b_crlf) catch {};
sb.appendLine(" \"excluido\": false", b_crlf) catch {};
sb.appendLine(" }", b_crlf) catch {};
sb.appendLine(" ],", b_crlf) catch {};
sb.appendLine(" \"tipo\": \"J\",", b_crlf) catch {};
sb.appendLine(" \"razao_social\": \"TIDY TECNOLOGIA LTDA - EPP\",", b_crlf) catch {};
sb.appendLine(" \"nome_fantasia\": \"TIDY\",", b_crlf) catch {};
sb.appendLine(" \"bairro\": \"TUNA\",", b_crlf) catch {};
sb.appendLine(" \"cidade\": \"JAGUAR\",", b_crlf) catch {};
sb.appendLine(" \"inscricao_estadual\": \"395.222.441.222\",", b_crlf) catch {};
sb.appendLine(" \"observacao\": \"ligar sempre depois das 14hs\",", b_crlf) catch {};
sb.appendLine(" \"id\": 2255597,", b_crlf) catch {};
sb.appendLine(" \"ultima_alteracao\": \"2016-12-28 07:31:52\",", b_crlf) catch {};
sb.appendLine(" \"cep\": \"13555500\",", b_crlf) catch {};
sb.appendLine(" \"suframa\": \"\",", b_crlf) catch {};
sb.appendLine(" \"estado\": \"SP\",", b_crlf) catch {};
sb.appendLine(" \"emails\": [", b_crlf) catch {};
sb.appendLine(" {", b_crlf) catch {};
sb.appendLine(" \"email\": \"xi@tidy.com.br\",", b_crlf) catch {};
sb.appendLine(" \"tipo\": \"T\",", b_crlf) catch {};
sb.appendLine(" \"id\": 10655558", b_crlf) catch {};
sb.appendLine(" }", b_crlf) catch {};
sb.appendLine(" ],", b_crlf) catch {};
sb.appendLine(" \"excluido\": false", b_crlf) catch {};
sb.appendLine(" }", b_crlf) catch {};
sb.appendLine("]", b_crlf) catch {};
// Load the JSON array into a JsonArray:
const json_array = try chilkat.JsonArray.init();
defer json_array.deinit();
json_array.loadSb(sb) catch {
std.debug.print("{s}\n", .{try json_array.getLastErrorText(alloc)});
return;
};
// Get some information from each record in the array.
const num_records = json_array.getSize();
var i: i32 = 0;
while (i < num_records) {
std.debug.print("------ Record {d} -------\n", .{i});
const json_record = try json_array.objectAt(i);
defer json_record.deinit();
// Examine information for this record
var num_telefones: i32 = json_record.sizeOfArray("telefones");
std.debug.print("Number of telefones: {d}\n", .{num_telefones});
var j: i32 = 0;
while (j < num_telefones) {
json_record.setJ(j);
std.debug.print(" telefones numero: {s}\n", .{try json_record.stringOf(alloc, "telefones[j].numero")});
std.debug.print(" telefones tipo: {s}\n", .{try json_record.stringOf(alloc, "telefones[j].tipo")});
std.debug.print(" telefones id: {s}\n", .{try json_record.stringOf(alloc, "telefones[j].id")});
j = j + 1;
}
std.debug.print("cnpj: {s}\n", .{try json_record.stringOf(alloc, "cnpj")});
std.debug.print("rua: {s}\n", .{try json_record.stringOf(alloc, "rua")});
// ...
const num_contatos = json_record.sizeOfArray("contatos");
std.debug.print("Number of contatos: {d}\n", .{num_contatos});
j = 0;
while (j < num_contatos) {
json_record.setJ(j);
num_telefones = json_record.sizeOfArray("contatos[j].telefones");
std.debug.print(" Number of telefones: {d}\n", .{num_telefones});
var k: i32 = 0;
while (k < num_telefones) {
json_record.setK(k);
std.debug.print(" telefones numero: {s}\n", .{try json_record.stringOf(alloc, "contatos[j].telefones[k].numero")});
std.debug.print(" telefones tipo: {s}\n", .{try json_record.stringOf(alloc, "contatos[j].telefones[k].tipo")});
std.debug.print(" telefones id: {s}\n", .{try json_record.stringOf(alloc, "contatos[j].telefones[k].id")});
k = k + 1;
}
std.debug.print(" cargo: {s}\n", .{try json_record.stringOf(alloc, "contatos[j].cargo")});
const num_emails = json_record.sizeOfArray("contatos[j].emails");
std.debug.print(" Number of emails: {d}\n", .{num_emails});
k = 0;
while (k < num_emails) {
json_record.setK(k);
std.debug.print(" emails email: {s}\n", .{try json_record.stringOf(alloc, "contatos[j].emails[k].email")});
std.debug.print(" emails tipo: {s}\n", .{try json_record.stringOf(alloc, "contatos[j].emails[k].tipo")});
std.debug.print(" emails id: {s}\n", .{try json_record.stringOf(alloc, "contatos[j].emails[k].id")});
k = k + 1;
}
j = j + 1;
}
i = i + 1;
}
// The output for the above code is:
// ------ Record 0 -------
// Number of telefones: 1
// telefones numero: 19995555555
// telefones tipo: T
// telefones id: 2541437
// cnpj: 11395551000164
// rua: R XAVIER AUGUSTO ROGGE, 22
// Number of contatos: 0
// ------ Record 1 -------
// Number of telefones: 0
// cnpj: 12496555500180
// rua: AV ROLF WIEST, 100
// Number of contatos: 0
// ------ Record 2 -------
// Number of telefones: 1
// telefones numero: 1938655556
// telefones tipo: T
// telefones id: 2555438
// cnpj: 00003555500153
// rua: AV ABCDEF PINTO CATAO, 18
// Number of contatos: 1
// Number of telefones: 1
// telefones numero: 1999655554
// telefones tipo: T
// telefones id: 2555559
// cargo: zzz de compras
// Number of emails: 1
// emails email: gerard@terra.com.br
// emails tipo: T
// emails id: 1065559
//
}