In my project, there is a requirement for me to get user_id from Endpoint 1,
Then merge with sessions data from endpoint 2.
Input 1:
{_user_id: "user1"}
Input 2:
{
"_session_id": "5182d9fe-1f4f-484b-88c2-708f32cdd661"
},
{
"_session_id": "372cf574-2526-41ed-9a9b-77ecc50b0d66"
}
Expected output
{
"_user_id: "user1",
"_session_id": "5182d9fe-1f4f-484b-88c2-708f32cdd661"
},
{
"_user_id: "user1",
"_session_id": "372cf574-2526-41ed-9a9b-77ecc50b0d66"
}
Is there a way to achieve this in Karate?
Please use keyword set in Karate
Feature: Merge JSON Payloads from different end points
Scenario: Merge JSON Payloads
* def response1 = { "_user_id": "user1" }
* print response1
* def response2 =
"""
[
{
"_session_id": "5182d9fe-1f4f-484b-88c2-708f32cdd661"
},
{
"_session_id": "372cf574-2526-41ed-9a9b-77ecc50b0d66"
}
]
"""
* print response2
* set response2[0]._user_id = response1._user_id
* set response2[1]._user_id = response1._user_id
* print response2
Html report
1
I suggest you work with someone to learn the basics of manipulating JSON in JS. You can use this as a starting point: /a/76091034/143475
This can be just one line:
* def data = input2.map(x => { x._user_id = input1._user_id; return x })
1