William
William

Reputation: 191

Swap JSON key and value

I have a JSON object that I'm trying to swap the keys with their value. The values are all unique but the keys are not. Because they are not unique, when creating an object the non unique keys are removed.

Is there anyway I can invert the keys and values before creating an object? I was attempting to do this with underscore but this library only allows you to invert object.

<!DOCTYPE html>
<html>
   <body>
   <script src="http://underscorejs.org/underscore-min.js"></script>
      <script>
         var myObj = {
                "503": "07:25",
                 "507": "06:00",
                 "500x": "06:50",
                 "500x": "07:20",
                 "500": "07:35",
                 "503": "07:50",
                 "507": "07:40",
                 "500x": "07:55",
                 "500": "08:30",
                 "500x": "08:00",
                 "500": "10:45",
                 "507": "09:05",
                 "500": "10:45",
                 "507": "09:05",
                 "500": "13:45",
                 "500": "16:45",
                 "500": "20:00",
                 "500": "22:00",
                 "500N\n*Thur/Fri Only": "23:00"
       },

       myObj = _.invert(myObj),
       keys = Object.keys(myObj),
       values = Object.values(myObj),
       i, len = values.length;
       console.log("Total len = " + len) 
       values.sort();

       console.log(myObj);

     for (i = 0; i < len; i++) {
       k = keys[i];
       v = values[i];
       console.log(k + ': ' + v);
     }
      </script>
   </body>
</html>

Upvotes: 1

Views: 999

Answers (2)

Hassan Imam
Hassan Imam

Reputation: 22574

If you could convert your obj to string, then using regex you can get the words between "" and then use for loop to group them together.

NOTE: You need to escape \n inside string.

const myObj = '{"503": "07:25","507": "06:00","500x": "06:50","500x": "07:20","500": "07:35","503": "07:50","507": "07:40","500x": "07:55","500": "08:30","500x": "08:00","500": "10:45","507": "09:05","500": "10:45","507": "09:05","500": "13:45","500": "16:45","500": "20:00","500": "22:00", "500N\\n*Thur/Fri Only" : "23:00"}';
       
var result = myObj.match(/"(.*?)"/gm).reduce((res,word)=>{
  res.push(word.replace(/"/g,''));
  return res;
},[]);

var swapped = {};
for(let i = 0 ; i < result.length - 1; i += 2){
  swapped[result[i + 1]] =result[i]
}

console.log(swapped);

Upvotes: 1

Chandra Kumar
Chandra Kumar

Reputation: 4205

Use this code:

var myObj = {
 "503": "07:25",
 "507": "06:00",
 "500x": "06:50",
 "500x": "07:20",
 "500": "07:35",
 "503": "07:50",
 "507": "07:40",
 "500x": "07:55",
 "500": "08:30",
 "500x": "08:00",
 "500": "10:45",
 "507": "09:05",
 "500": "10:45",
 "507": "09:05",
 "500": "13:45",
 "500": "16:45",
 "500": "20:00",
 "500": "22:00",
 "500N\n*Thur/Fri Only": "23:00"
};

function swap(json){
var ret = {};
for(var key in json){
ret[json[key]] = key;
}
return ret;
}

console.log(myObj);
console.log(swap(myObj));
<!DOCTYPE html>
<html>
   <body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   <script src="http://underscorejs.org/underscore-min.js"></script>
   </body>
</html>

Upvotes: 0

Related Questions