How to add new object in Json through robotframework

5,515 views
Skip to first unread message

rahul sharma

unread,
Oct 5, 2017, 11:15:05 PM10/5/17
to robotframework-users
Hi,

Can someone help me in adding a new object in json 

rajesh kumar

unread,
Oct 6, 2017, 12:26:11 AM10/6/17
to rah.sh...@gmail.com, robotframework-users
Hi Rahul,

Use Collections Library and Evaluate ( Evaluates the given expression in Python and returns the results)

Below is the example:

*** Settings ***
Library    Collections

*** Test Cases ***
Test JSon
    ${json_object}=    catenate
    ...  {
    ...    "r": 20,
    ...    "a": 100,
    ...    "je": {
    ...            "test": "some test"
    ...          }
    ...  }

    log to console       Original JSON:${json_object}
    ${json}=             evaluate        json.loads('''${json_object}''')   json
    set to dictionary    ${json["je"]}    test=the new value
    set to dictionary    ${json}    dp1=the new value
    ${json_new}=      evaluate        json.dumps(${json})                 json
    log to console       \nNew JSON string:\n${json_new}


I hope this will help ..

Thanks
-Rajesh-


On Fri, Oct 6, 2017 at 11:15 AM, rahul sharma <rah.sh...@gmail.com> wrote:
Hi,

Can someone help me in adding a new object in json 

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
To post to this group, send email to robotframework-users@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

rahul sharma

unread,
Oct 6, 2017, 1:11:30 AM10/6/17
to robotframework-users
Hi Rakesh,

Thanks for the quick reply,however i want to add a new object element,i dont want to change value of exiting object,for example in below json

Current json
{
    ...    "r": 20,
    ...    "a": 100,
    ...    "je": {
    ...            "test": "some test"
    ...          }
    ...  }

New output which i need is like below

{
    ...    "r": 20,
    ...    "a": 100,
    ...    "je": {
    ...            "test": "some test"
                    "new":""
    ...          }
    ...  }

To post to this group, send email to robotframe...@googlegroups.com.

rajesh kumar

unread,
Oct 6, 2017, 1:24:38 AM10/6/17
to rah.sh...@gmail.com, robotframework-users
Hi Rahul,

Use this : set to dictionary    ${json["je"]}    new=the new value
It will add to as value to "je"

Also above example I provided for adding new(dp1) key:values.
Please run and see the results.



For ur question this is the solution : set to dictionary    ${json["je"]}    new=the new value
Add to above code and excute.

Thanks
-Rajesh-





To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsubscrib...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
To post to this group, send email to robotframework-users@googlegroups.com.

Pekka Klärck

unread,
Oct 6, 2017, 4:27:18 AM10/6/17
to rajesh kumar, rahul sharma, robotframework-users
Hello,

As presented below by Rajesh, you can use `Evaluate` keyword to
execute Python's `json.loads` and `json.dumps`. That's a bit hacky,
though, and it would be a lot better to have a generic keywords for
handling JSON (and possibly also YAML). We could implement a new
library for that, but it probably wouldn't get any other keywords than
`Load JSON` and `Dump JSON`, because we already have Collections
library that can be used for verifying and manipulating the actual
data. Another solution would be adding these keywords directly to the
Collections library itself. Should we do it?

Cheers,
.peke
>> email to robotframework-u...@googlegroups.com.
>> To post to this group, send email to
>> robotframe...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/robotframework-users.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "robotframework-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to robotframework-u...@googlegroups.com.
> To post to this group, send email to robotframe...@googlegroups.com.
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

Hélio Guilherme

unread,
Oct 6, 2017, 4:36:51 AM10/6/17
to robotframework-users


On Fri, Oct 6, 2017 at 9:27 AM, Pekka Klärck <pe...@iki.fi> wrote:
Hello,

... 
 Another solution would be adding these keywords directly to the
Collections library itself. Should we do it?

Cheers,
    .peke

Yes, we should.

There has been a lot of questions related to JSON manipulation. I think it would be helpful to have tools for it.

Thanks,

Hélio
 

Jerry Schneider

unread,
Oct 6, 2017, 9:44:49 AM10/6/17
to robotframe...@googlegroups.com
What about adding them to the Robotframework-Requests library?  It already has ability to "pretty print" JSON.

jer
--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

-- 
http://www.linkedin.com/in/jerryschneider
Linux registered user #475536
Ubuntu registered user #28583

Александр Илюшкин

unread,
Oct 8, 2017, 3:57:25 PM10/8/17
to robotframework-users
I agree with Pekka.
Working with json in python keyword library allows you to  do everything you wan with json object.
Experience shows that it is much better to leave complex work in python insteard of doing everything on Robot Framework side.

So, separate your low level logic from top-level test-data.

пятница, 6 октября 2017 г., 6:15:05 UTC+3 пользователь rahul sharma написал:

Александр Илюшкин

unread,
Oct 8, 2017, 3:57:25 PM10/8/17
to robotframework-users
But that is probably not so bad idea to add new keywords in collections library, seems like ok.


пятница, 6 октября 2017 г., 6:15:05 UTC+3 пользователь rahul sharma написал:
Hi,

Wlad

unread,
Oct 9, 2017, 5:24:41 PM10/9/17
to ailju...@gmail.com, robotframework-users
Dont forget the robotframework-jsonlibrary which Uses JSONPath to navigate and manipulate JSON data. It makes handling json really easy even directly in RF. Look for Link in our slack #requests channel or just google it. 

Cheers
Tset

--

rahul sharma

unread,
Oct 10, 2017, 5:08:24 AM10/10/17
to robotframework-users
Hi Rajesh ,
Thanks for your prompt reply

I have below json 

[{
  "header": {
    "messageId": "bf06ed84-c15b-46c2-a751-b9387347d511",
    "correlationId": "",
    "causationId": "",
    "requestTime": "2017-09-20T05.00.00Z",
    "priority": null,
    "issuerId": null,
    "domain": null,
    "payloadType": "EventLogResponse",
    "payloadTypeVersion": "1.0",
    "networkDetails": {
      "deviceTime": "2017-09-20T05.00.00Z",
      "gatewayId": "123"
    }
  },
  "somePacket":{
  "deviceId": "FEED0020",
  "networkDeviceId": "3736015433",
  "id": "1.1.9.10",
  "time": {
    "timeStamp": "2017-09-20T05.00.00Z",
    "timeOffset": -300,
    "dstInEffect": false
  },
  "events": [
    {
      "time": {
        "timeStamp": "2017-09-20T05.00.00Z",
        "timeOffset": -300,
        "dstInEffect": false
      },
      "eventId": "1.3.20.5.1",
      "priority": "logOnly",
      "parameters": {
        "address": "10.9.08.7",
        "collectorIdentifier": "123"
      }
    }
  ]
}
}]



I want to add  "collectorIdentifier1": "567"  in "parameters" tags , unfortunately  i am not able to add this, there seems to be issue with xpath  
Reply all
Reply to author
Forward
0 new messages