Skip to content

Instantly share code, notes, and snippets.

@alexxlagutin
Last active October 2, 2019 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexxlagutin/68f89cae748282f19104f94a04196132 to your computer and use it in GitHub Desktop.
Save alexxlagutin/68f89cae748282f19104f94a04196132 to your computer and use it in GitHub Desktop.
sources to test merge-jsons-webpack-plugin
{
"result": [
{
"title": "exam1",
"data": "somedata"
},
{
"title": "exam2",
"data": "somedata"
},
{
"title": "exam3",
"data": "somedata"
}
]
}
{
"title": "exam1",
"data": "somedata"
}
{
"title": "exam2",
"data": "somedata"
}
{
"title": "exam3",
"data": "somedata"
}
@tettusud
Copy link

Is that fine ,if its because we may need to introduce extra variable for the key "result"
[
{
"title": "exam1"
"data": "somedata"
},
{
"title": "exam2"
"data": "somedata"
},
{
"title": "exam3"
"data": "somedata"
}
]

@alexxlagutin
Copy link
Author

Probably "result" key haven't to be here. I can get it on the next step merging (for example) result1.json and result2.json into a single file using prefixFileName: true and "files"-mode

@tettusud
Copy link

@alexxlagutin,
I have come across this solution(keeping in mind not to break existing results), please check if its fine.

Description:
If the same key happens to appear in more than one file at the same hierarchy, and if {duplicates = true} is passed, then instead of overriding the existing key , it will convert that key's value as an array and push the new values

Sample.

duplicate1.json

{
    "user":{
        "firstName":"Sudharsan",
        "lastName":"Tettu",
        "age":35
    }
}

duplicate2.json

{
    "user":{
        "firstName":"TestFirstName",
        "lastName":"TestLastName",
        "age":135
    }
}

duplicate3.json

{
    "user":{
        "firstName":"TestFirstName1",
        "lastName":"TestLastName1",
        "age":10,
        "address":{
            "street":"Street 33",
            "pincode":"640046"
        }
    }
}

result.json


{
    "user": [
        {
            "firstName": "Sudharsan",
            "lastName": "Tettu",
            "age": 35
        },
        {
            "firstName": "TestFirstName",
            "lastName": "TestLastName",
            "age": 135
        },
        {
            "firstName": "TestFirstName1",
            "lastName": "TestLastName1",
            "age": 10,
            "address": {
                "street": "Street 33",
                "pincode": "640046"
            }
        }
    ]
}

Scenerio 2
With {duplicates :false} ,it will override the previous values for the key.

{
    "user": {
        "firstName": "TestFirstName1",
        "lastName": "TestLastName1",
        "age": 10,
        "address": {
            "street": "Street 33",
            "pincode": "640046"
        }
    }
}

@alexxlagutin
Copy link
Author

Hi! Looks like it should work for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment