0
I have two json files, one with id, status and acronym and the other with the state id and the cities of that state. I read these files and recorded the content in arrays, but I need to create a file for each state with their respective cities, relating the ids. So far I was able to create the files separated by state in the first FOR, but I’m in doubt about how to put each city in their content, I was only able to create the files and the initial structure of JSON:
Example file that has to be created:
ES.json
[{
"ID": "1",
"Nome": "Afonso Cláudio",
},
{
"ID": "2",
"Nome": "Água Doce do Norte",
},
{
"ID": "3",
"Nome": "Águia Branca",
},
]
All these cities are part of ES, so I need a file for each state and its respective cities.
var fs = require("fs");
let dataStates = fs.readFileSync("Estados.json", "utf8");
let states = JSON.parse(dataStates);
let sigla = states.map((state) => {
const { ID, Sigla, Nome } = state;
return {
idstate: ID,
sigla: Sigla,
estadoNome: Nome,
};
});
//console.log(sigla);
let dataCities = fs.readFileSync("Cidades.json", "utf8");
let cities = JSON.parse(dataCities);
let nome = cities.map((city) => {
const { ID, Nome, Estado } = city;
return {
idcity: ID,
cidadeNome: Nome,
cidadeEstado: Estado,
};
});
//console.log(nome);
const initialJson = {
nextId: 1,
cidades: [],
};
for(i = 0; i < sigla.length; i++){
fs.writeFileSync(sigla[i].sigla + '.json', JSON.stringify(initialJson));
for(l = 0; l < nome.length; l++){
cidades = { id: nextId++, name: nome[l].cidadeNome };
cidades.push(cidades);
fs.writeFileSync(sigla[i].sigla + '.json', JSON.stringify(cidades));
}
}
An example of the two files was missing. The way it is, we have to interpret its code to try to reverse engineer it and figure out what’s in them. It seems too much work, no?
– Sorack
I pasted some images to facilitate understanding
– Rafael Costa
You can’t copy and paste from one
json
in an image. Learn more about this in: How NOT to Ask Questions Manual - Post Code as Image.– Sorack
I put the link to the files
– Rafael Costa
Also give an example of one of the status files that should be generated. You don’t have to have every city, just an example with a couple of cities is already good size.
– Sorack
You can give an example of the output result?
– Sergio
I put an example there, basically I need to create a file for each state, with the acronym as name and their respective cities as content, I don’t know if the example was good but something better.
– Rafael Costa