Search code examples
pythonjsonmergechatbot

Questions about json merge into python


Well my question actually isn't really about merge but about how I make it to not slice the whole dictionary and keep consistency in the end. Remembering that I'm reading n files in json with undefined size. The files are these

{"intents": [
        {"tag": "greeting",
         "patterns": ["Hi", "How are you", "Is anyone there?", "Hello", "Good day", "Whats up"],
         "responses": ["Hello!", "Good to see you again!", "Hi there, how can I help?"],
         "context_set": ""
        }
   ]
}
{"intents": [
        {"tag": "goodbye",
         "patterns": ["cya", "See you later", "Goodbye", "I am Leaving", "Have a Good day"],
         "responses": ["Sad to see you go :(", "Talk to you later", "Goodbye!"],
         "context_set": ""
        }
   ]
}
{"intents": [
        {"tag": "thanks",
         "patterns": ["Thanks", "Thank you", "That's helpful"],
         "responses": ["Happy to help!", "Any time!", "My pleasure"],
         "context_set": ""
        },
        {"tag": "payments",
         "patterns": ["Do you take credit cards?", "Do you accept Mastercard?", "Are you cash only?" ],
         "responses": ["We accept VISA, Mastercard and AMEX", "We accept most major credit cards"],
         "context_set": ""
        }
   ]
}

what i've done so far and how it works it takes any json file and puts it in a merged to save to json file. This is the code.

import json
import os

finaljson2 = {"intents" : []}
for filename in os.listdir('/content/'):
    if filename.endswith('.json'):
        with open(os.path.join('/content/', filename)) as file:
            data = json.load(file)
    middle= data["intents"][0]
    finaljson2["intents"].append(middle)

with open('merged.json', "w") as f:
    f.write(json.dumps(finaljson2, indent=2))

the end result

{
  "intents": [
    {
      "tag": "goodbye",
      "patterns": [
        "cya",
        "See you later",
        "Goodbye",
        "I am Leaving",
        "Have a Good day"
      ],
      "responses": [
        "Sad to see you go :(",
        "Talk to you later",
        "Goodbye!"
      ],
      "context_set": ""
    },
    {
      "tag": "greeting",
      "patterns": [
        "Hi",
        "How are you",
        "Is anyone there?",
        "Hello",
        "Good day",
        "Whats up"
      ],
      "responses": [
        "Hello!",
        "Good to see you again!",
        "Hi there, how can I help?"
      ],
      "context_set": ""
    },
    {
      "tag": "thanks",
      "patterns": [
        "Thanks",
        "Thank you",
        "That's helpful"
      ],
      "responses": [
        "Happy to help!",
        "Any time!",
        "My pleasure"
      ],
      "context_set": ""
    },
    {
      "tag": "goodbye",
      "patterns": [
        "cya",
        "See you later",
        "Goodbye",
        "I am Leaving",
        "Have a Good day"
      ],
      "responses": [
        "Sad to see you go :(",
        "Talk to you later",
        "Goodbye!"
      ],
      "context_set": ""
    },
    {
      "tag": "goodbye",
      "patterns": [
        "cya",
        "See you later",
        "Goodbye",
        "I am Leaving",
        "Have a Good day"
      ],
      "responses": [
        "Sad to see you go :(",
        "Talk to you later",
        "Goodbye!"
      ],
      "context_set": ""
    },
    {
      "tag": "goodbye",
      "patterns": [
        "cya",
        "See you later",
        "Goodbye",
        "I am Leaving",
        "Have a Good day"
      ],
      "responses": [
        "Sad to see you go :(",
        "Talk to you later",
        "Goodbye!"
      ],
      "context_set": ""
    }
  ]
}

In the end he ends up writing after he reads only the first part, I already tried to put it inside a conditional and it didn't work very well. That's why I came here to ask if anyone has any different ideas that might work.


Solution

  • try this:

    import json
    import os
    
    finaljson2 = {"intents" : []}
    for filename in os.listdir('/content/'):
        if filename.endswith('.json'):
            with open(os.path.join('/content/', filename)) as file:
                data = json.load(file)
    
                finaljson2["intents"].extend(data.get("intents",[]))
    
    with open('merged.json', "w") as f:
        f.write(json.dumps(finaljson2, indent=2))