{"info":{"_postman_id":"26c11f51-1c76-491a-817b-482690641073","name":"Move multiple collections into a single collection","description":"<html><head></head><body><p><img src=\"https://cdn2.hubspot.net/hubfs/208250/Blog_Images/postman.jpg\" alt=\"test\"></p>\n<h1 id=\"description\">Description</h1>\n<p>This collection helps in moving other collection under a single collection</p>\n<p>Here we get full target collections using postman API</p>\n<p><strong>THIS WONT AFFECT THE ACTUAL COLLECTION</strong>\nhere we create a new collection json so no negative effects</p>\n<h1 id=\"usage-instruction\">Usage instruction</h1>\n<p><strong>Authorize postman API</strong></p>\n<p>Goto: Postman account settings and create an API Key.</p>\n<p>replace the apikey value with your key</p>\n<p><strong>Replace pre-request information</strong></p>\n<p>Goto <strong>\"GEnerate new colelction\"</strong> request inside the  <strong>Run in collection runner</strong> folder and update the API key, collection UUID list.</p>\n<p><strong>Now execute the Run in collection runner folder</strong></p>\n<p>Run the <strong>Run in collection runner</strong> folder in collection runner</p>\n<p>This creates a new collection and collectionvariable with the required json value</p>\n<p><strong>Execute Download new collection</strong>\nThis implementation creates a new collection in the current workspace <strong>automatically</strong></p>\n<p>IF you want <strong>json</strong> Send the Download new collection json reqeust , and goto response &gt; body &gt;visualize</p>\n<p><strong>Goto visualise</strong></p>\n<p>Goto visualize and click download as json , you have the new json :)</p>\n<p><strong>IMPORTANT</strong></p>\n<p><img src=\"https://cdn.searchenginejournal.com/wp-content/uploads/2017/05/shutterstock_389230123-760x400.jpg\" alt=\"test\"></p>\n<p>Variables are not supported in folder level so , collection variables are aggregated from each collection and added as collection variable for the new collection ,so duplicate variables might be present please review.</p>\n</body></html>","schema":"https://schema.getpostman.com/json/collection/v2.0.0/collection.json","toc":[{"content":"Description","slug":"description"},{"content":"Usage instruction","slug":"usage-instruction"}],"owner":"8296678","collectionId":"26c11f51-1c76-491a-817b-482690641073","publishedId":"TzXtGzRv","public":true,"customColor":{"top-bar":"FFFFFF","right-sidebar":"303030","highlight":"EF5B25"},"publishDate":"2021-05-21T09:49:53.000Z"},"item":[{"name":"Run in collection runner","item":[{"name":"Generate new collection json","event":[{"listen":"test","script":{"id":"e38a27cc-161a-49d9-9fd8-fe621322a162","exec":["\r","pm.test(\"Status code is 200\", function () {\r","    pm.response.to.have.status(200);\r","});\r","\r","let collection = pm.response.json()\r","\r","let collectionitem = collection.collection.item\r","let collectionItemList = pm.variables.get(\"collectionItemList\")\r","let collectionVariables = pm.variables.get(\"collectionVariables\")\r","\r","collection.collection.name = collection.collection.info.name\r","collection.collection.description = collection.collection.info.description\r","collection.collection.variable ? collection.collection.variable.forEach((val) => collectionVariables.push(val)) : null\r","\r","pm.variables.set(\"collectionVariables\", collectionVariables)\r","\r","delete collection.collection.variable\r","\r","collectionItemList.push(collection.collection)\r","\r","pm.variables.set(\"collectionItemList\", collectionItemList)\r","\r","if (!pm.variables.get(\"collectionList\").length) {\r","    let newcollection = { \"collection\": {} }\r","    newcollection.collection.info = collection.collection.info\r","    delete collection.collection.info\r","    delete newcollection.collection.info[\"_postman_id\"]\r","    newcollection.collection.info.name = pm.variables.get(\"newcollectionname\")\r","    newcollection.collection.info.description = \"please provide a new description for this collection\"\r","    newcollection.collection.item = collectionItemList\r","    newcollection.collection.variable = collectionVariables\r","\r","    pm.collectionVariables.set(\"newcollection\", JSON.stringify(newcollection))\r","\r","    console.log(JSON.parse(pm.collectionVariables.get(\"newcollection\")))\r","}\r","\r","\r",""],"type":"text/javascript"}},{"listen":"prerequest","script":{"id":"916de580-6b1c-431c-aaab-a7da370a9058","exec":["//Replace with your API key\r","pm.variables.set(\"apikey\", \"PMAK-6085e62cd98b88003c821ef0-73725e0b4228223572b9350cb2a8d443a9\")\r","\r","//Give a name for the new collection\r","pm.variables.set(\"newcollectionname\", \"Some name\")\r","\r","//Please specify the work space in which you want to create this collection\r","\r","// Specify \"NA:Don't create\" if you don't want to create collection automatically , else replace it with the workpace name you wnat the collection to be created in\r","\r","// if multiple workspaces with same name , then the collections will be created in all those workspaces\r","pm.variables.set(\"workspace\", \"My Workspace\")\r","\r","//Please provide the UUIDs of the collections you want to move to newcollection\r","// you can get the collection uid by clicking the collection and clicking the information icon on the right side of the screen (the icon after code icon <>)\r","\r","let newcollectionList = [\"14272639-c806a959-89a9-4363-a0e5-93d2f150cf39\",\"14272639-1d304323-6613-49cc-8482-9fc732ac706d\",\"14272639-adc30219-168c-4b92-b593-2aebef712a16\",\"14272639-e010f8a0-b5ab-4ea5-b8b7-66593a7229ed\",\"14272639-d1e1a8ad-6bab-4c0b-bc4a-ab650291b84d\",\"14272639-e3c13808-7e1b-431e-a8cd-5751112c9fb6\",\"14272639-a13b5b35-c3c8-4dcb-b479-b0371bf8711c\",\"14272639-f7a8ef82-b697-4767-b27a-ff66d9679548\",\"14272639-9560fd58-cbb1-4383-95c9-900bdb4765d9\",\"14272639-6b1db929-d020-41f6-ab4d-8ed9fe72d853\",\"14272639-0681b447-a882-4abe-8685-929ab55aad5d\"]\r","\r","\r","\r","//#####DONT MODIFY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r","pm.variables.get(\"collectionList\") ? null : pm.collectionVariables.unset(\"newcollection\")\r","\r","pm.variables.get(\"collectionList\") ? null : pm.variables.set(\"collectionVariables\",[])\r","\r","pm.variables.get(\"collectionList\") ? null : pm.variables.set(\"collectionList\", newcollectionList.reverse())\r","\r","pm.variables.get(\"collectionItemList\") ? null : pm.variables.set(\"collectionItemList\", [])\r","\r","let collectionList = pm.variables.get(\"collectionList\");\r","let collection_uuid = collectionList.pop()\r","pm.variables.set(\"collection_uuid\", collection_uuid)\r","pm.variables.set(\"collectionList\", collectionList)\r","\r","\r","collectionList.length ? postman.setNextRequest(pm.info.requestName) : null\r",""],"type":"text/javascript"}}],"id":"c5c976d2-d375-4d4d-b339-75be1729fead","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"auth":{"type":"apikey","apikey":{"basicConfig":[{"key":"key","value":"X-Api-Key"},{"key":"value","value":"{{apikey}}"}]},"isInherited":false},"method":"GET","header":[],"body":{"mode":"raw","raw":"","options":{"raw":{"language":"json"}}},"url":"https://api.getpostman.com/collections/{{collection_uuid}}","urlObject":{"protocol":"https","path":["collections","{{collection_uuid}}"],"host":["api","getpostman","com"],"query":[],"variable":[]}},"response":[],"_postman_id":"c5c976d2-d375-4d4d-b339-75be1729fead"},{"name":"Find workspace","event":[{"listen":"test","script":{"id":"fb0c22d8-c50b-4271-be69-48d5678c4236","exec":["if (pm.variables.get(\"workspace\") !== \"NA:Don't create\") {\r","\r","    let workpaceList = []\r","\r","    pm.test(\"Status code is 200\", function () {\r","        pm.response.to.have.status(200);\r","        workpaceList = pm.response.json().workspaces.filter((item) => item.name === pm.variables.get(\"workspace\"))\r","        pm.expect(workpaceList, \"No matching workspace was found\").not.to.be.empty\r","    });\r","\r","    pm.test(\"Body matches string\", function () {\r","        pm.expect(pm.response.text()).to.not.include(\"error\");\r","    });\r","\r","\r","    pm.variables.set(\"workpaceList\", workpaceList)\r","}else{\r","    postman.setNextRequest(null)\r","}"],"type":"text/javascript"}},{"listen":"prerequest","script":{"id":"599e2a85-dee4-4134-969f-e32add029b9c","exec":[""],"type":"text/javascript"}}],"id":"2d9d5de1-52a9-4bbb-b4f6-fe975e229f16","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"auth":{"type":"apikey","apikey":{"basicConfig":[{"key":"key","value":"X-Api-Key"},{"key":"value","value":"{{apikey}}"}]},"isInherited":false},"method":"GET","header":[],"body":{"mode":"raw","raw":"","options":{"raw":{"language":"json"}}},"url":"https://api.getpostman.com/workspaces","description":"<p>Creates the new collection to the specified workspace</p>\n","urlObject":{"protocol":"https","path":["workspaces"],"host":["api","getpostman","com"],"query":[],"variable":[]}},"response":[],"_postman_id":"2d9d5de1-52a9-4bbb-b4f6-fe975e229f16"},{"name":"Create new collection","event":[{"listen":"test","script":{"id":"3e1fc615-f590-4d5d-af4c-7882ca7d3fcf","exec":["pm.test(\"Status code is 200\", function () {\r","    pm.response.to.have.status(200);\r","});\r","\r","pm.test(\"Body matches string\", function () {\r","    pm.expect(pm.response.text()).to.not.include(\"error\");\r","});"],"type":"text/javascript"}},{"listen":"prerequest","script":{"id":"e6de4a1d-f9db-4868-9fc1-93a161ecf820","exec":["postman.setNextRequest(null)\r","let workpaceList = pm.variables.get(\"workpaceList\")\r","console.log(workpaceList)\r","pm.variables.set(\"workspaceID\", workpaceList.pop().id)\r","pm.variables.set(\"workpaceList\", workpaceList)\r","pm.variables.set(\"requestBody\", pm.collectionVariables.get('newcollection').replace(/,\\s{0,15}:\\s{0,15}\\\"id\\\"\\s{0,15}:\\s{0,15}\\\"(.*?)\\\",*/img, \",\"))\r","\r","workpaceList.length ? postman.setNextRequest(pm.info.requestName) : postman.setNextRequest(null)"],"type":"text/javascript"}}],"id":"c23d868b-9b10-4b9b-8ac3-2072c369b91c","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"auth":{"type":"apikey","apikey":{"basicConfig":[{"key":"key","value":"X-Api-Key"},{"key":"value","value":"{{apikey}}"}]},"isInherited":false},"method":"POST","header":[],"body":{"mode":"raw","raw":"{{requestBody}}","options":{"raw":{"language":"json"}}},"url":"https://api.getpostman.com/collections?workspace={{workspaceID}}","description":"<p>Creates the new collection to the specified workspace</p>\n","urlObject":{"protocol":"https","path":["collections"],"host":["api","getpostman","com"],"query":[{"key":"workspace","value":"{{workspaceID}}"}],"variable":[]}},"response":[],"_postman_id":"c23d868b-9b10-4b9b-8ac3-2072c369b91c"}],"id":"cdb9bd75-7142-4b46-bd3c-17488140b807","description":"<p>##Description</p>\n<p>Just run this folder in the collection runner</p>\n","_postman_id":"cdb9bd75-7142-4b46-bd3c-17488140b807"},{"name":"Download new collection","event":[{"listen":"test","script":{"id":"51e7c73c-a366-47e2-9396-da1f336d61a9","exec":["//removing all ID so that it will be regenerated automatically when uploaded\r","let collection = JSON.parse(pm.collectionVariables.get('newcollection').replace(/(\\{)\\s{0,15}\"id\\\"\\s{0,15}:\\s{0,15}\\\"(.*?)\\\"\\s{0,15},*/img, \"$1\").replace(/\\,\\s{0,15}\"id\\\"\\s{0,15}:\\s{0,15}\\\"(.*?)\\\"\\s{0,15}(,*)/img, \"$2\"))\r","\r","pm.environment.set('Downloadcollection',\r","    `\r","<!DOCTYPE html>\r","<html>\r","<head>\r","<style>\r","\r",".align-right {\r"," \r","  background-color: #4CAF50;\r","  color: white;\r","  position:relative;\r","  text-align:center;\r","\r","  }\r","\r","</style>    \r","</head>\r","<body>\r","<script>\r"," \r","\r","function download(content, fileName, contentType) {\r"," const a = document.createElement('a');\r"," const file = new Blob([content], { type: contentType });\r"," a.href = URL.createObjectURL(file);\r"," a.download = fileName;\r"," a.click();\r","}\r","function onDownload(){\r"," download(decodeURIComponent(\\`${encodeURIComponent(JSON.stringify(collection))}\\`) ,'{{collectionName}}.postman_collection.json','text/plain');\r","}\r","</script>\r","<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'></script>\r","\r","<button class='table-btn align-right' data-table='list' onclick='onDownload()'>Download collection as JSON</button>\r","\r","</body>\r","</html>\r","`\r",")\r","\r","let template = pm.environment.get('Downloadcollection')\r","pm.visualizer.set(template, {\r","    // Pass the response body parsed as JSON as `data`\r","    collectionName: collection.collection.info.name\r","\r","})\r","\r","\r",""],"type":"text/javascript"}}],"id":"cba93403-27dd-4eeb-aea4-629be0d0ee54","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"auth":{"type":"apikey","apikey":{"basicConfig":[{"key":"key","value":"X-Api-Key"},{"key":"value","value":"{{apikey}}"}]},"isInherited":false},"method":"POST","header":[],"body":{"mode":"raw","raw":"","options":{"raw":{"language":"json"}}},"url":"postman-echo.com/post","urlObject":{"path":["post"],"host":["postman-echo","com"],"query":[],"variable":[]}},"response":[],"_postman_id":"cba93403-27dd-4eeb-aea4-629be0d0ee54"}],"variable":[{"key":"newcollection","value":""}]}