0

I am currently implementing some test automation that uses a json POST to a REST API to initialize the test data in the SUT. Most of the fields I don't have an issue editing using information I found in another thread: Json handling in ROBOT

However, one of the sets of information I am editing is a dictionary of meta data.

{
    "title": "Test Auotmation Post 2018-03-06T16:12:02Z",
    "content": "dummy text",
    "excerpt": "Post made by automation for testing purposes.",
    "name": "QA User",
    "status": "publish",
    "date": "2018-03-06T16:12:02Z",
    "primary_section": "Entertainment",
    "taxonomy": {
        "section": [
            "Entertainment"
        ]
    },
    "coauthors": [
        {
            "name": "QA User - CoAuthor",
            "meta": {
                "Title": "QA Engineer",
                "Organization": "That One Place"
            }
        }
    ],
    "post_meta": [
        {
            "key": "credit",
            "value": "QA Engineer"
        },
        {
            "key": "pub_date",
            "value": "2018-03-06T16:12:02Z"
        },
        {
            "key": "last_update",
            "value": "2018-03-06T16:12:02Z"
        },
        {
            "key": "source",
            "value": "wordpress"
        }
    ]
}

Is it possible to use the Set to Dictionary Keyword on a dictionary inside a dictionary? I would like to be able to edit the value of the pub_date and last_update inside of post_meta, specifically.

Chris Lovell
  • 3
  • 1
  • 4

2 Answers2

1

The most straightforward way would be to use the Evaluate keyword, and set the sub-dict value in it. Presuming you are working with a dictionary that's called ${value}:

Evaluate     $value['post_meta'][1]['pub_date'] = 'your new value here'

I won't get into how to find the index of the post_meta list that has the 'key' with value 'pub_date', as that's not part of your question.

Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
0

Is it possible to use the Set to Dictionary Keyword on a dictionary inside a dictionary?

Yes, it's possible.

However, because post_meta is a list rather than a dictionary, you will have to write some code to iterate over all of the values of post_meta until you find one with the key you want to update.

You could do this in python quite simply. You could also write a keyword in robot to do that for you. Here's an example:

*** Keywords ***
Set list element by key
    [Arguments]  ${data}  ${target_key}  ${new_value}

    :FOR  ${item}  IN  @{data}
    \  run keyword if  '''${item['key']}''' == '''${target_key}'''
    \  ...  set to dictionary  ${item}  value=${new_value}

    [return]  ${data}

Assuming you have a variable named ${data} contains the original JSON data as a string, you could call this keyword like the following:

${JSON}=  evaluate  json.loads('''${data}''')    json

set list element by key  ${JSON['post_meta']}  pub_date     yesterday
set list element by key  ${JSON['post_meta']}  last_update  today

You will then have a python object in ${JSON} with the modified values.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • This new keyword, and example are perfect. Thank you, Bryan, for the assistance as well as the correction on it being a list. Just diving in headfirst and still learning. – Chris Lovell Jun 19 '18 at 19:35