robotframework - Json handling in ROBOT -


i have json file , in there field need edit , save file next usage.

but field need edit shown below,

the val need assing fr field generated randomly in run time i'll capturing in variable , pass json specific key "dp" save json.

the saved json used rest post url.

            {                 "p": "10",                 "v": 100,                 "vt": [                     {                         "dp": "field edited"(integer value) ,                    ]                       } 

please me out , using robot framework, need update json field in run time.

the simplest solution write python keyword can change value you. however, can solve robot keywords performing following steps:

  1. convert json string dictionary
  2. modify dictionary
  3. convert dictionary json string

convert json string dictionary

python has module (json) working json data. can use evaluate keyword convert json string python dictionary using loads (load string) method of module.

assuming json data in robot variable named ${json_string}, can convert python dictionary this:

${json}=    evaluate    json.loads('''${json_string}''')    json 

with above, ${json} holds reference dictionary contains of json data.

modify dictionary

the collections library comes robot has keyword named set dictionary can used set value of dictionary element. in case need change value of dictionary nested inside vt element of json object. can reference nested dictionary using robot's extended variable syntax.

for example:

set dictionary    ${json["vt"]}    dp=the new value 

with that, ${json} has new value. however, still python dictionary rather json data, there's 1 more step.

convert dictionary json

converting dictionary json reverse of first step. namely, use dumps (dump string) method of json module:

${json_string}=    evaluate    json.dumps(${json})    json 

with that, ${json_string} contain valid json string modified data.


complete example

the following complete working example. json string printed before , after substitution of new value:

*** settings *** library    collections  *** test cases *** example     ${json_string}=    catenate     ...  {     ...    "p": "10",     ...    "v": 100,     ...    "vt": {     ...            "dp": "field edited"     ...          }     ...  }      log console       \noriginal json:\n${json_string}     ${json}=             evaluate        json.loads('''${json_string}''')    json     set dictionary    ${json["vt"]}    dp=the new value     ${json_string}=      evaluate        json.dumps(${json})                 json     log console       \nnew json string:\n${json_string} 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -