2

Here is program code, I have one json file code.json which structure is like this : [ {key:value}, {key:value} ]

When I convert list element into dictionary , it throws error

*** Settings ***
Library  JSONLibrary
Library  OperatingSystem
Library  unicodelist.py
Library  Collections

*** Test Cases ***
test json data1

    ${json_obj}=    Get file  code.json
    ${getfile}=  evaluate    json.loads('''${json_obj}''')    json
    #getfile contain json of list with dictionary
    ${obj}=  Convert To List  ${getfile}
    log to console   ${obj}
    #converted sucessfully
    log to console  " Display 1"
    #just log
    ${length}=  get length  ${obj}
    log to console  ${length}
    ${list0} =  Get From List  ${obj}  0
    log to console  ${list0}
    #list0 contain first dictionary element inside the list
    ${convert}=  Convert To Dictionary  ${list0}
    log to console  ${convert}
    # no error
    log to console  " Display 2"
    ${get_item} =  Get Dictionary Keys  ${obj}
    log to console   ${get_item}
    #error list' object has no attribute 'Keys'
    log to console  " Display 3"
    ${get_item} =  Copy Dictionary   ${obj}
    log to console   ${get_item}
    # error list' object has no attribute 'copy'
3
  • here is json file : [ { "id": 29, "name": "small", "city": "AA" }, { "id": 40, "name": "medium", "city": "BB" }, { "id": 30, "name": "Large", "city": "CC" }, { "id": 35, "portfolio_name": "Large", "city": "DD" } ] Commented Nov 28, 2017 at 1:20
  • Please don't put code or data in the comment section. edit your question and add the additional information there. Commented Nov 28, 2017 at 3:02
  • @bryan , i tried many times but it didn't worked for me so i posted here Commented Nov 28, 2017 at 5:39

1 Answer 1

2

You seem to be doing a whole lot of needless converting of data, and you're confusing the types of the objects.

Your code works if you remove all of the unnecessary conversions. It also helps if you use more descriptive variable names.

Example:

*** Settings ***
Library  OperatingSystem
Library  Collections

*** Test Cases ***
test json data1

    # ${json_data} is a string of characters
    ${json_data}=    Get file  code.json

    # ${obj_list} is a list of dictionaries
    ${obj_list}=  evaluate    json.loads('''${json_data}''')    json
    log to console  \nobj_list: ${obj_list}

    # ${obj} is the first dictionary in the list
    ${obj} =  Get From List  ${obj_list}  0
    log to console  obj: ${obj}

    # ${keys} is a list of keys
    ${keys} =  Get Dictionary Keys  ${obj}
    log to console   keys: ${keys}

    # ${new_obj} is a copy of the original obj
    ${new_obj} =  Copy Dictionary   ${obj}
    log to console   new_obj: ${new_obj}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.