Home > Software design >  Convert array in JSON file to string
Convert array in JSON file to string

Time:02-14

Here's a snippet of my JSON object -

{
    "traditional": "%",
    "simplified": "%",
    "referencedTraditional": [],
    "referencedSimplified": [],
    "pinyinNumeric": "pa1",
    "pinyinDiacritic": "pā",
    "definitions": [
        "percent (Tw)"
    ],
    "definitionsDiacritic": [
        "percent (Tw)"
    ]
}

I want to convert every key called definitions from an array into a string.

E.g. From ["percent (Tw)"] to "percent (Tw)". Basically, I don't want the array brackets.

So far, I've tried looping through the file and converting every "definitions" key with JSON.stringify() or toString() -

translations.forEach(key => JSON.stringify((key.definitions)))

However, nothing changes in the outputted file.

CodePudding user response:

const obj = {
    traditional: '%',
    simplified: '%',
    referencedTraditional: [],
    referencedSimplified: [],
    pinyinNumeric: 'pa1',
    pinyinDiacritic: 'pā',
    definitions: ['percent (Tw)'],
    definitionsDiacritic: ['percent (Tw)'],
};

const returnStringDefinitions = (obj) => {
    const arr = [];
    for (const [key, value] of Object.entries(obj)) {
        if (key === 'definitions') arr.push(value.toString());
    }
    return arr;
};

console.log(returnStringDefinitions(obj));

CodePudding user response:

I think what may be happening for you is that you aren't saving the resulting array anywhere. The approach could be to create a new object.

const obj = {
    traditional: '%',
    simplified: '%',
    referencedTraditional: [],
    referencedSimplified: [],
    pinyinNumeric: 'pa1',
    pinyinDiacritic: 'pā',
    definitions: ['percent (Tw)'],
    definitionsDiacritic: ['percent (Tw)'],
};

const output = Object.keys(obj).reduce((accumulator, currentKey) => {
    const currentValue = obj[currentKey];
    if (currentKey === 'definitions' && Array.isArray(currentValue)) {
            accumulator[currentKey] = currentValue.join(''); 
    } else {
        accumulator[currentKey] = currentValue;
    }
    return accumulator;
}, {})

console.log(output);

This solution checks if it is an array, and also just joins everything in the array just in case there are multiple things in the array, but how this is handled is really up to you :)

CodePudding user response:

Issue with the code is the one below

translations.forEach(key => JSON.stringify((key.definitions)))

Simply running this statement will not update the value for the key inside that object. You have to update the object for this.

Logic.

  • Loop through the keys in the object translations. I used Object.entries(translations).forEach for that.
  • Check each key, whether it includes "definitions" in that key.
  • If the key has "definitions" in that, run your stringification logic. I prefer Array.join() compared to JSON.stringify.
  • Update the key of that object.

Working Fiddle

const translations = {
    "traditional": "%",
    "simplified": "%",
    "referencedTraditional": [],
    "referencedSimplified": [],
    "pinyinNumeric": "pa1",
    "pinyinDiacritic": "pā",
    "definitions": [
        "percent (Tw)"
    ],
    "definitionsDiacritic": [
        "percent (Tw)"
    ]
}
Object.entries(translations).forEach(([key, value]) => key.includes('definitions') ? translations[key] = translations[key].join("") : null);
console.log(translations);

  • Related