Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json handling in ROBOT

I have a Json file in which there is a field which I need to edit and save the file for next usage.

But the field which I need to edit is as shown below,

The value I need to assign fr the field is generated Randomly in run time which i'll be capturing in a variable and pass it to this json specific key "dp" then save the json.

The saved json will be used for REST POST url.

            {
                "p": "10",
                "v": 100,
                "vt": [
                    {
                        "dp": "Field to be edited"(integer value) , 
                  ]          
            }
like image 891
Vidya S Avatar asked Feb 08 '16 04:02

Vidya S


People also ask

What is JSON in machine learning?

JSON stands for JavaScript Object Notation. JSON is a lightweight data format used for data interchange between multiple different languages. It is easy to read for humans and easily parsed by machines.

What is JSON automation?

In short, JSON stands for JavaScript Object Notation, and is a concise hierarchical data serialization syntax that is supported by all modern browsers. Its format makes it a lightweight way of representing objects, while remaining human-readable. For this reason it has replaced XML notation on many platforms.

What is JSON manipulation?

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. Extension is . json , and MIME type is application/json . It's broadly used in data transaction between application and servers. JS only has one data type which is Object.

What is JSON parsing example?

JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the best alternative for XML. This chapter explains how to parse the JSON file and extract necessary information from it. Android provides four different classes to manipulate JSON data.


Video Answer


2 Answers

The simplest solution would be to write a python keyword that can change the value for you. However, you can solve this with robot keywords by performing the following steps:

  1. convert the JSON string to a dictionary
  2. modify the dictionary
  3. convert the dictionary back to a JSON string

Convert the JSON string to a dictionary

Python has a module (json) for working with JSON data. You can use the evaluate keyword to convert your JSON string to a python dictionary using the loads (load string) method of that module.

Assuming your JSON data is in a robot variable named ${json_string}, you can convert it to a python dictionary like this:

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

With the above, ${json} now holds a reference to a dictionary that contains all of the json data.

Modify the dictionary

The Collections library that comes with robot has a keyword named set to dictionary which can be used to set the value of a dictionary element. In this case, you need to change the value of a dictionary nested inside the vt element of the JSON object. We can reference that nested dictionary using robot's extended variable syntax.

For example:

set to dictionary    ${json["vt"]}    dp=the new value

With that, ${json} now has the new value. However, it is still a python dictionary rather than JSON data, so there's one more step.

Convert the dictionary back to JSON

Converting the dictionary back to JSON is the reverse of the first step. Namely, use the dumps (dump string) method of the json module:

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

With that, ${json_string} will contain a valid JSON string with the modified data.


Complete example

The following is a complete working example. The JSON string will be printed before and after the substitution of the new value:

*** Settings ***
Library    Collections

*** Test Cases ***
Example
    ${json_string}=    catenate
    ...  {
    ...    "p": "10",
    ...    "v": 100,
    ...    "vt": {
    ...            "dp": "Field to be edited"
    ...          }
    ...  }

    log to console       \nOriginal JSON:\n${json_string}
    ${json}=             evaluate        json.loads('''${json_string}''')    json
    set to dictionary    ${json["vt"]}    dp=the new value
    ${json_string}=      evaluate        json.dumps(${json})                 json
    log to console       \nNew JSON string:\n${json_string}
like image 127
Bryan Oakley Avatar answered Oct 27 '22 17:10

Bryan Oakley


For reading and writing data to and from file I am using OperatingSystem library

${json} Get Binary File ${json_path}nameOfJsonFile.json

It works for me on API testing, to read .json and POST, like here

*** Settings ***
Library    Collections
Library    ExtendedRequestsLibrary 
Library    OperatingSystem
*** Variables ***  
${uri}    https://blabla.com/service/
${json_path}    C:/home/user/project/src/json/
*** Test Cases ***
Robot Test Case  
   Create Session    alias    ${uri}    
   &{headers}  Create Dictionary  Content-Type=application/json; charset=utf-8
   ${json}  Get Binary File  ${json_path}nameOfJsonFile.json
   ${resp}    Post Request    alias    data=${json}    headers=${headers}
   Should Be Equal As Strings    ${resp.status_code}    200
like image 26
idomoni Avatar answered Oct 27 '22 18:10

idomoni