karate

How to merge 2 JSON payloads using Karate


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?


Solution

  • I suggest you work with someone to learn the basics of manipulating JSON in JS. You can use this as a starting point: https://stackoverflow.com/a/76091034/143475

    This can be just one line:

    * def data = input2.map(x => { x._user_id = input1._user_id; return x })