restassured - parsing response and searching by value - rest-assured

I have following REST API response:
"items":
[
{
"empid": "1234",
"name": "Santosh",
"hiredby": "Mark",
"date": "2017-01-31,00:19:41 PST",
},
{
"empid": "5678",
"name": "Kumar",
"hiredby": "Bob",
"date": "2017-01-31,08:30:31 PST"
}
]
My query is : - How do i get empid based on querying name as Kumar.
For example: I need to find "Kumar" name and get his empid. (that is, search by name and get his empid as response) I'm able to get the response and store it in Response object. but, from response object how can i traverse and query to get the required value.
Also,
I tried by retrieving as:
String name = get(REST_ENDPOINT).then().body("items.name",hasItems("Kumar")).extract().path("items.empid").toString();
when i print the response i get collection of the empid like [1234,5678], where as my expectation is to get only 5678.
Do I need to parse via JSONArray and JSONObject and iterate the response?
Please suggest.

You can use something like this
response1.jsonPath().getList("collect { it.credentials.findAll { it.credentialType == 'Ban User Name'}.credentialId }.flatten()")

Related

SwiftyJson get value from array in dictionary

I've been trying to retrieve the value of code for a while now. This is the json I'm getting back from the server:
{
"message": "The given data was invalid.",
"errors": {
"code": [
"The code has already been taken."
]
}
}
How do I get the value of "code" with swiftyJson?
I've already tried this but I don't think it's correct.
let list: Dictionary<String, JSON> = json["errors"].dictionaryValue
var retunValue = list.values.first
You can access the first element of the "code" array like this:
json["errors"]["code"].array?.first?.string
This will give you nil if code were an empty array, or the JSON isn't the expected structure.
Since "code" is an array, it might contain more than 1 string, so you might want a [String] instead:
json["errors"]["code"].array?.compactMap { $0.string } ?? []
This will give you an empty array if the JSON is not the expected structure.

Quicktype option parameter handling

I have an optional parameter in my web-service response which can or can not return null. What will be the best what to manage those?
As if I pass null in creator lets say for this structure:
{
"email": "test#test.com",
"profilePicture": null,
"firstTime": true,
"preference1": {
"$id": "26",
"$values": []
},
"Preference2": {
"$id": "27",
"$values": []
}
}
Now profilePicture is set to JSONNull and next time when I actually get profile URL it will not parse that and my response data to LNUser is nil. If I set this variable to String and null received response get set to nil.
You need
let profilePicture:URL?
quicktype needs an exhaustive set of examples, so make sure one of your samples includes a non-null value. Like this: https://app.quicktype.io?share=ldXiDP9cIKB1Q7CPJ7Id

Google Firebase Cloud Messaging HTTP Protocol Payload Data json key

I have problem with sending some key in data Payload to iOS device
Here's my json to send https://fcm.googleapis.com/fcm/send -> #POST
{
"time_to_live": 216000,
"registration_ids": [
"device token"
],
"content-available" : true,
"notification": {
"body": "Some msg",
"title": "title"
},
"data": {
"code":11,
"orderStatus": {
"status": 1,
"id": 5102
}
}
}
Headers in POST request:
Authorization=key=myFirebaseKey
Content-Type=application/json
Problem with key orderStatus, in client side I get this key as a String
orderStatus = "{\"id\":5102,\"status\":1}";
Can I tell Firebase to send this key as an Object?
Or all keys in data must be simple key=value keys?
I mean value only string integer etc...?
The documentation for downstream JSON message syntax says this about the key-value pairs in the data object:
The key should not be a reserved word ("from" or any word starting
with "google" or "gcm"). Do not use any of the words defined in this
table (such as collapse_key).
Values in string types are recommended. You have to convert values in
objects or other non-string data types (e.g., integers or booleans) to
string.

RestKit JSON Mapping

I have given JSON and cannot parse partial data. It seems dictionary into dictionary:
{
"products": [
{
"id": 6796,
"title": "my title",
"description": "desc",
"code": "12345",
"valueType": "null",
"discounts": [
{
"minPrice": null,
"maxPrice": null,
"value": 20,
"avail": false
}
]
}
]
}
I am using the latest version of RESTKit but I cannot properly parse under discounts.
my RestKit settings are:
responseMapping.addAttributeMappingsFromDictionary([
"id" : "id",
"code" : "code",
"title" : "title",
"valueType" : "valueType",
"description" : "desc",
"discounts.minPrice" : "minPrice",
"discounts.maxPrice" : "maxPrice",
"discounts.value" : "value",
"discounts.avail" : "avail",
])
but all values below discounts always return Nil. What I am doing wrong?
You can't directly map using discounts.XXX because discounts is an array and you have no way to index into that array and extract a single value.
You either need to change the source JSON to compress the values out of the dictionary, or create a custom object that you can map each item in the discounts array to.
Technically you could map the whole discounts array, which would give you an array of dictionaries, that you could then unpack in the setter method, but the array of custom objects is usually a better approach.

prolems in converting from json formatted String to json object

I have a web servlet that returns a json which is stored in my bb mobile app as string in json format....
Now I want to parse the string to extract values from it...
using
JSONObject jsobject = new JSONObject(jsonString);
returns an error:
json must start with {
My JSON generated is in this format
[
{"LASTNAME":"akre","FIRSTNAME":"swapnil"},
{"LASTNAME":"akre","FIRSTNAME":"swapnil"},
{"LASTNAME":"akre","FIRSTNAME":"swapnil"}
]
which is corect format as verified by jsonlint.com....
yes, json must start with { and end with } , what you can do is you put your json array in
{}, so it will be parsed correctly. JsonLint parses the partial json as well thats why it is showing it as correct. You can try like following
yes, json must start with { and end with } , what you can do is you put your json array in {}, so it will be parsed correctly as jsonobject. JsonLint parses the partial json as well thats why it is showing it as correct
{
"data": [
{
"LASTNAME": "akre",
"FIRSTNAME": "swapnil"
},
{
"LASTNAME": "akre",
"FIRSTNAME": "swapnil"
},
{
"LASTNAME": "akre",
"FIRSTNAME": "swapnil"
}
]
}
I'm assuming you're using this library.
Your JSON is an Array, so you'll have to use the JSONArray class to parse it

Resources