Home > Software design >  How can I dynamically return a list of names of specific parameters from .JSON file?
How can I dynamically return a list of names of specific parameters from .JSON file?

Time:03-26

So I have .json file that looks like this:

[{"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"
        },
        "kor": {
            "official": "\uc544\ub8e8\ubc14",
            "common": "\uc544\ub8e8\ubc14"
        },
        "nld": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "per": {
            "official": "\u0622\u0631\u0648\u0628\u0627",
            "common": "\u0622\u0631\u0648\u0628\u0627"
        },
        "pol": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "por": {
            "official": "Aruba",
            "common": "Aruba"
        },
        "rus": {
            "official": "\u0410\u0440\u0443\u0431\u0430",
            "common": "\u0410\u0440\u0443\u0431\u0430"
        }          
    }]

There are actually many translations like this, this is just one of them for clarification. And I want to dynamically return the names of the translation keys like "rus" and put them into an array. ["rus", "deu", "est","fra"] I do not need to return the context of these translation keys, I only need to store their names and put these names into an array. Because it should be done dynamically, I believe the code should also return newly added translation keys from .JSON file. I have tried the following code:

const keys = [data.map((point=>point.translations), function(v, k){ return k } )];

but it didn't work. Can anyone help me putting them into an array?

CodePudding user response:

You can get the keys of an object as array via the Object.keys() method.

Give this a try:

const keys = Object.keys(data.translations)

CodePudding user response:

The very first thing that I observed is that the data present in the JSON file must be in {}. Then try out this code

const jsonData= require('./temp.json'); //The path of your JSON file
const JavaScriptobject = JSON.parse(JSON.stringify(jsonData));//converts JSON to JS object
let obj = JavaScriptobject.translations; //access the required object
const arr = Object.keys(obj);//since you need names, those are keys
console.log(arr);

CodePudding user response:

try this

var lang = [];
json.forEach((element) => {
  Object.keys(element.translations).forEach((item) => {
    lang.push(item);
  });
});

result

lang=["ces","deu","est","fin","fra","hrv","hun","ita","jpn","kor","nld","per","pol","por","rus"]

CodePudding user response:

as you have updated the question I'll create a separate answer for this variant.

To convert this JSON file to a list like: ['ces', 'deu', 'est', 'fin', 'fra', 'hrv', 'hun', 'ita', 'jpn', 'kor', 'nld', 'per', 'pol', 'por', 'rus'], you can use the following code:

const keys = []
translations.map(def => {
    keys.push(...Object.keys(def.translations))
})

Be aware: In case that your translations array contains multiple translation definitions, e.g. like this:

[
  {"translations": {...},
  {"translations": {...}
]

This approach can lead to duplicates in the output, which you might have to filter out or prevent by changing the conversion code by a bit.

If - for multiple translation definitions - you desire an output like this:

[
  ['ces', 'deu', 'est', 'fin', 'fra', 'hrv', 'hun', 'ita', 'jpn', 'kor', 'nld', 'per', 'pol', 'por', 'rus'],
  [...]
]

Then you can modify the conversion code to this:

translations.map(def => Object.keys(def.translations))

  • Related