0

I want to read muliptle keys and values from json response and store it in another file.

I have tried using robot framework.

 name:1
 name:2  
 name: 3     
 : FOR    ${item}    IN RANGE   0    5
  \   ${readname}=     set variable   [${item}]['name']
  append to list ${z}  ${readname}

  writeJson   ${z}

 def writeJson(data):
    with open("data_file.json", "w") as write_file:
       json.dump(data, write_file)

I expected all values but only last value is stored.

Stack Kiddy
  • 556
  • 1
  • 4
  • 10
Mia
  • 448
  • 8
  • 22

1 Answers1

3

The way you are doing it in the sample code, the Append To List is not inside the loop, but called only once - with the last value of ${z}. Try putting it inside it, thus you'll be appending on each iteration of the loop, e.g. each of the values:

:FOR    ${item}    IN RANGE   0    5
  \   ${readname}=     set variable   [${item}]['name']
  \   Append To List ${z}  ${readname}
Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
  • Can i use set to dictionary instead. I need to get the name in json format and store in a file. But if i use that set dictionary it is appending and printing only last values – Mia Nov 12 '19 at 05:38
  • 1
    You can, if the data structure you're storing into is a dictionary, and you have different key names for each element - if you use one and the same, each call will overwrite its value. – Todor Minakov Nov 12 '19 at 05:40
  • ,can you guide me on this link for dictionary code, https://stackoverflow.com/q/58797352/11867978 – Mia Nov 12 '19 at 05:44