1
votes

I use robot framework at following environment.

  • Python 2.7.6
  • robotframework 2.8.7
  • Ubuntu 14.04.3 LTS

I want to create json that include list. But Following script,Json.dumps interpret part as string.

*** Settings ***
Library    Collections
Library    json
Library    String

*** Variables ***
${NAME}    XXXX
${ID}    YYYY
${PART_1}    ZZZ1
${PART_2}    ZZZ2

*** Test Cases ***
Test Create Json
    ${req_dict}    Create Dictionary    name=${NAME}    id=${ID}    part=[${PART_1},${PART_2}]
    Log To Console    *** Test Create Json Result ***
    Log To Console    ${req_json}

*** Keywords ***
Prepare Json Keyword
    [Arguments]    @{args}
    ${req_dict}    Create Dictionary
    :FOR     ${pair}    IN     @{args}
    \     ${key}    ${value}=     Split String    ${pair}    =
    \     Set To Dictionary    ${req_dict}    ${key}=${value}
    ${req_json}    Json.Dumps    ${req_dict}
    [Return]    ${req_json}

This case,Robot shows

{"part": "[ZZZ1,ZZZ2]", "name": "XXXX", "id": "YYYY"}

But I want to create json like following.

{"part": ["ZZZ1","ZZZ2"], "name": "XXXX", "id": "YYYY"}

To interpret list, how can I change it ?

1
is there a reason you're creating the json from a dictionary, rather than just hard-coding the actual json string? - Bryan Oakley

1 Answers

1
votes

Didn't you want to achieve this?:

*** Settings ***
Library    json

*** Variables ***
${NAME}    XXXX
${ID}    YYYY
${PART_1}    ZZZ1
${PART_2}    ZZZ2
@{parts}    ${PART_1}    ${PART_2}

*** Test Cases ***
Test Create Json
    ${req_dict}    Create Dictionary    name=${NAME}    id=${ID}    part=@{parts}
    Log To Console    \n*** DICTIONARY ***
    Log To Console    ${req_dict}
    ${req_json}    Json.Dumps    ${req_dict}
    Log To Console    *** JSON ***
    Log To Console    ${req_json}