Home > Net >  join multible json files in on json with its path as a key
join multible json files in on json with its path as a key

Time:10-13

I have some json files that I want to join them in one json file, but I need to set everyone under a key of its path so,I can get them very easy when I use the big json file

            const jsonFullPath = path.relative(root, file).split(path.sep).join('.')
                .replace(/(_[a-z]{2}(_[A-Z]{2})?)?\.json/, '')   '.' ;
            const obj = [jsonFullPath];
            let rawdata = fs.readFileSync(file);
            let student = JSON.parse(rawdata);

the code above called in an array of json files

anyone can help please

an example is like this :

{  "sessionTimer": {
"accessibleTimingOut": "{{timeoutMinutes}} minutes until this 
page times out. Move your mouse pointer or press any key to 
extend the time with {{extendMinutes}} minutes.",
"reload": "Reload",
"timedOut": "The page has timed out and needs to be reloaded.",
"timingOut": "seconds until this page times out and needs to be 
 reloaded. Move your mouse pointer or press any key to extend the 
  time with {{extendMinutes}} minutes."
 },
 "header": {
"screenReaderLabel": "Poslechnout",
"myAccountLabel": "Přihlásit se",
"myAccountLogoutLabel": "Odhlásit se",
"myAccountPagesLabel": "Moje stránky",
"newToTheLibrary": "Jste v knihovně noví?",
"joinTheLibraryLabel": "Registrovat do knihovny",
"openingsLabel": "Provozní doba",
"languageLabel": "Jazyk"
 },
 "branchOpeningHours": {
"headerLabel": "Provozní doba",
"closedLabel": "Zavřeno",
"todayLabel": "Dnes",
"navigationBackLabel": "Zpět",
"navigationNextLabel": "Další",
"navigationPreviousLabel": "Předchozí",
"serviceTypeClosedLabel": "Zavřeno",
"serviceTypeStaffedLabel": "S personálem",
"serviceTypeSelfServiceLabel": "Samoobsluha",
"typeRegularLabel": "Pravidelný",
"typeSpecialLabel": "Zvláštní",
"viewAllLabel": "Zobrazit veškerou otevírací dobu"
 }
}

after the join will be some think like this ;

"modules.provider.assets.locales.en_US.translation":{  
"sessionTimer": {
"accessibleTimingOut": "{{timeoutMinutes}} minutes until this 
page times out. Move your mouse pointer or press any key to 
extend the time with {{extendMinutes}} minutes.",
"reload": "Reload",
"timedOut": "The page has timed out and needs to be reloaded.",
"timingOut": "seconds until this page times out and needs to be 
 reloaded. Move your mouse pointer or press any key to extend the 
  time with {{extendMinutes}} minutes."
 },
 "header": {
"screenReaderLabel": "Poslechnout",
"myAccountLabel": "Přihlásit se",
"myAccountLogoutLabel": "Odhlásit se",
"myAccountPagesLabel": "Moje stránky",
"newToTheLibrary": "Jste v knihovně noví?",
"joinTheLibraryLabel": "Registrovat do knihovny",
"openingsLabel": "Provozní doba",
"languageLabel": "Jazyk"
 },
 "branchOpeningHours": {
"headerLabel": "Provozní doba",
"closedLabel": "Zavřeno",
"todayLabel": "Dnes",
"navigationBackLabel": "Zpět",
"navigationNextLabel": "Další",
"navigationPreviousLabel": "Předchozí",
"serviceTypeClosedLabel": "Zavřeno",
"serviceTypeStaffedLabel": "S personálem",
"serviceTypeSelfServiceLabel": "Samoobsluha",
"typeRegularLabel": "Pravidelný",
"typeSpecialLabel": "Zvláštní",
"viewAllLabel": "Zobrazit veškerou otevírací dobu"
 }
}

CodePudding user response:

You mean something like this?

let myJson = '{';
myJson  = `{jsonFullPath}: {rawdata}`;
myJson  = '}';

You can just build your final string.

CodePudding user response:

Assuming you have a list of files to add to your JSON, and your jsonFullPath is created correctly the following should work

let files = [...]; //your json files you want to add
let myobj = files.reduce((obj, file) => {
  //not sure why you are appending an addtional "." at the end ...
  let key = path.relative(root, file).split(path.sep).join('.')
              .replace(/(_[a-z]{2}(_[A-Z]{2})?)?\.json/, '')   '.' ; 
  obj[key] = JSON.parse(fs.readFileSync(file, "utf-8"));
  return obj;
}, {});

Ie, the callback of Array.reduce is executed for each file in the files array. It transforms the filename to your desired key, then reads the file from disk (depending on where this code is running, you probably should use the asynchronous variant of fs.readFile() because the readFileSync will block your execution) and adds the parsed object to your resulting object

If you are more comfortable with loops you can it also do this way

let files = [...];
let myobj = {};

files.forEach(file => {
  let key = path.relative(root, file).split(path.sep).join('.')
            .replace(/(_[a-z]{2}(_[A-Z]{2})?)?\.json/, '')   '.' ; 
  myobj[key] = JSON.parse(fs.readFileSync(file, "utf-8"));
});

or with a simple for .. of loop

let files = [...];
let myobj = {};

for (let file of files) {
  let key = path.relative(root, file).split(path.sep).join('.')
            .replace(/(_[a-z]{2}(_[A-Z]{2})?)?\.json/, '')   '.' ; 
  myobj[key] = JSON.parse(fs.readFileSync(file, "utf-8"));
}
  • Related