Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
80 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 Answer

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'}]}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...