关于 json 的问题合并到 python

Questions about json merge into python

好吧,我的问题实际上不是关于合并,而是关于我如何做到不分割整个字典并最终保持一致性。请记住,我正在读取 json 中 n 个未定义大小的文件。 这些文件是这些

{"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": ""
        }
   ]
}

到目前为止我做了什么以及它是如何工作的它需要任何 json 文件并将其合并以保存到 json 文件。 这是代码。

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))

最终结果

{
  "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": ""
    }
  ]
}

最后他只看了第一部分就写了,我已经试过把它放在条件语句中,但效果不佳。这就是为什么我来这里询问是否有人有任何不同的想法可能有效。

试试这个:

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))