我最近开始使用Python,我正在try 将我的一个JSON字符串与现有的JSON字符串连接起来.我也在使用ZooKeeper,所以在使用Python kazoo库时,我从ZooKeeper node 获取现有的json字符串.

# gets the data from zookeeper
data, stat = zk.get(some_znode_path)
jsonStringA = data.decode("utf-8")

如果我印jsonStringA张,结果是这样的-

{"error_1395946244342":"valueA","error_1395952003":"valueB"}

But if I do print json.loads(jsonString) then it prints out like this -

{u'error_1395946244342': u'valueA', u'error_1395952003': u'valueB'}

Here jsonStringA will have my existing JSON String. Now I have another key-value pair which I need to add in the exiting jsonStringA -

下面是我的Python代码-

# gets the data from zookeeper
data, stat = zk.get(some_znode_path)
jsonStringA = data.decode("utf-8")

timestamp_in_ms = "error_"+str(int(round(time.time() * 1000)))
node = "/pp/tf/test/v1"
a,b,c,d = node.split("/")[1:]
host_info = "h1"
local_dc = "dc3"
step = "step2"

从zookeeper中提取后,我现有的jsonStringA个将是这样-

{"error_1395946244342":"valueA","error_1395952003":"valueB"}

Now I need to append this key-value pair in the jsonStringA -

"timestamp_in_ms":"Error Occured on machine "+host_info+" in datacenter "+ local_dc +" on the "+ step +" of process "+ c +"

So in short I need to merge below key-value pair -

"error_1395952167":"Error Occured on machine h1 in datacenter dc3 on the step2 of process test"

最后的JSON字符串如下所示-

{"error_1395946244342":"valueA","error_1395952003":"valueB","error_1395952167":"Error Occured on machine h1 in datacenter dc3 on the step2 of process test"}

这有可能做到吗?

推荐答案

假设a和b是您要合并的字典:

c = {key: value for (key, value) in (a.items() + b.items())}

To convert your string to python dictionary you use the following:

import json
my_dict = json.loads(json_str)

Update: full code using strings:

# test cases for jsonStringA and jsonStringB according to your data input
jsonStringA = '{"error_1395946244342":"valueA","error_1395952003":"valueB"}'
jsonStringB = '{"error_%d":"Error Occured on machine %s in datacenter %s on the %s of process %s"}' % (timestamp_number, host_info, local_dc, step, c)

# now we have two json STRINGS
import json
dictA = json.loads(jsonStringA)
dictB = json.loads(jsonStringB)

merged_dict = {key: value for (key, value) in (dictA.items() + dictB.items())}

# string dump of the merged dict
jsonString_merged = json.dumps(merged_dict)

但我必须说,总的来说,你试图做的并不是最佳实践.请阅读一些python字典.


Alternative solution:

jsonStringA = get_my_value_as_string_from_somewhere()
errors_dict = json.loads(jsonStringA)

new_error_str = "Error Ocurred in datacenter %s blah for step %s blah" % (datacenter, step)
new_error_key = "error_%d" % (timestamp_number)

errors_dict[new_error_key] = new_error_str

# and if I want to export it somewhere I use the following
write_my_dict_to_a_file_as_string(json.dumps(errors_dict))

And actually you can avoid all these if you just use an array to hold all your errors.

Json相关问答推荐

使用PowerShell解析文件并获取特定行的值

GO KaZaam转换返回意外结果

集成wix.comstore API|变音符号问题

简单条形图和有序分类中条形图的 colored颜色 梯度

Kotlin Android Room 处理 Moshi TypeConverter 中的空对象列表

如何将动态复杂 json 解析为dart 对象或模型

将嵌套的 JSON 对象规范化为 Pandas 数据框

以 JSON 格式访问(新型、公共)Google 工作表

为什么在测试 RSPEC 时 JBuilder 不返回 JSON 中的响应正文

从多维数组数据生成json字符串

我应该如何处理 JSON 中的 HATEOAS 链接和引用?

ASP.NET MVC 读取原始 JSON 发布数据

如何在 Rails 模型中验证字符串是否为 json

请求返回字节,我无法解码它们

如何使用 Javascript 将数据写入 JSON 文件

强制 JSON.NET 在序列化 DateTime 时包含毫秒(即使 ms 组件为零)

从 JSON 到 JSONL 的 Python 转换

将多个值存储在json中的单个键中

XML vs YAML vs JSON

无法解析 JSON 文件中的 TAB