Python 插入到 JSON

huangapple go评论66阅读模式
英文:

Python Insert into json

问题

Here are the translated portions:

  1. Let's say I want to insert "Daniel.Michaelson@outlook.com:1234567" into the JSON, so it would add
"One": "Daniel.Michaelson@outlook.com:1234567",
"Two": [""]

How would I do it?

  1. Let's say I want to insert another row into "Two," for "Matt.Demon@outlook.com," how would I do this in Python?

Thank you!

英文:

so I need to make a little json database with Python and JSON and insert new data into it.

Here's what I am trying to do:

{
	"One": "catherine.hamilton@outlook.com:1234567",
	"Two": ["catherine.hamilton@outlook.com:1234567", "catherine.hamilton@outlook.com:1234567"]
}

So I want to insert multiple instances of like this:

{
	"One": "catherine.hamilton@outlook.com:1234567",
	"Two": ["catherine.hamilton@outlook.com:1234567", "catherine.hamilton@outlook.com:1234567"],
	"One": "John.Jones@outlook.com:1234567",
	"Two": ["John.Jones@outlook.com:1234567", "John.Jones@outlook.com:1234567"],
	"One": "Matt.Demon@outlook.com:1234567",
	"Two": ["Matt.Demon@outlook.com:1234567", "Matt.Demon@outlook.com1234567"],
	"One": "Adam.Baker@outlook.com:1234567",
	"Two": ["Matt.Demon@outlook.com:1234567", "Matt.Demon@outlook.com:1234567"],

}

(I know this is invalid json syntax, i'm not sure what the correct would be).

  1. Let's say I want to insert "Daniel.Michaelson@outlook.com:1234567" into the json, so it would add

    "One": "Daniel.Michaelson@outlook.com:1234567",
    "Two": [""],

How would I do it?

  1. Let's say I want to insert another row into Two, for Matt.Demon@outlook.com, how would I do this in Python?

Thank you!

答案1

得分: 0

As you said this isn't a valid JSON, so your question isn't very clear. JSONs are built from keys and values; however, keys need to be injective. This means that every two keys should be different from each other.

Going back to your question. In Python, a dictionary is used to store JSON-like data. So you actually need to store all your data in a dictionary and then convert it to JSON. For example:

import json

data = {"One": "catherine.hamilton@outlook.com:1234567",
    "Two": ["catherine.hamilton@outlook.com:1234567", "catherine.hamilton@outlook.com:1234567"]
        }

json_s = json.dumps(data)

If you already have a JSON string and want to convert it to a dictionary, use json.loads:

data2 = json.loads(json_s)
英文:

As you said this isn't a valid json so your question isn't very clear. Jsons are built from keys and values, however keys need to be injective. This means that every two keys should be different from each other.

Going back to your question. In python dictionary is used to store a json like data. So you actually need to store all your data in a dictionary and than convert it to json. for example:

import json

data = {"One": "catherine.hamilton@outlook.com:1234567",
    "Two": ["catherine.hamilton@outlook.com:1234567", "catherine.hamilton@outlook.com:1234567"]
        }

json_s = json.dumps(data)

if you already have a json string and want to convert it to dictionary use loads:

data2 = json.loads(json_s)

答案2

得分: -1

这是基本的想法。您可以考虑创建一个“import_data”函数,从您的文件中读取并返回数据结构,以及一个“export_data”函数,用于将数据写回文件。

import json
jjj = """\
[
    {
        "One": "catherine.hamilton@outlook.com:1234567",
        "Two": ["catherine.hamilton@outlook.com:1234567", "catherine.hamilton@outlook.com:1234567"]
    }
]"""

data = json.loads(jjj)

data.append({
    "One": "John.Jones@outlook.com:1234567",
    "Two": ["John.Jones@outlook.com:1234567", "John.Jones@outlook.com:1234567"]
})
data.append({
    "One": "Matt.Demon@outlook.com:1234567",
    "Two": ["Matt.Demon@outlook.com:1234567", "Matt.Demon@outlook.com1234567"]
})
data.append({
    "One": "Adam.Baker@outlook.com:1234567",
    "Two": ["Matt.Demon@outlook.com:1234567", "Matt.Demon@outlook.com:1234567"]
})

print(json.dumps(data, indent=4))

输出:

[
    {
        "One": "catherine.hamilton@outlook.com:1234567",
        "Two": [
            "catherine.hamilton@outlook.com:1234567",
            "catherine.hamilton@outlook.com:1234567"
        ]
    },
    {
        "One": "John.Jones@outlook.com:1234567",
        "Two": [
            "John.Jones@outlook.com:1234567",
            "John.Jones@outlook.com:1234567"
        ]
    },
    {
        "One": "Matt.Demon@outlook.com:1234567",
        "Two": [
            "Matt.Demon@outlook.com:1234567",
            "Matt.Demon@outlook.com1234567"
        ]
    },
    {
        "One": "Adam.Baker@outlook.com:1234567",
        "Two": [
            "Matt.Demon@outlook.com:1234567",
            "Matt.Demon@outlook.com:1234567"
        ]
    }
]
英文:

This is the basic idea. You might consider having an "import_data" function that reads from your file and returns the data structure, and an "export_data" function that writes it back out.

import json
jjj = """\
[
    {
        "One": "catherine.hamilton@outlook.com:1234567",
        "Two": ["catherine.hamilton@outlook.com:1234567", "catherine.hamilton@outlook.com:1234567"]
    }
]"""

data = json.loads( jjj )

data.append({
    "One": "John.Jones@outlook.com:1234567",
    "Two": ["John.Jones@outlook.com:1234567", "John.Jones@outlook.com:1234567"]
})
data.append({
    "One": "Matt.Demon@outlook.com:1234567",
    "Two": ["Matt.Demon@outlook.com:1234567", "Matt.Demon@outlook.com1234567"]
})
data.append({
    "One": "Adam.Baker@outlook.com:1234567",
    "Two": ["Matt.Demon@outlook.com:1234567", "Matt.Demon@outlook.com:1234567"]
})

print(json.dumps(data, indent=4))

Output:

[
    {
        "One": "catherine.hamilton@outlook.com:1234567",
        "Two": [
            "catherine.hamilton@outlook.com:1234567",
            "catherine.hamilton@outlook.com:1234567"
        ]
    },
    {
        "One": "John.Jones@outlook.com:1234567",
        "Two": [
            "John.Jones@outlook.com:1234567",
            "John.Jones@outlook.com:1234567"
        ]
    },
    {
        "One": "Matt.Demon@outlook.com:1234567",
        "Two": [
            "Matt.Demon@outlook.com:1234567",
            "Matt.Demon@outlook.com1234567"
        ]
    },
    {
        "One": "Adam.Baker@outlook.com:1234567",
        "Two": [
            "Matt.Demon@outlook.com:1234567",
            "Matt.Demon@outlook.com:1234567"
        ]
    }
]

huangapple
  • 本文由 发表于 2023年3月21日 01:40:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定