1

I have .json file, but since it is very big, I will paste an example of how it looks like:

  [{"translations": {
        "ces": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "deu": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "est": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "fin": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "fra": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "hrv": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "hun": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "ita": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "jpn": {
            "official": "\u30a2\u30eb\u30d0",
            "common": "\u30a2\u30eb\u30d0"
        }}]

Original .json file contains more than 10 blocks like this. If you are curious, you can check the link: https://raw.githubusercontent.com/TheRadioDept/technical-question/main/countries.json

I also have javascript code that reads values from this .json file using require method.

const enteredKey = process.argv.slice(2);
console.log('Key is : ', enteredKey);

/* Checking if number of parameters more than 0 */
/* Checking if entered translation key is supported by.json file. */
// eslint-disable-next-line max-len
if (enteredKey.length < 2 && enteredKey !== null &&  removeDuplicates(keys).includes(enteredKey[0])) {
try {
console.log(data.map(point => point.translations[0][enteredKey] ?
// eslint-disable-next-line max-len
point.translations[enteredKey].official: `No translation for ${ enteredKey }.`));
} catch (error) {
console.log('Cannot translate variable');
}
} else {
console.log('Incorrect key parameter');
}

When program runs, it takes an CLI parameter, let's say deu for example and returns all parameters(official names`) of this key in .json file. Please note that on my local PC both .json and JS files are stored in the same folder.

What I need is instead of doing one console.log for the whole array of keys is to log(print) them one by one. I have tried using for loop, but since I am unfamiliar with .json structures I don't know how to make it print it individually. This is for loop I tried:

for(i = 0; i < data.translations[enteredKey]; ++i) { 
console.log(data.map(point => point.translations[enteredKey] ?
// eslint-disable-next-line max-len
point.translations[enteredKey].official: `No translation for ${ enteredKey }.`));

}

The output was console.log('Cannot translate variable');.

Could anyone help please?

Farkhad
  • 89
  • 7
  • 1
    @T.J.Crowder Hi! Thank you for feedback. I have edit the question, hopefully it will be more clear now. – Farkhad Mar 29 '22 at 08:49
  • Thanks for editing the question! Looks like this question's answers will help: [*How can I access and process nested objects, arrays or JSON?*](https://stackoverflow.com/questions/11922383/) so this should probably be marked as a duplicate. Something along the lines of: `const result = data.map(({translations}) => translations[enteredKey]?.official).filter(v => v); ` which will give you an array of the `official` value of the object identified by the entered key for each element of the array, unless there isn't one in which case it's left out. Or ... – T.J. Crowder Mar 29 '22 at 09:05
  • ...if you just want to print them out one by one: `for (const {translations} of data) { const value = translations[enteredKey]?.official; if (value) { console.log(value); } }`. (That's using [destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) and [optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining), if there are parts that look unfamiliar.) – T.J. Crowder Mar 29 '22 at 09:06
  • @T.J.Crowder Thank you! Where do I have to write this code `const result = data.map(({translations}) => translations[enteredKey]?.official).filter(v => v);`? I am not quite familiar with it, so I would appreciate if you can show me how to do it. And thank you for resource sharing! – Farkhad Mar 29 '22 at 09:29
  • I'm not quite sure what you mean. You'd include that code after getting `data` and wherever it is in your code that you want that array (if you do want an array). – T.J. Crowder Mar 29 '22 at 09:32
  • 1
    @T.J.Crowder oh yes. I did that and it worked. Thank you so much! – Farkhad Mar 29 '22 at 09:38

1 Answers1

0

If I understand your requirement correctly, You want to check for the enteredKey in the JSON you have and if key is available you want to print the official key value else No translation available text. If Yes, Then you can try this :

const data = [{
    "translations": {
        "ces": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "deu": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "est": {
            "official": "Aruba",
            "common": "Aruba"
        }
    }
}];

const enteredKey = 'deu';

const res = data.map(point => point.translations[enteredKey] ?
point.translations[enteredKey].official : `No translation for ${enteredKey }.`)[0];

console.log(res);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123