Reputation: 690
I have two JSON objects as below
one.json
[
{
"name": "testname",
"phone": 212121,
"seq" : 1
},
{
"name": "testname1",
"phone": 2146354564,
"seq" : 2
},
{
"name": "testname2",
"phone": 12312,
"seq" : 3
},
{
"name": "testname1",
"phone": 211221,
"seq" : 4
},
{
"name": "testname3",
"phone": 10218550,
"seq" : 5
},
{
"name": "1testname",
"phone": 212121,
"seq" : 6
},
{
"name": "testname12",
"phone": 2146354564,
"seq" : 7
},
{
"name": "testname255",
"phone": 12312,
"seq" : 8
},
{
"name": "testname123",
"phone": 211221,
"seq" : 9
},
{
"name": "testname2133",
"phone": 10218550,
"seq" : 10
}
]
two.json
[ {
"success" : true,
"created" : true,
"id" : "123",
"errors" : [ ]
}, {
"success" : true,
"created" : true,
"id" : "12121",
"errors" : [ ]
},{
"success" : false,
"created" : false,
"id" : null,
"errors" : [ {
"message" : "Use one of these records?",
"fields" : [ ],
"statusCode" : "DUPLICATES_DETECTED",
"extendedErrorDetails" : null
} ]
}, {
"success" : false,
"created" : false,
"id" : null,
"errors" : [ {
"message" : "Use one of these records?",
"fields" : [ ],
"statusCode" : "DUPLICATES_DETECTED",
"extendedErrorDetails" : null
} ]
}, {
"success" : false,
"created" : false,
"id" : null,
"errors" : [ {
"message" : "Use one of these records?",
"fields" : [ ],
"statusCode" : "DUPLICATES_DETECTED",
"extendedErrorDetails" : null
} ]
}, {
"success" : false,
"created" : false,
"id" : null,
"errors" : [ {
"message" : "Use one of these records?",
"fields" : [ ],
"statusCode" : "DUPLICATES_DETECTED",
"extendedErrorDetails" : null
} ]
}, {
"success" : false,
"created" : false,
"id" : null,
"errors" : [ {
"message" : "Use one of these records?",
"fields" : [ ],
"statusCode" : "DUPLICATES_DETECTED",
"extendedErrorDetails" : null
} ]
}, {
"success" : false,
"created" : false,
"id" : null,
"errors" : [ {
"message" : "Use one of these records?",
"fields" : [ ],
"statusCode" : "DUPLICATES_DETECTED",
"extendedErrorDetails" : null
} ]
}, {
"success" : false,
"created" : false,
"id" : null,
"errors" : [ {
"message" : "Use one of these records?",
"fields" : [ ],
"statusCode" : "DUPLICATES_DETECTED",
"extendedErrorDetails" : null
} ]
}, {
"success" : false,
"created" : false,
"id" : null,
"errors" : [ {
"message" : "Use one of these records?",
"fields" : [ ],
"statusCode" : "DUPLICATES_DETECTED",
"extendedErrorDetails" : null
} ]
} ]
I am getting two.json(which is output after some functionality) in the same order as one.json
this is my final output json i want to achieve
final.json
[
{
"name": "testname",
"phone": 212121,
"seq": 1,
"success": true,
"created": true,
"id": "11",
"errors": []
},
{
"name": "testname1",
"phone": 2146354564,
"seq": 2,
"success": true,
"created": true,
"id": "323",
"errors": []
},
{
"name": "testname2",
"phone": 12312,
"seq": 3,
"success": false,
"created": false,
"id": null,
"errors": [
{
"message": "Use one of these records?",
"fields": [],
"statusCode": "DUPLICATES_DETECTED",
"extendedErrorDetails": null
}
]
},
{
"name": "testname1",
"phone": 211221,
"seq": 4,
"success": false,
"created": false,
"id": null,
"errors": [
{
"message": "Use one of these records?",
"fields": [],
"statusCode": "DUPLICATES_DETECTED",
"extendedErrorDetails": null,
"success": false,
"created": false,
"id": null,
"errors": [
{
"message": "Use one of these records?",
"fields": [],
"statusCode": "DUPLICATES_DETECTED",
"extendedErrorDetails": null
}
]
}
]
},
{
"name": "testname3",
"phone": 10218550,
"seq": 5,
"success": false,
"created": false,
"id": null,
"errors": [
{
"message": "Use one of these records?",
"fields": [],
"statusCode": "DUPLICATES_DETECTED",
"extendedErrorDetails": null
}
]
},
{
"name": "1testname",
"phone": 212121,
"seq": 6,
"success": false,
"created": false,
"id": null,
"errors": [
{
"message": "Use one of these records?",
"fields": [],
"statusCode": "DUPLICATES_DETECTED",
"extendedErrorDetails": null
}
]
},
{
"name": "testname12",
"phone": 2146354564,
"seq": 7,
"success": false,
"created": false,
"id": null,
"errors": [
{
"message": "Use one of these records?",
"fields": [],
"statusCode": "DUPLICATES_DETECTED",
"extendedErrorDetails": null
}
]
},
{
"name": "testname255",
"phone": 12312,
"seq": 8,
"success": false,
"created": false,
"id": null,
"errors": [
{
"message": "Use one of these records?",
"fields": [],
"statusCode": "DUPLICATES_DETECTED",
"extendedErrorDetails": null
}
]
},
{
"name": "testname123",
"phone": 211221,
"seq": 9,
"success": false,
"created": false,
"id": null,
"errors": [
{
"message": "Use one of these records?",
"fields": [],
"statusCode": "DUPLICATES_DETECTED",
"extendedErrorDetails": null
}
]
},
{
"name": "testname2133",
"phone": 10218550,
"seq": 10,
"success": false,
"created": false,
"id": null,
"errors": [
{
"message": "Use one of these records?",
"fields": [],
"statusCode": "DUPLICATES_DETECTED",
"extendedErrorDetails": null
}
]
}
]
So the order of output will be same in the order of input sequence. So i need to append two.json key values in the same order into one.json
Please suggest me how can i achieve this.
Upvotes: 1
Views: 86
Reputation: 14094
You can do this in pandas
import pandas as pd
df1 = pd.DataFrame(j1)
df2 = pd.DataFrame(j2)
jsn = df1.merge(df2, left_index=True, right_index=True).to_dict(orient='records')
Then save the jsn
with open ('merged_json.json', 'w') as f:
json.dump(jsn, f, indent=4)
Here is a sample of the first 3 records
[
{
"name": "testname",
"phone": 212121,
"seq": 1,
"created": true,
"errors": [],
"id": "123",
"success": true
},
{
"name": "testname1",
"phone": 2146354564,
"seq": 2,
"created": true,
"errors": [],
"id": "12121",
"success": true
},
{
"name": "testname2",
"phone": 12312,
"seq": 3,
"created": false,
"errors": [
{
"message": "Use one of these records?",
"fields": [],
"statusCode": "DUPLICATES_DETECTED",
"extendedErrorDetails": null
}
],
"id": null,
"success": false
},
This should be the most efficient approach, read more here
Upvotes: 1
Reputation: 5757
You can try:
output = []
for a,b in zip(one_json, two_json):
a.update(b)
output.append(a)
print(output)
Upvotes: 1