postman-newman

Merging env JSON files for newman run


In my project, we are maintaining multiple JSON files for each post request of collection.Request wise, we need to maintain like this for keeping environment variables. We will do run via new man.

My requirement is to want to merge all environment json files and make it as single file. Then,get it executed with source(collection file)

Is any way to merge files via any cmd comment and any method


Solution

  • This demo.js can merge multiple environment variable files

    The merged JSON file ('demo.postman_environment.json') copy first JSON's attributes (id, name, _postman_variable_scope, _postman_exported_at and _postman_exported_using)

    from the 2nd to the nth JSON just copy Key/value pair into merged JSON file.

    enter image description here

    const fs = require('fs');
    
    // Function to merge multiple JSON objects
    function mergeJSONObjects(jsonObjects) {
        const baseJSON = jsonObjects[0]; // Use the first JSON as the base structure
    
        // Loop over remaining JSON objects and merge their 'values' arrays into the base JSON
        jsonObjects.slice(1).forEach(json => {
            baseJSON.values = [...baseJSON.values, ...json.values];
        });
    
        return baseJSON;
    }
    
    // Read JSON data from files specified in command-line arguments
    const jsonFiles = process.argv.slice(2); // Skip the first two args (node and script name)
    const jsonObjects = jsonFiles.map(file => JSON.parse(fs.readFileSync(file, 'utf8')));
    
    // Perform the merge
    const mergedJSON = mergeJSONObjects(jsonObjects);
    
    // Output the merged JSON
    console.log(JSON.stringify(mergedJSON, null, 4));
    
    // Optionally, save the merged JSON to a file
    fs.writeFileSync('demo.postman_environment.json', JSON.stringify(mergedJSON, null, 4), 'utf8');
    

    Run it with file names argurement

    node demo.js first.json second.json third.json
    

    Result

    demo.postman_environment.json
    

    example

    demo1.postman_environment.json

    {
        "id": "efdf42ef-3df9-45cb-a6a7-b8f8a40b2712",
        "name": "demo1",
        "values": [
            {
                "key": "Animal1",
                "value": "Lion",
                "type": "default",
                "enabled": true
            }
        ],
        "_postman_variable_scope": "environment",
        "_postman_exported_at": "2024-04-27T16:26:09.645Z",
        "_postman_exported_using": "Postman/10.24.24"
    }
    

    demo2.postman_environment.json

    {
        "id": "d85b8da5-2a00-4daa-beba-ba00f55b46b1",
        "name": "demo2",
        "values": [
            {
                "key": "Animal2",
                "value": "Elephant",
                "type": "default",
                "enabled": true
            }
        ],
        "_postman_variable_scope": "environment",
        "_postman_exported_at": "2024-04-27T16:26:29.585Z",
        "_postman_exported_using": "Postman/10.24.24"
    }
    

    demo3.postman_environment.json

    {
        "id": "59fe0bfa-5071-4108-987e-e30a0345af69",
        "name": "demo3",
        "values": [
            {
                "key": "Animal3",
                "value": "Tiger",
                "type": "default",
                "enabled": true
            }
        ],
        "_postman_variable_scope": "environment",
        "_postman_exported_at": "2024-04-27T16:26:58.829Z",
        "_postman_exported_using": "Postman/10.24.24"
    }
    

    Result

    {
        "id": "efdf42ef-3df9-45cb-a6a7-b8f8a40b2712",
        "name": "demo1",
        "values": [
            {
                "key": "Animal1",
                "value": "Lion",
                "type": "default",
                "enabled": true
            },
            {
                "key": "Animal2",
                "value": "Elephant",
                "type": "default",
                "enabled": true
            },
            {
                "key": "Animal3",
                "value": "Tiger",
                "type": "default",
                "enabled": true
            }
        ],
        "_postman_variable_scope": "environment",
        "_postman_exported_at": "2024-04-27T16:26:09.645Z",
        "_postman_exported_using": "Postman/10.24.24"
    }
    

    Test merged JSON with collection

    enter image description here

    1-demo.postman_collection.json

    {
        "info": {
            "_postman_id": "7fab6ad2-98f1-429e-9943-fac7f5b9e491",
            "name": "1-demo",
            "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
            "_exporter_id": "1826150"
        },
        "item": [
            {
                "name": "Get Tiger",
                "event": [
                    {
                        "listen": "prerequest",
                        "script": {
                            "exec": [
                                ""
                            ],
                            "type": "text/javascript",
                            "packages": {}
                        }
                    },
                    {
                        "listen": "test",
                        "script": {
                            "exec": [
                                "const jsonData = JSON.parse(responseBody);\r",
                                "console.log(jsonData[0].name);\r",
                                "pm.test('Animal Name = ' + jsonData[0].name, function () {\r",
                                "    pm.expect(jsonData[0].name).to.eql(\"Lion\");\r",
                                "});\r",
                                ""
                            ],
                            "type": "text/javascript",
                            "packages": {}
                        }
                    }
                ],
                "request": {
                    "auth": {
                        "type": "noauth"
                    },
                    "method": "GET",
                    "header": [],
                    "url": {
                        "raw": "https://freetestapi.com/api/v1/animals?search={{Animal1}}",
                        "protocol": "https",
                        "host": [
                            "freetestapi",
                            "com"
                        ],
                        "path": [
                            "api",
                            "v1",
                            "animals"
                        ],
                        "query": [
                            {
                                "key": "search",
                                "value": "{{Animal1}}"
                            }
                        ]
                    }
                },
                "response": []
            },
            {
                "name": "Get Elephant",
                "event": [
                    {
                        "listen": "prerequest",
                        "script": {
                            "exec": [
                                ""
                            ],
                            "type": "text/javascript",
                            "packages": {}
                        }
                    },
                    {
                        "listen": "test",
                        "script": {
                            "exec": [
                                "const jsonData = JSON.parse(responseBody);\r",
                                "console.log(jsonData[0].name);\r",
                                "pm.test('Animal Name = ' + jsonData[0].name, function () {\r",
                                "    pm.expect(jsonData[0].name).to.eql(\"Elephant\");\r",
                                "});\r",
                                ""
                            ],
                            "type": "text/javascript",
                            "packages": {}
                        }
                    }
                ],
                "request": {
                    "auth": {
                        "type": "noauth"
                    },
                    "method": "GET",
                    "header": [],
                    "url": {
                        "raw": "https://freetestapi.com/api/v1/animals?search={{Animal2}}",
                        "protocol": "https",
                        "host": [
                            "freetestapi",
                            "com"
                        ],
                        "path": [
                            "api",
                            "v1",
                            "animals"
                        ],
                        "query": [
                            {
                                "key": "search",
                                "value": "{{Animal2}}"
                            }
                        ]
                    }
                },
                "response": []
            },
            {
                "name": "Get Tiger",
                "event": [
                    {
                        "listen": "prerequest",
                        "script": {
                            "exec": [
                                ""
                            ],
                            "type": "text/javascript",
                            "packages": {}
                        }
                    },
                    {
                        "listen": "test",
                        "script": {
                            "exec": [
                                "const jsonData = JSON.parse(responseBody);\r",
                                "console.log(jsonData[0].name);\r",
                                "pm.test('Animal Name = ' + jsonData[0].name, function () {\r",
                                "    pm.expect(jsonData[0].name).to.eql(\"Tiger\");\r",
                                "});\r",
                                ""
                            ],
                            "type": "text/javascript",
                            "packages": {}
                        }
                    }
                ],
                "request": {
                    "auth": {
                        "type": "noauth"
                    },
                    "method": "GET",
                    "header": [],
                    "url": {
                        "raw": "https://freetestapi.com/api/v1/animals?search={{Animal3}}",
                        "protocol": "https",
                        "host": [
                            "freetestapi",
                            "com"
                        ],
                        "path": [
                            "api",
                            "v1",
                            "animals"
                        ],
                        "query": [
                            {
                                "key": "search",
                                "value": "{{Animal3}}"
                            }
                        ]
                    }
                },
                "response": []
            }
        ],
        "event": [
            {
                "listen": "prerequest",
                "script": {
                    "type": "text/javascript",
                    "exec": [
                        ""
                    ]
                }
            },
            {
                "listen": "test",
                "script": {
                    "type": "text/javascript",
                    "exec": [
                        ""
                    ]
                }
            }
        ],
        "variable": [
            {
                "key": "priductIds",
                "value": "[\"1001\", \"1002\", \"1003\", \"1004\", \"3002\"]",
                "type": "string"
            },
            {
                "key": "priductId",
                "value": "1001",
                "type": "string"
            },
            {
                "key": "baseUrl",
                "value": "https://valentinos-coffee.herokuapp.com",
                "type": "string"
            },
            {
                "key": "AnimalItemLength",
                "value": ""
            }
        ]
    }
    

    newman run

    newman run 1-demo.postman_collection.json -e demo.postman_environment.json
    

    enter image description here