2

I would like to merge multiple json files into a single object or file. An example JSON object could be

{
"food": {
    "ingredents": [

            "one": "this",
            "two": "that",

            ]

        }
  }

and

"food": {
    "amount": [

            "tablespoons": "3",

            ]
        }
  }

I would like to be able to merge these so that we get

"food": {
    "ingredents": [

            "one": "this",
            "two": "that",

            ],
      "amount": [

            "tablespoons": "3",

            ]
        }

  }

so that they are all combined via the parent keys instead of just as a list where "food" would repeat itself. Also I would like the outgoing file to replace anything that is repeated, such as if ingredients "one" : "this" existed somewhere else it would only appear once.

Any help would be greatly appreciated. Specifically something that iterates through a list of JSON files and applies the method would be ideal.

I have tried using glob and iterating through the JSON files such as

ar = []
for f in glob.glob("*.json"):
    with open(f, "rb") as filename:
        ar.append(json.load(filename))

with open("outfile.json", "w") as outfile:
    json.dump(ar, outfile)

yet this just gives me a list of JSON objects without connecting them by key.

I could likely write one solution where it collects the key data and uses conditionals to determine where to place an object inside a certain key, but this would require a lot more work especially since I am dealing with a large amount of files. If there is a simpler solution that would be amazing.

elephant
  • 69
  • 2
  • 9
  • What solutions have you tried? – xDaevax Aug 21 '15 at 16:02
  • Yes, we aren't here to write your code, but usually we are happy to tell you what you are doing wrong if you've made an honest attempt at solving the problem yourself – holdenweb Aug 21 '15 at 16:04
  • @xDaevax I have tried iterating throguh the files with glob and using json.load(each file) and collecting them in an array and then dumping the json file. This though, causes me to have a list of json objects that are not actually combined – elephant Aug 21 '15 at 16:08
  • I am new to python and JSON and was just wondering if you know of any library or solutions that could aid with this, everyone I have looked up hasn't worked correctly. – elephant Aug 21 '15 at 16:09
  • 1
    You should add what you've tried to your question. This will make the question better and prevent further downvotes. It also gives people with potential answers clues as to what you've already tried. Have a read here: http://stackoverflow.com/help/how-to-ask – xDaevax Aug 21 '15 at 16:09
  • Thanks @xDaevax. Will do – elephant Aug 21 '15 at 16:14
  • @elephant: the code you posted did not do anything to begin to attempt to merge; it just collected each object as-is in a list. – dsh Aug 21 '15 at 16:24

1 Answers1

1

Not sure which libraries you've tried that you say didn't work correctly for your needs, but I would recommend lodash. It's an incredibly fast, tiny, robust library to handle these kinds of operations. For this specific case you could easily accomplish it with lodash merge https://lodash.com/docs#merge

example:

var users = {
  'data': [{ 'user': 'barney' }, { 'user': 'fred' }]
};

var ages = {
  'data': [{ 'age': 36 }, { 'age': 40 }]
};

_.merge(users, ages);
// → { 'data': [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }] }
wesww
  • 2,863
  • 18
  • 16
  • Great, I will check it out – elephant Aug 21 '15 at 16:21
  • 2
    alternately, underscore is also solid. lodash is generally preferred though http://stackoverflow.com/questions/13789618/differences-between-lodash-and-underscore and it seems like they're both on track to merge into one single library in the future – wesww Aug 21 '15 at 16:22