0

I have a JSON like this:

{ "id_document" : "12345679", "name" : "John SMmith", "value" : "127,30" }

I would like to change the "value" field to a Double/Number, like this:

{ "id_document" : "12345679", "name" : "John SMmith", "value" : 127.30 }

The problem is that I have a lot of JSON data, eg 70k lines. So I need a way to automate this. Does anyone have any ideias?

Resolution can be made in any language. I was trying NodeJS.

Thanks for the help guys.

EDIT:

My JSON file is something like:

{"DESPESA": [{"sgPartido": "DEM", "numMes": 1, "txtCNPJCPF": "03625917000170", "vlrGlosa": "0", "txNomeParlamentar": "ABEL MESQUITA JR.", "sgUF": "RR", "nuCarteiraParlamentar": 1, "vlrDocumento": "4007,06", "nuDeputadoId": 3074, "numSubCota": 3, "txtDescricaoEspecificacao": "Veículos Automotores", "txtPassageiro": null, "datEmissao": null, "codLegislatura": 55, "vlrRestituicao": "0", "numParcela": 0, "txtDescricao": "COMBUSTÍVEIS E LUBRIFICANTES.", "txtNumero": "4339", "numEspecificacaoSubCota": 1, "txtFornecedor": "B.B. PETROLEO LTDA", "numLote": 1354058, "indTipoDocumento": 0, "idecadastro": 178957, "numAno": 2017, "txtTrecho": null, "numRessarcimento": 5711, "vlrLiquido": "4007,06", "ideDocumento": 6196889, "nuLegislatura": 2015}, ...70k lines with objects like this and the end}

EDIT: I did guys, the result was that:

fs = require('fs');
var in_file = 'in.json';
var out_file = 'out.json' 
fs.readFile(in_file, 'utf8', function(err, data) {
data = JSON.parse(data);

for(let i in data.DESPESA) {
    let despesa = data.DESPESA[i];
    let valor = despesa.vlrDocumento.replace(',', '.');
    data.DESPESA[i].vlrDocumento = parseFloat(valor);
}

fs.writeFile(out_file, JSON.stringify(data), function(err) {
    if (err) throw err;
        console.log('The file has been saved!');
});
});

thanks for que the help, the answers made the difference.

Yago
  • 1
  • 1
  • 3

1 Answers1

0

Assuming your data is an array of objects, you should take a look at Array.proptoy.map then you can write a single function to parse through your objects, and convert your data from string to number, as an example:

const myData = JSON.parse(<data from file>);
const parsedData = mydata.map(function(val) {
   val.value = parseFloat("554,20".replace(",", "."));
   return val;
});

//go back to JSON:
JSON.stringify(parsedData);

(the parseFloat calculation came from this answer

Community
  • 1
  • 1
JoeTortuga
  • 376
  • 1
  • 3
  • 9
  • Something like this? fs = require('fs'); var m ='out.json'; fs.readFile(m, 'utf8', function(err, data) { data = JSON.parse(data); Object.keys(data).forEach(function(vlrDocumento) { vlrDocumento.value = parseFloat(vlrDocumento.replace(",", ".")) JSON.stringify(vlrDocumento.value) }) }); – Yago May 11 '17 at 20:25
  • I'm not sure what Object.keys() is going to do here, if you have single file with one giant JSON object inside of it, I'd wonder what the structure of that is. if it's an array of objects, you wouldn't object.keys, you'd use forEach or map. – JoeTortuga May 11 '17 at 22:24
  • I edited my question with one Object of the JSON. I am trying to change the "vlrDocumento": "4007,06" item to "vlrDocumento": 4007.06 and of all the 70k others objects. Each one with some different value for the same "vlrDocumento" key. – Yago May 12 '17 at 00:25