0

I am writing a test with the robot framework. I am trying to update values in a json field, but I am seeing some odd behavior.

I do a HTTP GET and receive a json. I check what the value is originally,

Log To Console | ${JSON["Components"]["SubComponents"]}

which returns

valueA

Then I try to update the values as below.

${JSON["Components"]["SubComponents"]}= | Set Variable | valueB

If I log the output this way

Log To Console | ${JSON["Components"]["SubComponents"]}

I see

valueB

but if I check this way

Log To Console | ${JSON["Components"]}

I see

{u'SubComponents': valueA, u'MoreComponents': whatever}

What is going on and why is it not updating?

I need to update the JSON field so I can do an HTTP PATCH. I have tried following Json handling in ROBOT But it doesn't work and I think it has something to do with my JSON file having single quotes.

FYI I am new to json and robotframework

Community
  • 1
  • 1
scarlso9
  • 195
  • 3
  • 14
  • Where is the Python code, and why are you piping things? You can edit the JSON variable within Python – OneCricketeer Feb 02 '17 at 20:13
  • 1
    The pipe is how the robotframework works.. or you can use two spaces. All the attempts I've made to edit it within python using Evaluate have failed.. the post I linked to attempts to do that but it doesn't work for me and I can't figure out how to fix it. – scarlso9 Feb 02 '17 at 20:17
  • 1
    @cricket_007 - the `|` is used as a keyword separator, not the generic pipe we know normally. Can either use a pipe or space separator - matter of preference. – Goralight Feb 02 '17 at 20:18
  • 1
    I have actually found a solution.. If I do `Set To Dictionary | ${JSON["Components"]} | SubComponents=valueB` then I see the proper behavior.. I still would like to know what is going on above and why I see it in one case but not the other. This is part of the proposed solution in the post I linked to but I was unable to get past the json.load() due to problems so I never got to try it. – scarlso9 Feb 02 '17 at 20:28

1 Answers1

4

You can't set variables that way. You have to keep reminding yourself that robot is not a programming language. What you've done is that you've created a variable named, literally, ${JSON["Components"]["SubComponents"]}. You are not updating the original ${JSON} variable.

You can see this if you use the keyword Log variables. You'll see this new keyword with a name that looks like a dictionary.

To change the value of a dictionary you will need to use a keyword such as Set to dictionary

For example:

set to dictionary    ${JSON["Components"]}   Subcomponents=valueb
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks for the explanation! That makes sense now that I realize it. It seems a little strange that robot would allow for a variable to be named the same as an entry in a dictionary.. but I'm guessing thats because like you said its not a programming language. -- you must've posted this at the same time I put my comment – scarlso9 Feb 02 '17 at 20:33