How to swap key and value of JSON element using JavaScript ? Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report In this article, we will swap the key and value of JSON elements using JavaScript. Given a JSON object and the task is to swap the JSON object key with values and vice-versa with the help of JavaScript. Below are the following approaches: Using for loopUsing Object.keys() and ForEach() MethodUsing Object.entries() and map() MethodApproach 1: Using for loopCreate a new empty object.Visit every key of an object by for loop and add the elements from the old object to the new object in reverse form(by swapping the key and values).Example: This example implements the above approach. JavaScript let obj = { "Prop_1": "Val_1", "Prop_2": "Val_2", "Prop_3": "Val_3" }; function swapValues(j) { let res = {}; for (let key in j) { res[j[key]] = key; } return res; } function GFG_Fun() { console.log(JSON.stringify(swapValues(obj))); } GFG_Fun(); Output{"Val_1":"Prop_1","Val_2":"Prop_2","Val_3":"Prop_3"}Approach 2: Using Object.keys() and ForEach() MethodCreate a new empty object.For each key of the object, add the elements from the old object to the new object in reverse form (by swapping the key and values). Example: This example implements the above approach. JavaScript let obj = { "Prop_1": "Val_1", "Prop_2": "Val_2", "Prop_3": "Val_3" }; function swapValues(o) { const res = {}; Object.keys(o).forEach(key => { res[o[key]] = key; }); return res; } function GFG_Fun() { console.log(JSON.stringify(swapValues(obj))); } GFG_Fun(); Output{"Val_1":"Prop_1","Val_2":"Prop_2","Val_3":"Prop_3"}Approach 3: Using Object.entries() and map() MethodIn this method, we will use Object.entries() and map() Methods to get all the key values of the object in the array format. Example: JavaScript let obj = { "Prop_1": "Val_1", "Prop_2": "Val_2", "Prop_3": "Val_3" }; const swappedObject = Object.entries(obj).reduce((acc, [key, value]) => { acc[value] = key; return acc; }, {}); console.log(swappedObject); Output{ Val_1: 'Prop_1', Val_2: 'Prop_2', Val_3: 'Prop_3' } Create Quiz Comment P PranchalKatiyar Follow 1 Improve P PranchalKatiyar Follow 1 Improve Article Tags : JavaScript Web Technologies javascript-object JSON JavaScript-Questions +1 More Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like