0

Suppose the below is my response body

body={
  "message": {
    "Code": 26181,
    "rollnos": [
      {
        "ID": 1439173,
        "date_input": "2022-01-31 14:09:30.206748",
      }
    ],
    "start_date": "2022-01-31 00:00:00",
  }
}

I want to set the ID value in a variable. I have tried the below but it did not work out.

  ${json_response}=  set variable    ${xyz.json()}
  
  ${temp}=  Set Variable    ${json_response['message']}
  
  ${value_1}=  Set Variable    ${temp['rollnos']}
  
  ${register_id} =  Set Variable    ${value_1["ID"]

Where have I gone wrong in this?

3 Answers 3

1

If you have body already as a dictionary you can use only the last 4 lines from the test example. I have included also the transformation from string to dictionary in case you need it:

*** Settings ***
Library  Collections

Resource          robot/resources/environment_resources.robot

*** Variables ***
${body_temp}    {"message": {"Code": "26181", "rollnos": [{"ID": "1439173", "date_input": "2022-01-31 14:09:30.206748"}],"start_date": "2022-01-31 00:00:00"}}

*** Keywords ***
Converting a JSON File
    ${body}    evaluate  json.loads($body_temp)    json
    [Return]  ${body}

*** Test Cases ***
Example
    # Get body as dict
    ${body}=  converting a json file

    # Get the ID
    ${message} =    Get From Dictionary     ${body}    message
    ${rollnos} =    Get From Dictionary     ${message}    rollnos

    ${id} =   Get From Dictionary     ${rollnos}[0]    ID

    # Log the ID
    Log To Console    ID:${id}
Sign up to request clarification or add additional context in comments.

Comments

1

Where have I gone wrong in this?

You are treating ${temp['rollnos']} as a dictionary, but it is a list of dictionaries.

Instead of this:

${value_1}=  Set Variable    ${temp['rollnos']}

${register_id} =  Set Variable    ${value_1["ID"]

Do this, to get the ID of the first item in the dictionary:

${value_1}=  Set Variable    ${temp['rollnos'][0]}
#                                             ^^^
${register_id} =  Set Variable    ${value_1["ID"]}

Comments

-1

Well, when you say:

but it did not work out.

You don't mention what were the error messages.

This is my interpretation of your code:

  ${json_response}=  set variable    ${xyz.json()}
  # ${json_response} is now a dictionary with content: body=&{message}
  # So the next step would be: 
  ${temp}=  Set Variable    ${json_response['body']['message']}
  # And now we have: &{temp} = { Code:26181, &{rollnos}, ...
  # The next code will be correct
  ${value_1}=  Set Variable    ${temp['rollnos']}
  
  ${register_id} =  Set Variable    ${value_1['ID']

But you got the value of ID, and you wanted to Set it, so that is another question.

2 Comments

This looks like it has the same mistake as the OP: you're treating the 'rollnos' value as a dictionary but it's a list of dictionaries.
Yes, you are right. Thanks.

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.