Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

How to get Values of one JSON object into the Values of another with Python


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to unpack the JSON objects, add them together(see code), and then put them back into a new JSON object.
Code:

import json
d4=json.loads(payload4)
d5=json.loads(payload5)
d4["Values"].extend(d5["Values"])
newpayload=json.dumps(d4)

Output:

>>> print(newpayload)
{"Values": [{"Name": "projectionLength", "Attribute": "Test"}, {"Name": "projectionHeight", "Attribute": "Test"}, {"Name": "projectionLength", "Attribute": "Test5"}, {"Name": "projectionHeight", "Attribute": "Test6"}]}

Nicer looking version just to give you a feel of whats stored in newpayload(this is not the real JSON text):

>>> import pprint
>>> pprint.pprint(d4)
{'Values': [{'Attribute': 'Test', 'Name': 'projectionLength'},
            {'Attribute': 'Test', 'Name': 'projectionHeight'},
            {'Attribute': 'Test5', 'Name': 'projectionLength'},
            {'Attribute': 'Test6', 'Name': 'projectionHeight'}]}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...