0

I have this JSON object and i like to update a value within the object. I found a way how i should do this on stackoverflow (Json handling in ROBOT) and its failing and i don't understand why.

This is de object:

{"elementKey":"P690-C0-C3-B1","fields":[{"key":"P690-C1-C2-C1-C1-C1-F0","values":[]},{"key":"P690-C0-C2-F8","values":["1200"]},{"key":"P690-C0-C2-F9","values":["22000"]},{"key":"P690-C0-C2-F11","values":["I"]},{"key":"P690-C0-C2-F10","values":["2200"]},{"key":"P690-C0-C2-C0-C0-F0","values":["98-zsg-2"]},{"key":"P690-C1-C0-C0-F1","values":["Personenauto"]},{"key":"P690-C1-C0-C0-F2","values":["Personenauto KVP"]},{"key":"P690-C0-C2-F6","values":["B"]},{"key":"P690-C0-C2-F7","values":["75"]},{"key":"P690-C0-C2-F4","values":["2"]},{"key":"P690-C0-C2-F5","values":["5"]},{"key":"P690-C0-C2-F2","values":["model"]},{"key":"P690-C0-C2-F3","values":["2017"]},{"key":"P690-C1-C2-C2-C2-C1-F0","values":[]},{"key":"P690-C0-C2-F1","values":["merk"]}]}

In Robot frame I made this test, inspired on the given link.

${json_string}=    Set Variable "see text above"
${json}=    Evaluate    json.loads('''${json_string}''')    json
Set To Dictionary    ${json["fields"]} ${new_value} 
${json_string}=    evaluate    json.dumps(${json})    json

With ${new_value} i tried value=shizzleliz, value[0]=shizzleliz, value[1]=shizzleliz, P690-C1-C2-C1-C1-C1-F0=shizzleliz

All give the error: AttributeError: 'list' object has no attribute 'update'

When i change ${json["fields"]} to ${json} then the give value is set to the library but not in de fields section/collection.

Does anyone have a clue of what i'm doing wrong? And if you have a suggestion how i can update the value, i'd like that very much :)

target is to change: {"key":"P690-C1-C2-C1-C1-C1-F0","values":[]} to: {"key":"P690-C1-C2-C1-C1-C1-F0","values":["shizzleliz"]}

Community
  • 1
  • 1
Shizzleliz
  • 55
  • 3
  • 7

2 Answers2

1

For the first part in your question - the error AttributeError: 'list' object has no attribute 'update', you've already seen the comment - you're calling Set To Dictionary on a list object, which cannot pass.

For the second part, in order to set that value when the key is equal to something, you have to iterate over all the list members, and set it based on a condition over the key:

${json_string}=    Set Variable     see text above
${json1}=    Evaluate    json.loads('''${json_string}''')    json
${target value}=    Create List    shizzleiz
:FOR    ${element}      IN      @{json1["fields"]}
\   Run Keyword If      "${element['key']}" == "P690-C1-C2-C1-C1-C1-F0"
...     Set To Dictionary  ${element}   values=${target value}

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

It looks a little cumbersome in RF (compared to python); one remark - it's never a good idea to name a local variable the same as a module - thus I've renamed it to ${json1}

Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
  • This isn't quite right. It sets the value to a string (`"shizzeleliz"`) rather than a list of strings (`["shizzeleliz"]`) – Bryan Oakley Mar 06 '17 at 21:33
  • It really is setting it to a string, right, missed that in the question; updated to create a list var and set to it. – Todor Minakov Mar 07 '17 at 07:15
  • Thx for the awnser, it turned out to be a bit different. But you certainly helped me in the right direction! – Shizzleliz Mar 14 '17 at 07:52
0

I found an easier solution using Catenate where I needed to randomize two values in the json body.

${shizzleiz}=      shizzleiz    # or whatever you want to appear there
${json_string}=   Catenate   {"elementKey":"P690-C0-C3-B1","fields":[{"key":"P690-C1-C2-C1-C1-C1-F0","values":[]},{"key":"P690-C0-C2-F8","values":["1200"]},{"key":"P690-C0-C2-F9","values":["22000"]},{"key":"P690-C0-C2-F11","values":["I"]},{"key":"P690-C0-C2-F10","values":["2200"]},{"key":"P690-C0-C2-C0-C0-F0","values":["98-zsg-2"]},{"key":"P690-C1-C0-C0-F1","values":["Personenauto"]},{"key":"P690-C1-C0-C0-F2","values":["Personenauto KVP"]},{"key":"P690-C0-C2-F6","values":["B"]},{"key":"P690-C0-C2-F7","values":["75"]},{"key":"P690-C0-C2-F4","values":["2"]},{"key":"P690-C0-C2-F5","values":["5"]},{"key":"P690-C0-C2-F2","values":["model"]},{"key":"P690-C0-C2-F3","values":["2017"]},{"key":"P690-C1-C2-C2-C2-C1-F0","values":  ${shizzleiz}  ${the-rest-of-the-long-json-as-a-string}

then continue on with:

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

(basically do the work before reacting to the json function - obviously this requires knowing the values beforehand and could also work with more variables.)

vladwoguer
  • 951
  • 1
  • 14
  • 28
  • 1
    Please format your answer using four blank spaces before 'code lines' to make it easier to read. – tda Oct 17 '18 at 13:48