3

Still learning Python and trying to combine 2 JSON objects.

Primary Data Set:

{
    "form-fields": [
        {"type": "text", "x-position": 0, "y-position": 0, "field": "buyer-1"},
        {"type": "text", "x-position": 0, "y-position": 10, "field": "buyer-2"}
    ]
}

Secondary Data Set:

{
    "form-data": [
        {"field": "buyer-1", "value": "John Smith"},
        {"field": "buyer-2", "value": "Susan Smith"}
    ]
}

Intended Result: With "value" appended to the original object.

{
    "form-fields": [
        {"type": "text", "x-position": 0, "y-position": 0, "field": "buyer-1", "value": "John Smith"},
        {"type": "text", "x-position": 0, "y-position": 10, "field": "buyer-2", "value": "Susan Smith"}
    ]
}
1
  • like merging element at first index with first index element.. and second with second index element? Commented Jun 11, 2022 at 17:58

2 Answers 2

3

I assume that the fields are not in order between primary and secondary. The plan is to create a dictionary call update where key=field and value=secondary's value. Then update the primary.

primary = {
    "form-fields": [
        {"type": "text", "x-position": 0, "y-position": 0, "field": "buyer-1"},
        {"type": "text", "x-position": 0, "y-position": 10, "field": "buyer-2"}
    ]
}

secondary = {
    "form-data": [
        {"field": "buyer-1", "value": "John Smith"},
        {"field": "buyer-2", "value": "Susan Smith"}
    ]
}

update = {
    record["field"]: record["value"]
    for record in secondary["form-data"]
}
# update is {'buyer-1': 'John Smith', 'buyer-2': 'Susan Smith'}

for record in primary["form-fields"]:
    if record["field"] in update:
        record["value"] = update[record["field"]]

The result is primary to become

{
    "form-fields": [
        {
            "type": "text",
            "x-position": 0,
            "y-position": 0,
            "field": "buyer-1",
            "value": "John Smith"
        },
        {
            "type": "text",
            "x-position": 0,
            "y-position": 10,
            "field": "buyer-2",
            "value": "Susan Smith"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hai Vu - thanks so much for this great answer! This was exactly what I was looking for. I learned a lot from @Nandha's answer, but your solution makes more sense because the field's may not be in order and need to match exactly on their key values. I marked your answer as the accepted answer. Thanks a million sir!
2

Assuming length and index will be same in both dictionaries,

primary = {
    "form-fields": [
        {"type": "text", "x-position": 0, "y-position": 0, "field": "buyer-1"},
        {"type": "text", "x-position": 0, "y-position": 10, "field": "buyer-2"}
    ]
}

sec = {
    "form-data": [
        {"field": "buyer-1", "value": "John Smith"},
        {"field": "buyer-2", "value": "Susan Smith"}
    ]
}

pl = primary.get("form-fields")
sl = sec.get("form-data")

for index, prim_val in enumerate(pl):
    sec_val = sl[index]
    prim_val.update(sec_val)
    
print(primary)

Sample code for merging dictionaries

marks = {'Physics':67, 'Maths':87}
internal_marks = {'Practical':48}

marks.update(internal_marks)


print(marks)

# Output: {'Physics': 67, 'Maths': 87, 'Practical': 48}

1 Comment

Nandha this was a very elegant solution. Thank you so much for the thoughtful response! I didn't know about enumerate, so I added print statements in the for loop for print(index, prim_val) and print(sl[index]). I ended up accepting Hai Vu's answer only because the order might not be the same. For example the buyer-2 might come before buyer-1 in the form-data. I wasn't super specific in my original post, so apologies for that. I definitely learned a lot from your answer with enumerate and the sample code for merging dictionaries. Thank you!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.