简体   繁体   中英

Best way to update json payload from cucumber feature file

I have a complex Json which acts like a input to a webservice. I have created necessary POJO classes for it and I am using Jackson object mapper in my code.

What could be the best way to update the partial Json data from cucumber data tables?

Thanks in advance !!!

Original Json:

{
    "accNum": "Dummy_Account",
    "customerData": {
        "customerName": "Dummy_Name",
        "customerAddress": "Dummy_Address"
        
    },
    "accountData": {
        "cashAccountRef": "Dummy",
        "acntCrncy": "EUR",
        "foreignCurrencyAccounts": [
            {
                "foreignCurrency": "USD",
                "foreignCrncyAcntRef": "Dummy2"
                
            }
        ]
    }
    ]
}

Cucumber Feature File

Given Prepare Input request for below data
      | accNum                                                 | 12345   |
      | customerData.customerName                              | New Name|
      | accountData.foreignCurrencyAccounts[0].foreignCurrency | EUR     |

Final Json to be passed as input

{
    "accNum": "12345",
    "customerData": {
        "customerName": "New Name",
        "customerAddress": "Dummy_Address"
        
    },
    "accountData": {
        "cashAccountRef": "Dummy",
        "acntCrncy": "EUR",
        "foreignCurrencyAccounts": [
            {
                "foreignCurrency": "EUR",
                "foreignCrncyAcntRef": "Dummy2"
                
            }
        ]
    }
    ]
}

If you're using POJO then stick to this approach. Using setter to update value of the key, of course you can use builder style for setter to make it more easier to read.

I found only this way

((ObjectNode) parent).putRawValue(valueOfKeyToModify, new RawValue(newValue));

The problem arises when there are many identical keys. And then you need to read the tree and go around it until it matches exactly the key that needs to be changed.

And if you need to lay string, then you must also specify quotes, since it lays the raw value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM