Skip to content

Commit da74ca7

Browse files
author
Traitanit Huangsri
committed
add keyword to convert json to string
1 parent 7cda66b commit da74ca7

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

JSONLibrary/JSONLibraryKeywords.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,17 @@ def delete_object_from_json(self, json_object, json_path):
121121
del(match.context.value[match.path.fields[0]])
122122
return json_object
123123

124+
@keyword('Convert JSON To String')
125+
def convert_json_to_string(self, json_object):
126+
"""Convert JSON object to string
127+
128+
Arguments:
129+
- json_object: json as a dictionary object.
130+
131+
Return new json_string
132+
133+
Examples:
134+
| ${json_str}= | Convert JSON To String | ${json_obj} |
135+
"""
136+
return json.dumps(json_object)
137+

docs/JSONLibrary.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@
468468
jQuery.extend({highlight:function(e,t,n,r){if(e.nodeType===3){var i=e.data.match(t);if(i){var s=document.createElement(n||"span");s.className=r||"highlight";var o=e.splitText(i.index);o.splitText(i[0].length);var u=o.cloneNode(true);s.appendChild(u);o.parentNode.replaceChild(s,o);return 1}}else if(e.nodeType===1&&e.childNodes&&!/(script|style)/i.test(e.tagName)&&!(e.tagName===n.toUpperCase()&&e.className===r)){for(var a=0;a<e.childNodes.length;a++){a+=jQuery.highlight(e.childNodes[a],t,n,r)}}return 0}});jQuery.fn.unhighlight=function(e){var t={className:"highlight",element:"span"};jQuery.extend(t,e);return this.find(t.element+"."+t.className).each(function(){var e=this.parentNode;e.replaceChild(this.firstChild,this);e.normalize()}).end()};jQuery.fn.highlight=function(e,t){var n={className:"highlight",element:"span",caseSensitive:false,wordsOnly:false};jQuery.extend(n,t);if(e.constructor===String){e=[e]}e=jQuery.grep(e,function(e,t){return e!=""});e=jQuery.map(e,function(e,t){return e.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")});if(e.length==0){return this}var r=n.caseSensitive?"":"i";var i="("+e.join("|")+")";if(n.wordsOnly){i="\\b"+i+"\\b"}var s=new RegExp(i,r);return this.each(function(){jQuery.highlight(this,s,n.element,n.className)})}
469469
</script>
470470
<script type="text/javascript">
471-
libdoc = {"all_tags":[],"contains_tags":false,"doc":"<p>JSONLibrary is a robotframework testlibrary for manipulating JSON object (dictionary)\x3c/p>\n<p>You can get, add, update and delete your json object using JSONPath.\x3c/p>\n<h3 id=\"JSONPath Syntax\">JSONPath Syntax\x3c/h3>\n<table border=\"1\">\n<tr>\n<td>JSONPath\x3c/td>\n<td>Description\x3c/td>\n\x3c/tr>\n<tr>\n<td>$\x3c/td>\n<td>the root object/element\x3c/td>\n\x3c/tr>\n<tr>\n<td>@\x3c/td>\n<td>the current object/element\x3c/td>\n\x3c/tr>\n<tr>\n<td>. or []\x3c/td>\n<td>child operator\x3c/td>\n\x3c/tr>\n<tr>\n<td>..\x3c/td>\n<td>recursive descent. JSONPath borrows this syntax from E4X\x3c/td>\n\x3c/tr>\n<tr>\n<td>*\x3c/td>\n<td>wildcard. All objects/element regardless their names.\x3c/td>\n\x3c/tr>\n<tr>\n<td>[]\x3c/td>\n<td>subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator.\x3c/td>\n\x3c/tr>\n<tr>\n<td>[,]\x3c/td>\n<td>Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set.\x3c/td>\n\x3c/tr>\n<tr>\n<td>[start:end:step]\x3c/td>\n<td>array slice operator borrowed from ES4\x3c/td>\n\x3c/tr>\n<tr>\n<td>?()\x3c/td>\n<td>applies a filter (script) expression.\x3c/td>\n\x3c/tr>\n<tr>\n<td>()\x3c/td>\n<td>script expression, using the underlying script engine.\x3c/td>\n\x3c/tr>\n\x3c/table>\n<h3 id=\"Example Test Cases\">Example Test Cases\x3c/h3>\n<table border=\"1\">\n<tr>\n<td><b>*\x3c/b> Settings <b>*\x3c/b>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>Library\x3c/td>\n<td>JSONLibrary\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td><b>*\x3c/b> Test Cases <b>*\x3c/b>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>TestManipulatingJSON\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>${json_object}=\x3c/td>\n<td>Load JSON From File\x3c/td>\n<td>example.json\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>${object_to_add}=\x3c/td>\n<td>Create Dictionary\x3c/td>\n<td>country=Thailand\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>${json_object}=\x3c/td>\n<td>Add Object To Json\x3c/td>\n<td>${json_object}\x3c/td>\n<td>$..address\x3c/td>\n<td>${object_to_add}\x3c/td>\n\x3c/tr>\n<tr>\n<td>${value}=\x3c/td>\n<td>Get Value From Json\x3c/td>\n<td>${json_object}\x3c/td>\n<td>$..country\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>Should Be Equal As Strings\x3c/td>\n<td>${value[0]}\x3c/td>\n<td>Thailand\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n\x3c/table>","generated":"2017-03-02 09:18:52","inits":[],"keywords":[{"args":["json_object","json_path","object_to_add"],"doc":"<p>Add an dictionary or list object to json object using json_path\x3c/p>\n<p>Arguments:\x3c/p>\n<ul>\n<li>json_object: json as a dictionary object.\x3c/li>\n<li>json_path: jsonpath expression\x3c/li>\n<li>object_to_add: dictionary or list object to add to json_object which is matched by json_path\x3c/li>\n\x3c/ul>\n<p>Return new json object.\x3c/p>\n<p>Examples:\x3c/p>\n<table border=\"1\">\n<tr>\n<td>${dict}=\x3c/td>\n<td>Create Dictionary\x3c/td>\n<td>latitude=13.1234\x3c/td>\n<td>longitude=130.1234\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>${json}=\x3c/td>\n<td>Add Object To Json\x3c/td>\n<td>${json}\x3c/td>\n<td>$..address\x3c/td>\n<td>${dict}\x3c/td>\n\x3c/tr>\n\x3c/table>","matched":true,"name":"Add Object To Json","shortdoc":"Add an dictionary or list object to json object using json_path","tags":[]},{"args":["json_object","json_path"],"doc":"<p>Delete Object From JSON using json_path\x3c/p>\n<p>Arguments:\x3c/p>\n<ul>\n<li>json_object: json as a dictionary object.\x3c/li>\n<li>json_path: jsonpath expression\x3c/li>\n\x3c/ul>\n<p>Return new json_object\x3c/p>\n<p>Examples:\x3c/p>\n<table border=\"1\">\n<tr>\n<td>${json_object}=\x3c/td>\n<td>Delete Object From Json\x3c/td>\n<td>${json}\x3c/td>\n<td>$..address.streetAddress\x3c/td>\n\x3c/tr>\n\x3c/table>","matched":true,"name":"Delete Object From Json","shortdoc":"Delete Object From JSON using json_path","tags":[]},{"args":["json_object","json_path"],"doc":"<p>Get Value From JSON using JSONPath\x3c/p>\n<p>Arguments:\x3c/p>\n<ul>\n<li>json_object: json as a dictionary object.\x3c/li>\n<li>json_path: jsonpath expression\x3c/li>\n\x3c/ul>\n<p>Return array of values\x3c/p>\n<p>Examples:\x3c/p>\n<table border=\"1\">\n<tr>\n<td>${values}=\x3c/td>\n<td>Get Value From Jsonpath\x3c/td>\n<td>${json}\x3c/td>\n<td>$..phone_number\x3c/td>\n\x3c/tr>\n\x3c/table>","matched":true,"name":"Get Value From Json","shortdoc":"Get Value From JSON using JSONPath","tags":[]},{"args":["file_name"],"doc":"<p>Load JSON from file.\x3c/p>\n<p>Return json as a dictionary object.\x3c/p>\n<p>Arguments:\x3c/p>\n<ul>\n<li>file_name: absolute json file name\x3c/li>\n\x3c/ul>\n<p>Return json object (list or dictionary)\x3c/p>\n<p>Examples:\x3c/p>\n<table border=\"1\">\n<tr>\n<td>${result}=\x3c/td>\n<td>Load Json From File\x3c/td>\n<td>/path/to/file.json\x3c/td>\n\x3c/tr>\n\x3c/table>","matched":true,"name":"Load JSON From File","shortdoc":"Load JSON from file.","tags":[]},{"args":["json_object","json_path","new_value"],"doc":"<p>Update value to JSON using JSONPath\x3c/p>\n<p>Arguments:\x3c/p>\n<ul>\n<li>json_object: json as a dictionary object.\x3c/li>\n<li>json_path: jsonpath expression\x3c/li>\n<li>new_value: value to update\x3c/li>\n\x3c/ul>\n<p>Return new json_object\x3c/p>\n<p>Examples:\x3c/p>\n<table border=\"1\">\n<tr>\n<td>${json_object}=\x3c/td>\n<td>Update Value To Json\x3c/td>\n<td>${json}\x3c/td>\n<td>$..address.streetAddress\x3c/td>\n<td>Ratchadapisek Road\x3c/td>\n\x3c/tr>\n\x3c/table>","matched":true,"name":"Update Value To Json","shortdoc":"Update value to JSON using JSONPath","tags":[]}],"name":"JSONLibrary","named_args":true,"scope":"global","version":""};
471+
libdoc = {"all_tags":[],"contains_tags":false,"doc":"<p>JSONLibrary is a robotframework testlibrary for manipulating JSON object (dictionary)\x3c/p>\n<p>You can get, add, update and delete your json object using JSONPath.\x3c/p>\n<h3 id=\"JSONPath Syntax\">JSONPath Syntax\x3c/h3>\n<table border=\"1\">\n<tr>\n<td>JSONPath\x3c/td>\n<td>Description\x3c/td>\n\x3c/tr>\n<tr>\n<td>$\x3c/td>\n<td>the root object/element\x3c/td>\n\x3c/tr>\n<tr>\n<td>@\x3c/td>\n<td>the current object/element\x3c/td>\n\x3c/tr>\n<tr>\n<td>. or []\x3c/td>\n<td>child operator\x3c/td>\n\x3c/tr>\n<tr>\n<td>..\x3c/td>\n<td>recursive descent. JSONPath borrows this syntax from E4X\x3c/td>\n\x3c/tr>\n<tr>\n<td>*\x3c/td>\n<td>wildcard. All objects/element regardless their names.\x3c/td>\n\x3c/tr>\n<tr>\n<td>[]\x3c/td>\n<td>subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator.\x3c/td>\n\x3c/tr>\n<tr>\n<td>[,]\x3c/td>\n<td>Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set.\x3c/td>\n\x3c/tr>\n<tr>\n<td>[start:end:step]\x3c/td>\n<td>array slice operator borrowed from ES4\x3c/td>\n\x3c/tr>\n<tr>\n<td>?()\x3c/td>\n<td>applies a filter (script) expression.\x3c/td>\n\x3c/tr>\n<tr>\n<td>()\x3c/td>\n<td>script expression, using the underlying script engine.\x3c/td>\n\x3c/tr>\n\x3c/table>\n<h3 id=\"Example Test Cases\">Example Test Cases\x3c/h3>\n<table border=\"1\">\n<tr>\n<td><b>*\x3c/b> Settings <b>*\x3c/b>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>Library\x3c/td>\n<td>JSONLibrary\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td><b>*\x3c/b> Test Cases <b>*\x3c/b>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>TestManipulatingJSON\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>${json_object}=\x3c/td>\n<td>Load JSON From File\x3c/td>\n<td>example.json\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>${object_to_add}=\x3c/td>\n<td>Create Dictionary\x3c/td>\n<td>country=Thailand\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>${json_object}=\x3c/td>\n<td>Add Object To Json\x3c/td>\n<td>${json_object}\x3c/td>\n<td>$..address\x3c/td>\n<td>${object_to_add}\x3c/td>\n\x3c/tr>\n<tr>\n<td>${value}=\x3c/td>\n<td>Get Value From Json\x3c/td>\n<td>${json_object}\x3c/td>\n<td>$..country\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>Should Be Equal As Strings\x3c/td>\n<td>${value[0]}\x3c/td>\n<td>Thailand\x3c/td>\n<td>\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n\x3c/table>","generated":"2017-03-02 11:02:22","inits":[],"keywords":[{"args":["json_object","json_path","object_to_add"],"doc":"<p>Add an dictionary or list object to json object using json_path\x3c/p>\n<p>Arguments:\x3c/p>\n<ul>\n<li>json_object: json as a dictionary object.\x3c/li>\n<li>json_path: jsonpath expression\x3c/li>\n<li>object_to_add: dictionary or list object to add to json_object which is matched by json_path\x3c/li>\n\x3c/ul>\n<p>Return new json object.\x3c/p>\n<p>Examples:\x3c/p>\n<table border=\"1\">\n<tr>\n<td>${dict}=\x3c/td>\n<td>Create Dictionary\x3c/td>\n<td>latitude=13.1234\x3c/td>\n<td>longitude=130.1234\x3c/td>\n<td>\x3c/td>\n\x3c/tr>\n<tr>\n<td>${json}=\x3c/td>\n<td>Add Object To Json\x3c/td>\n<td>${json}\x3c/td>\n<td>$..address\x3c/td>\n<td>${dict}\x3c/td>\n\x3c/tr>\n\x3c/table>","matched":true,"name":"Add Object To Json","shortdoc":"Add an dictionary or list object to json object using json_path","tags":[]},{"args":["json_object"],"doc":"<p>Convert JSON object to string\x3c/p>\n<p>Arguments:\x3c/p>\n<ul>\n<li>json_object: json as a dictionary object.\x3c/li>\n\x3c/ul>\n<p>Return new json_string\x3c/p>\n<p>Examples:\x3c/p>\n<table border=\"1\">\n<tr>\n<td>${json_str}=\x3c/td>\n<td>Convert JSON To String\x3c/td>\n<td>${json_obj}\x3c/td>\n\x3c/tr>\n\x3c/table>","matched":true,"name":"Convert JSON To String","shortdoc":"Convert JSON object to string","tags":[]},{"args":["json_object","json_path"],"doc":"<p>Delete Object From JSON using json_path\x3c/p>\n<p>Arguments:\x3c/p>\n<ul>\n<li>json_object: json as a dictionary object.\x3c/li>\n<li>json_path: jsonpath expression\x3c/li>\n\x3c/ul>\n<p>Return new json_object\x3c/p>\n<p>Examples:\x3c/p>\n<table border=\"1\">\n<tr>\n<td>${json_object}=\x3c/td>\n<td>Delete Object From Json\x3c/td>\n<td>${json}\x3c/td>\n<td>$..address.streetAddress\x3c/td>\n\x3c/tr>\n\x3c/table>","matched":true,"name":"Delete Object From Json","shortdoc":"Delete Object From JSON using json_path","tags":[]},{"args":["json_object","json_path"],"doc":"<p>Get Value From JSON using JSONPath\x3c/p>\n<p>Arguments:\x3c/p>\n<ul>\n<li>json_object: json as a dictionary object.\x3c/li>\n<li>json_path: jsonpath expression\x3c/li>\n\x3c/ul>\n<p>Return array of values\x3c/p>\n<p>Examples:\x3c/p>\n<table border=\"1\">\n<tr>\n<td>${values}=\x3c/td>\n<td>Get Value From Jsonpath\x3c/td>\n<td>${json}\x3c/td>\n<td>$..phone_number\x3c/td>\n\x3c/tr>\n\x3c/table>","matched":true,"name":"Get Value From Json","shortdoc":"Get Value From JSON using JSONPath","tags":[]},{"args":["file_name"],"doc":"<p>Load JSON from file.\x3c/p>\n<p>Return json as a dictionary object.\x3c/p>\n<p>Arguments:\x3c/p>\n<ul>\n<li>file_name: absolute json file name\x3c/li>\n\x3c/ul>\n<p>Return json object (list or dictionary)\x3c/p>\n<p>Examples:\x3c/p>\n<table border=\"1\">\n<tr>\n<td>${result}=\x3c/td>\n<td>Load Json From File\x3c/td>\n<td>/path/to/file.json\x3c/td>\n\x3c/tr>\n\x3c/table>","matched":true,"name":"Load JSON From File","shortdoc":"Load JSON from file.","tags":[]},{"args":["json_object","json_path","new_value"],"doc":"<p>Update value to JSON using JSONPath\x3c/p>\n<p>Arguments:\x3c/p>\n<ul>\n<li>json_object: json as a dictionary object.\x3c/li>\n<li>json_path: jsonpath expression\x3c/li>\n<li>new_value: value to update\x3c/li>\n\x3c/ul>\n<p>Return new json_object\x3c/p>\n<p>Examples:\x3c/p>\n<table border=\"1\">\n<tr>\n<td>${json_object}=\x3c/td>\n<td>Update Value To Json\x3c/td>\n<td>${json}\x3c/td>\n<td>$..address.streetAddress\x3c/td>\n<td>Ratchadapisek Road\x3c/td>\n\x3c/tr>\n\x3c/table>","matched":true,"name":"Update Value To Json","shortdoc":"Update value to JSON using JSONPath","tags":[]}],"name":"JSONLibrary","named_args":true,"scope":"global","version":""};
472472
</script>
473473
<title></title>
474474
</head>

tests/test_JSONLibrary.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_add_object_to_json(self):
2020
json_path = '$..address'
2121
data_to_add = {'latitude': '13.1234', 'longitude': '130.1234'}
2222
json_object = self.test.add_object_to_json(self.json, json_path, data_to_add)
23-
self.assertTrue(set(data_to_add.items()).issubset(set(json_object['address'].items())))
23+
self.assertDictContainsSubset(data_to_add, json_object['address'])
2424

2525
def test_add_new_object_to_root(self):
2626
json_path = '$'
@@ -63,5 +63,9 @@ def test_delete_object_from_json(self):
6363
json_object = self.test.delete_object_from_json(self.json, json_path)
6464
self.assertFalse('isMarried' in json_object)
6565

66+
def test_convert_json_to_string(self):
67+
json_str = self.test.convert_json_to_string(self.json)
68+
self.assertTrue(isinstance(json_str, str))
69+
6670
def tearDown(self):
6771
pass

0 commit comments

Comments
 (0)