How to represent nested json objects in a cucumber feature file - json

I have a need to represent JSON object in the feature file. I could use a json file for this for the code to pick up. But this would mean that i cant pass values from the feature file.
Scenario: Test
Given a condition is met
Then the following json response is sent
| json |
| {"dddd":"dddd","ggggg":"ggggg"}|
Above works for a normal json. However if there are nested objects etc then writing the json in a single line like above would make the feature very difficult to read and difficult to fix.
Please let me know.

You can use a string to do that, it makes the json much more legible.
Then the following json response is sent
"""
{
'dddd': 'dddd',
'ggggg': 'ggggg',
'somethingelse': {
'thing': 'thingvalue',
'thing2': 'thing2value'
}
}
"""
In the code, you can use it directly:
Then(/^the following json response is sent$/) do |message|
expect(rest_stub.body).to eq(message)
end
or something like that.

Related

Prevent parsing a JSON node with common-lisp YASON library

I am using the Yason library in common-lisp, I want to parse a json string but would like the parser to keep one a its node unparsed.
Typically with an example like that:
{
"metadata1" : "mydata1",
"metadata2" : "mydata2",
"payload" : {...my long payload object},
"otherNodesToParse" : {...}
}
How can I set the yason parser to parse my json but skip the payload node and keep it as a string in the json format.
Use: let's say I just want the envelope data (everything that's not the payload), and to forward the payload as-is (as json string) to another system.
If I parse the whole json (so including payload) and then re-encode the payload to json, it is inefficient. The payload size could also be pretty big.
How do you know where the end of the payload object is in the stream? You do so by parsing the stream: if you don't parse the stream you simply can't know where the end of the object is: that's the nature of JSON's syntax (as it is the nature of CL's default syntax). For instance the only way you can know the difference between where to continue after
{x:1}
and after
{x:1.2}
is by parsing the two things.
So you must necessarily parse the whole thing.
So the answer to your question is: you can't do this.
You could (but not, I think, with YASON) decide that you did not want to build an object as a result of the parse. And perhaps, if the stream you are parsing corresponds to something with random access like a string or a file, you could note the start and end positions in the stream to later extract a string from it corresponding to the unparsed data (or you could perhaps build it up as you go).
It looks as if some or all of this might be possible with CL-JSON, but you'd have to work at it.
Unless the objects you are reading are vast the benefit of this seems questionable-to-none. If you really do want to do something like this efficiently you need a serialisation scheme which tells you how long things are.

How to use csv to provide different values for json assertion for different inputs in Jmeter?

I am relatively new to API Testing.
Currently I have a setup where I am having different request jsons and for each request json there is different repsonse json assertion configuration provided with the appropriate values in Jmeter.
Since my json structure will be same and only the input values will be different, I am thinking to generalize the input values using csv and keep only one request configuration.
But would I be able to provide different values(like jsonpath and expected value) to one response json assertion configuration using csv? Because the jsonpath and expected value both will depend upon the input provided and it can have major difference as per the case.
If yes, then how to do it please let me know.
Also if I can achieve my use case using other free API Testing Tool like Postman then also let me know.
You can normally parameterize any JMeter Test Element using CSV Data Set Config
For example you have the following response:
{ "name":"John" }
And the following CSV file:
$.name,John
$.name,Jane
Add CSV Data Set Config to your Test Plan and configure it like:
Add JSON Assertion as a child of the request which returns the above JSON and configure it like:
That's it, each virtual user and/or iteration will pick up the next line from the CSV file and the ${jsonPath} and ${name} placeholders will be replaced with their respective values:
as you can see, the first request passed because name matched John and the second failed because the assertion expected name to be Jane and got John

Get value from JSON object in Prolog

I have two questions.
How can I access the values in a JSON data structure in Prolog?
How can I yield a list of solutions from a predicate as JSON?
My Code:
handle(Request) :-
format(user_output,"I'm here~n",[]),
http_read_json(Request, DictIn,[json_object(term)]),
%beat(DictIn.name,DictIn.move,X),
%get list of solution of beat in to JSON and keep it in DictOut
reply_json(DictOut).
I assume you are using SWI Prolog. Your code suggests that you want to convert JSON to SWI Prolog dicts. So you need to change
http_read_json(Request, DictIn, [json_object(term)])
to
http_read_json(Request, DictIn, [json_object(dict)]),
or you can just use http_read_json_dict/2. Note that Request must be a PUT or POST request or else these predicates will throw a domain_error(Type, Term).
You can print out DictIn to take a look at what fields you want to extract.
For the response use reply_json_dict/{1, 2}.

how to display json data got from json2 format

I get json data from Struts Action, like below:
I want to precoss this data in JSP page, but I tried to use .$each or attr, all does not woek, I use Json2.js JSON.stringfy() get these data, so how I can fet each key and value in it?
[{"agreementNumber":"161446628","employeeIndicator":"N","enrollmentSrc":"363","fepIndicator":"N","groupCancelDate":null,"groupCancelDateTime":0,"groupCnlDate":"","groupEffDate":"20070701","groupEffectiveDate":{"date":1,"day":0,"hours":0,"minutes":0,"month":6,"seconds":0,"time":1183262400000,"timezoneOffset":240,"year":107},"groupEffectiveDateTime":1183262400000,"groupName":"Westminster College","groupNumber":"01501701 ","index":"1","memberList":{"enrollmetSrc":"363","groupNumber":"01501701 ","memberList":[{"agreementNumber":"161446628","birthDate":{"date":10,"day":0,"hours":0,"minutes":0,"month":1,"seconds":0,"time":-217450800000,"timezoneOffset":300,"year":63},"birthDateTime":-217450800000,"cancelDate":null,"cancelDateTime":0,"classCode":" I3","effectiveDate":{"date":1,"day":0,"hours":0,"minutes":0,"month":6,"seconds":0,"time":1183262400000,"timezoneOffset":240,"year":107},"effectiveDateTime":1183262400000,"firstName":"KENNETH ","gender":"M","groupName":"","groupNumber":"01501701 ","lastName":"ROMIG ","medicareAdvantage":"","memberId":375315,"middleName":"J ","pin":"1","preTtlName":" ","relation":"Self","relationCode":"1","sucTtlName":" "},{"agreementNumber":"161446628","birthDate":{"date":23,"day":5,"hours":0,"minutes":0,"month":7,"seconds":0,"time":-200692800000,"timezoneOffset":240,"year":63},"birthDateTime":-200692800000,"cancelDate":null,"cancelDateTime":0,"classCode":" I3","effectiveDate":{"date":1,"day":0,"hours":0,"minutes":0,"month":6,"seconds":0,"time":1183262400000,"timezoneOffset":240,"year":107},"effectiveDateTime":1183262400000,"firstName":"KIMBERLY ","gender":"F","groupName":"","groupNumber":"01501701 ","lastName":"ROMIG ","medicareAdvantage":"","memberId":1424959,"middleName":"G ","pin":"3","preTtlName":" ","relation":"Spouse","relationCode":"2","sucTtlName":" "},{"agreementNumber":"161446628","birthDate":{"date":8,"day":1,"hours":0,"minutes":0,"month":0,"seconds":0,"time":631774800000,"timezoneOffset":300,"year":90},"birthDateTime":631774800000,"cancelDate":null,"cancelDateTime":0,"classCode":" I3","effectiveDate":{"date":1,"day":0,"hours":0,"minutes":0,"month":6,"seconds":0,"time":1183262400000,"timezoneOffset":240,"year":107},"effectiveDateTime":1183262400000,"firstName":"NICOLE ","gender":"F","groupName":"","groupNumber":"01501701 ","lastName":"CRUMBACHER ","medicareAdvantage":"","memberId":375314,"middleName":"A ","pin":"4","preTtlName":" ","relation":"Child","relationCode":"3","sucTtlName":" "},{"agreementNumber":"161446628","birthDate":{"date":7,"day":6,"hours":0,"minutes":0,"month":6,"seconds":0,"time":994478400000,"timezoneOffset":240,"year":101},"birthDateTime":994478400000,"cancelDate":null,"cancelDateTime":0,"classCode":" I3","effectiveDate":{"date":1,"day":0,"hours":0,"minutes":0,"month":6,"seconds":0,"time":1183262400000,"timezoneOffset":240,"year":107},"effectiveDateTime":1183262400000,"firstName":"NATHAN ","gender":"M","groupName":"","groupNumber":"01501701 ","lastName":"ROMIG ","medicareAdvantage":"","memberId":1424960,"middleName":"J ","pin":"6","preTtlName":" ","relation":"Child","relationCode":"3","sucTtlName":" "}]},"ownerCode":"HM"}]
Your json object has a complex structure so I am not going to write the function how to map it properly but I am going to give you some tools how to understand it better and work with it.
To see your json object more clearly use a an online json pareser http://json.parser.online.fr/ just paste your json object there and in the right you will see the tree like structure.
Here you can see examples how to access you json object properties jsfidle
I know that this is not what you want exactly but will help you build it.

Confused with JSON data and normal data in Django ajax request

I read about JSON from internet but still i have not got the grasp of it. I am reading this article
http://webcloud.se/log/AJAX-in-Django-with-jQuery/
I could not understood the first part where the function is using JSON
def xhr_test(request, format):
if request.is_ajax():
if format == 'xml':
mimetype = 'application/xml'
if format == 'json':
mimetype = 'application/javascript'
data = serializers.serialize(format, ExampleModel.objects.all())
return HttpResponse(data,mimetype)
# If you want to prevent non XHR calls
else:
return HttpResponse(status=400)
My Main Problems are
From where the function is getting format variable
Does format is json mean that data given to function is json or data which will be recived is json
Can anyone give me simple example that what will be the ouput of this function
data = serializers.serialize(format, ExampleModel.objects.all())
How will I use that data when i get that response in jquery function
If i don't use JSON in above function then how will the input and response back will chnage
Thanks
From where the function is getting format variable
In practice, there are lots of ways this format could be populated. HTTP provides an Accept: header that requests can use to indicate the preferred Content-Type for the response. On the client, you might use xhr.setRequestHeader('accept', 'application/json') to tell the server that you want your response in json format. In practice, though, very few frameworks actually do this. This being django, arguments to view functions are usually set in the urlconf, you might craft a urlconf like this:
urlpatterns = patterns('',
# ...
(r'^xhr_test.(?<format>.*)$', 'path.to.xhr_test'),
)
2 . Does format is json mean that data given to function is json or data which will be recived is json
This particular view doesn't do anything at all with the request body, and is certainly providing a response body in the supplied format
4 . How will I use that data when i get that response in jquery function
Depending on how complicated your request needs to be, you can use jQuery.getJSON, which will pass your callback with regular JavaScript objects that result from parsing the JSON. If you need to do a bit more work to get the request right, you can use jQuery.parseJSON to process the json data, and that will return the same JavaScript objects.
From the urlconf, just like it says in the article right below it.
The query set will be serialized as JSON.
It will be the query set represented as either XML or JSON. python manage.py shell is your friend.
You will decode it, then iterate over it.
You'll need to find some other format to serialize it in instead.