In RestAssured jsonPathEvaluator not giving correct values for double value - rest-assured

I have a json response as shown below :-
{
"someField": [
{
"abc": "abcdId"
}
],
"someId": "pqrsId",
"oneTier": {
"startThreshold": 25000,
"endThreshold": 74999.99
},
"nextTier": {
"startThreshold": 75000,
"endThreshold": 149999.99
}
}
When I try to print
response.getBody().asString()
I can get the see the value of nextTier.endThreshold as '14999.99'
but when I do
response.getBody().jsonPath().get("nextTier.maxThreshold").toString();
I am seeing the value as 14999.98'.
Not able to figure out why this is happening.

The default config JsonPathConfig defines NumberReturnType = FLOAT_AND_DOUBLE, this may lose the precision when converting between types.
Fortunately, RestAssured has a solution for this kind of issue. You can config NumberReturnType = BIG_DECIMAL then it saves the precision of floating-point number.
JsonPath.config = new JsonPathConfig().numberReturnType(JsonPathConfig.NumberReturnType.BIG_DECIMAL);
BigDecimal endThreshold = JsonPath.with(json).get("nextTier.endThreshold");
System.out.println(endThreshold.doubleValue());
//149999.99

Related

Twilio Studio Say/Gather Hints

We are using the Twilio Studio for managing our IVR flows, and have come across an issue when recognising particular numbers.
Example: A verification code that has 22 in it, is being recognised by Twilio as "tutu"
Aside from changing the settings like "recognition language", I'd like to get Twilio to recognise numbers more than other inputs. There is the option for "Speech Recognition Hints" which is a comma separate list of values - but what should you put in there? The documentation just talks about a comma separated list, nothing else!
Any help gratefully received.
Thanks in advance
You could see if entering $OOV_CLASS_DIGIT_SEQUENCE in the hints section has any impact on the captured SpeechResult.
Another option is to run the result through a normalization Twilio Function that converts tutu to 22.
I would recommend DTMF when capturing digits, to avoid this.
// converts number words to integers (e.g. "one two three" => 123)
function convertStringToInteger( str ) {
let resultValue = "";
let arrInput = [];
let valuesToConvert = {
"one": "1",
"two": "2",
"to": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
"zero": "0"
};
str = str.replace(/[.,-]/g," "); // sanitize string for punctuation
arrInput = str.split(" "); // split input into an array
// iterate through array and convert values
arrInput.forEach( thisValue => {
if( ! isNaN(parseInt(thisValue)) ) { // value is already an integer
resultValue += `${parseInt(thisValue)}`;
} else { // non-integer
if( valuesToConvert[thisValue] !== undefined) {
resultValue += valuesToConvert[thisValue];
} else {
// we don't know how to interpret this number..
return false;
}
}
});
console.log('value converted!', str, ' ====> ', resultValue);
return resultValue;
}

how to get double value from Firebase Remote Config in iOS?

I have a default value in remote config console like the image above, I need to get that double value. in Android, I can get it like this
val remoteConfig = FirebaseRemoteConfig.getInstance()
val value = remoteConfig.getDouble("parameter_name")
but now I am confused how to get that double value for iOS, it seems there is no getDouble equivalent in swift, so what should I do ?
let remoteConfig = RemoteConfig.remoteConfig()
let value = // what should i write in here ?
You could read it as a String form the config, and then parse the String into a Double.
if let valueAsString = RemoteConfig.remoteConfig().configValue(forKey: "yourKey").stringValue {
if let valueAsDouble = Double(valueAsString) {
// you have your value as a Double now
} else {
print("Not a valid number: \(valueAsString)")
}
}
Firebase also offers "numberValue". The firebase code internally is:
[NSNumber numberWithDouble:self.stringValue.doubleValue]
This means: it returns 0 if the value cannot be converted. You have more control with the String way.

How to put variable inside text field AND how to convert all elements to string NOT just 1 at a time?

This is a follow up question to How to have 10 characters total and make sure at least one character from 4 different sets is used randomly
this is my code so far
let sets = ["ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz", "1234567890", "\"-/:;()$&#.,?!'[]{}#%^\\|~<>€£¥•.,"].map { Array($0.characters) }
var randoms = sets.map { $0.random }
while randoms.count < 10 {
randoms.append(sets.random.random)
}
var convertedElems = String()
let something = randoms.shuffled()
for key in something {
convertedElems = String(key)
}
uniqueRoomID.text = randoms.shuffled()
Im getting an error saying cannot convert [Element] to type "String"
So i tried a for loop but that only converts 1 at a time when its supposed to do all 10
my other question is i tried storing a character in a variable and then setting a text field.text equal to that variable and nothing happened
What am i doing wrong here
Your randoms.shuffled() is an array of Characters. You need to convert it back into a String.
Change this:
uniqueRoomID.text = randoms.shuffled()
to this:
uniqueRoomID.text = String(randoms.shuffled())

json parsing in swift

Here is my Json
{
"id": "63",
"name": "Magnet",
"price": "₹1250",
"description": "",
"image": [
"catalog/IMG-20150119-WA0012_azw1e3ge.jpg",
"catalog/IMG-20150119-WA0029_6mr3ndda.jpg",
"catalog/IMG-20150119-WA0028_ooc2ea52.jpg",
"catalog/IMG-20150119-WA0026_4wjz5882.jpg",
"catalog/IMG-20150119-WA0024_e38xvczi.jpg",
"catalog/IMG-20150119-WA0020_vyzhfkvf.jpg",
"catalog/IMG-20150119-WA0018_u686bmde.jpg",
"catalog/IMG-20150119-WA0016_c8ffp19i.jpg"
],
"thumb_image": [
"cache/catalog/IMG-20150119-WA0012_azw1e3ge-300x412.jpg",
"cache/catalog/IMG-20150119-WA0029_6mr3ndda-300x412.jpg",
"cache/catalog/IMG-20150119-WA0028_ooc2ea52-300x412.jpg",
"cache/catalog/IMG-20150119-WA0026_4wjz5882-300x412.jpg",
"cache/catalog/IMG-20150119-WA0024_e38xvczi-300x412.jpg",
"cache/catalog/IMG-20150119-WA0020_vyzhfkvf-300x412.jpg",
"cache/catalog/IMG-20150119-WA0018_u686bmde-300x412.jpg",
"cache/catalog/IMG-20150119-WA0016_c8ffp19i-300x412.jpg"
],
"specifications": [
{
"Fabrics": [
"Pure chiffon straight cut suits 48" length"
]
},
{
"MOQ": [
"Minimum 10"
]
}
]
}
In above json string the "specification" arraylist has dynamic number of key and each key has dynamic number of values
So how can parse this? Please help if anyone knows this...
Thanks in advance
There are multiple ways to do parsing. In your case specifications should be an array, so you'll be able to loop on each items.
You might want to :
Create your own JSON parse class / methods ;
Use an existing library to parse JSON.
For the second option, you can give a look at the following :
https://github.com/Wolg/awesome-swift#jsonxml-manipulation
var yourJson = data as? NSDictionary
if let id = yourJson.valueForKey("id") as String
{
//save your id from json
}
if let name = yourJson.valueForKey("name") as String
{
//save your name
}
...
if let images = yourJson.valueForKey("image") as NSArray
{
for im in images
{
//save image
}
//the same for all othe images
}
... And so on...
You should also watch some tutorials, to understand the basics of JSON parsing..
https://www.youtube.com/watch?v=MtcscjMxxq4

Parsing JSON Response with Alamofire in Swift

When using the Alamofire Framework, my responses don't seem to be getting parsed correctly. The JSON response I get has some keys that appear to not be strings, and I don't know how to reference them/get their values.
Here is the part of my code that makes the call:
var url = "http://api.sandbox.amadeus.com/v1.2/flights/low-fare-search"
var params = ["origin": "IST",
"destination":"BOS",
"departure_date":"2014-10-15",
"number_of_results": 1,
"apikey": KEY]
Alamofire.request(.GET, url, parameters: params)
.responseJSON { (_, _, json, _) in
println(json)
}
}
And here is the first section printout when that function is called
Optional({
currency = USD;
results = ({
fare = {
"price_per_adult" = {
tax = "245.43";
"total_fare" = "721.43";
};
restrictions = {
"change_penalties" = 1;
refundable = 0;
};
"total_price" = "721.43";
};
...
});
});
You'll notice that results is not "results", but "price_per_adult" is the correct format. Is there some step I'm missing? When I cast it to NSDictionary it doesn't do anything to help the key format either.
I also tried the same endpoint in javascript and ruby, and both came back without problem, so I'm fairly confident that it is not the API that is causing problems.
Those keys are still Strings, that's just how Dictionarys are printlnd. It looks like it will surround the String in quotes when printing it only if it contains non-alphanumeric characters (_ in this case). You can test this by manually creating a Dictionary similar to the one you're getting back from your API request and then printing it:
let test = [
"currency": "USD",
"results": [
[
"fare": [
"price_per_adult": [
"tax": "245.43",
"total_fare": "721.43"
],
"restrictions": [
"change_penalties": 1,
"refundable": 0
],
"total_price": "721.43"
]
]
]
]
println(test)
Outputs:
{
currency = USD;
results = (
{
fare = {
"price_per_adult" = {
tax = "245.43";
"total_fare" = "721.43";
};
restrictions = {
"change_penalties" = 1;
refundable = 0;
};
"total_price" = "721.43";
};
}
);
}

Resources