Home > OS >  Replace entire JSON file key values with empty strings
Replace entire JSON file key values with empty strings

Time:11-11

I have a list of flat JSON keys and values. I am trying to replace all the key values with empty strings

Here is my JSON.

{
    "wl.label.accountPin": "Account PIN",
    "wl.label.logon": "Logon details",
    "wl.label.logonDetails": "Logon details",
    "wl.label.password": "Logon details",

    "Sunday": "Sunday",
    "Monday": "Monday",
    "Tuesday": "Tuesday",
    "Wednesday": "Wednesday",
    "Thursday": "Thursday",
    "Friday": "Friday",
    "Saturday": "Saturday",

    "Su": "Su",
    "Mo": "Mo",
    "Tu": "Tu",
    "We": "We",
    "Th": "Th",
    "Fr": "Fr",
    "Sa": "Sa",

    "January": "January",
    "February": "February",
    "March": "March",
    "April": "April",
    "May": "May",
    "June": "June",
    "July": "July",
    "August": "August",
    "September": "September",
    "October": "October",
    "November": "November",
    "December": "December",

    "Jan": "Jan",
    "Feb": "Feb",
    "Mar": "Mar",
    "Apr": "Apr",
    "May": "May",
    "Jun": "Jun",
    "Jul": "Jul",
    "Aug": "Aug",
    "Sep": "Sep",
    "Oct": "Oct",
    "Nov": "Nov",
    "Dec": "Dec"
}

So I want to keep the key names but replace all the key values with empty strings

eg "wl.label.accountPin": "Account PIN", should be "wl.label.accountPin": "",

What would be the best way to do this?

CodePudding user response:

You json is not json but an Object. Furthermore, it's a one liner. Here's a a minimal reproducable example.

const obj = {
    "wl.label.accountPin": "Account PIN",
    "Sunday": "Sunday",
    "Su": "Su",
    "January": "January",
    "Jan": "Jan",
};

console.log(Object.entries(obj).reduce( (acc, [key,]) => ({...acc, [key]: ""}), {}));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Simple solution

json = {YOUR_DATA};
Object.keys(json).forEach(function(key) {
    json[key] = "";
})

See JSFiddle here http://jsfiddle.net/1xo6pf8s/

CodePudding user response:

Here you go, enjoy and have a good day :)

 var d = {
         'firstKey':'firstValue',
         'secondKey':'secondValue'
        }

for(i in d){
d[i] = ''
}
console.log(d)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related