Node generating Json

Asked

Viewed 296 times

1

I have to develop a Java application that through a Json communicates with Node and changes the site language.

The problem is: the Node has to access the site’s literals/Labels at .txt and turn it into .json.

Using JSON.parser I was able to create an object and print the value key I specified, but I can’t make it one .json. Any help? Follow Node code:

 var sitecadastrar = '{"cadastrartitulo":"Registrierien Company",
"cadastrarnom edaempresa":"Unterhlnem Name",
"cadastrarnomedosegmento":"Segment", "cadastrarbt
nCadastrar":"Registrierien", "cadastrarop0":"Wahl ihre segment...",
"cadastrarop 1":"Raumfahrt und Verteidigung", 
"cadastrarop2":"Landwirtschaft", "cadastrarop3" :"Essen",
"cadastrarop4":"Autos", "cadastrarop5":"Grün Wirtschaft", 
"cadastrarop6":"Maschinen und Anlagen", "cadastrarop7":"Immobilienmarkt",
"cadastrarop8":"Fo rschung und Entwicklung", "cadastrarop9":"Erdöl und
Erdgas", "cadastrarop10":"Ge sundheits und Lebenswissenschaften",
"cadastrarop11":"Finanzdienstleistungen", "
cadastrarop12":"Informationstechnologie", "cadastrarop13":"Bildung",
"cadastraro p14":"Leistung", "cadastrarop15":"Verwaltung",
"cadastrarop16":"Bergbau", "cadas trarop17":"Auslagerung",
"cadastrarop18":"Kleidung/Mode", "cadastrarop19":"Ander e",
"cadastrarph":"Unternehmen Name"}';

 undefined

 console.log(sitecadastrar); 

{"cadastrartitulo":"Registrierien Company",
"cadastrarnomedaempresa":"Unterhlnem  Name",
"cadastrarnomedosegmento":"Segment",
"cadastrarbtnCadastrar":"Registrier ien", "cadastrarop0":"Wahl ihre
segment...", "cadastrarop1":"Raumfahrt und Verte idigung",
"cadastrarop2":"Landwirtschaft", "cadastrarop3":"Essen", "cadastrarop4
":"Autos", "cadastrarop5":"Grün Wirtschaft", "cadastrarop6":"Maschinen
und Anlag en", "cadastrarop7":"Immobilienmarkt",
"cadastrarop8":"Forschung und Entwicklung ", "cadastrarop9":"Erdöl und
Erdgas", "cadastrarop10":"Gesundheits und Lebenswis senschaften",
"cadastrarop11":"Finanzdienstleistungen", "cadastrarop12":"Informa
tionstechnologie", "cadastrarop13":"Bildung",
"cadastrarop14":"Leistung", "cadas trarop15":"Verwaltung",
"cadastrarop16":"Bergbau", "cadastrarop17":"Auslagerung" ,
"cadastrarop18":"Kleidung/Mode", "cadastrarop19":"Andere",
"cadastrarph":"Unte rnehmen Name"}

undefined

var obj = JSON.parse(sitecadastrar); 

undefined

console.log(obj); 

{ cadastrartitulo: 'Registrierien Company',   
 cadastrarnomedaempresa:  'Unterhlnem Name',   cadastrarnomedosegmento:
 'Segment',   cadastrarbtnCadastrar: 'Registrierien',   cadastrarop0:
 'Wahl ihre segment...',   cadastrarop1: 'Raumfahrt und Verteidigung', 
 cadastrarop2: 'Landwirtschaft',   cadastrarop3: 'Essen',  
 cadastrarop4: 'Autos',   cadastrarop5: 'Grün Wirtschaft',  
 cadastrarop6: 'Maschinen und Anlagen',   cadastrarop7:
 'Immobilienmarkt',   cadastrarop8: 'Forschung und Entwicklung',  
 cadastrarop9: 'Erdöl und Erdgas',   cadastrarop10: 'Gesundheits und
 Lebenswissenschaften',   cadastrarop11: 'Finanzdienstleistungen',  
 cadastrarop12: 'Informationstechnologie',   cadastrarop13: 'Bildung', 
 cadastrarop14: 'Leistung',   cadastrarop15: 'Verwaltung',  
 cadastrarop16: 'Bergbau',   cadastrarop17: 'Auslagerung',  
 cadastrarop18: 'Kleidung/Mode',   cadastrarop19: 'Andere',  
 cadastrarph: 'Unternehmen Name' } 

undefined

console.log(obj.cadastrarph); 

Unternehmen Name 

undefined
  • Guys, actually, I needed Node to access my.txt file and generate a json with what’s inside it.

  • What are these undefined throughout the question? which of the JSON is the one in the txt file? the txt file is unique or there are several?

2 answers

1

To read a text file and make it an object, in Nodejs, you have to use the fs (filesystem) and the JSON.parse();

var fs = require('fs');
var object;

object = JSON.parse(fs.readFile('ficheiro.txt', function(err,data) { return data; }));

object now contains the json that is inside the file.

0

Install npm on Node, use the Fylesystem (Fs) module with the "npm install" command at the prompt.

https://www.npmjs.com/package/npm/tutorial

After, format to json with the "strinffy" function and use the "writeFileSync" method, as ex below.

var fs = require('fs');

var sitecadastrar ="xxxxxx";

var data = JSON.stringify(sitecadastrar);

fs.writeFileSync('meujason.json',data);

Browser other questions tagged

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