How to get first item from array in Robot Framework - robotframework

I have the following response from a POST request:
{"facilities":[{"id":"f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4","description":"luovbfvwofgdrcwvqtyqohjioocszgplcjh","hasAnyPartnership":false,"hasAnyProcedure":false}
So I used the "Convert String to JSON" function and got the following response:
{'facilities': [{'id': 'f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4',
'description': 'luovbfvwofgdrcwvqtyqohjioocszgplcjh',
'hasAnyPartnership': False, 'hasAnyProcedure': False}
How do I get the ID value that is inside FACILITIES?
'facilities': [{'id': 'f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4'

The JSON example you have provided is not the valid one. It is missing ] of facilities array and } of opening external brace. After correction it should look like this -
{"facilities":[{"id":"f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4","description":"luovbfvwofgdrcwvqtyqohjioocszgplcjh","hasAnyPartnership":false,"hasAnyProcedure":false}]}
You can use following keywords from JSONLibrary
${json}= Convert String to JSON ${JsonVar}
${idValue}= Get Value From Json ${json} $.facilities[0].id
Output -

Related

Structural error property type should be equal to one of the allowed values allowedValues: string, number, boolean, integer, array in Swagger editor

I'm using springfox 2.9.2 and want test my swagger JSON as YAML in https://editor.swagger.io/
I have property with #ApiParam annotation type: object
#ApiParam(value = "metadata file")
protected Object metadataFile;
but when I test my generated json on swagger editor I got this error:
Structural error at ---.parameters.5.type should be equal to one of
the allowed values allowedValues: string, number, boolean, integer,
array Jump to line ---
there is way to allowed property type object in this section?
the section that trigger the problem
paths:
:
post:
parameters:
name: metadataFile
in: query
description: ...
required: false
type: object
Swagger documentation (go to the bottom of the page): https://swagger.io/docs/specification/2-0/describing-parameters/
"Can I have an object as a query parameter?
This is possible in OpenAPI 3.0, but not in 2.0."

Load JSON File into Robot Framework

I am trying to load a JSON file and use the values to perform some actions based on my tests. I tried to load the json value which I think I got right, but when trying to log the output, I got error message:
Resolving variable '${qa["REQUEST_ID"]}' failed: TypeError: list indices must be integers or slices, not str
Not exactly sure what this means since I am new to Robot Framework. This is what I did to load and log the values:
${file} Get File ${CURDIR}/RequestIDs.json
${qa} Evaluate json.loads('''${file}''') json
Log To Console ${qa["REQUEST_ID"]}
Json file looks something like:
[
{
"REQUEST_ID" : 10513
},
{
"REQUEST_ID" : 48156
},
{
"REQUEST_ID" : 455131
}
]
So basically I want to get the "REQUEST_ID" value and type that in a text field.
Look at the structure of your json - it's a list of dictionaries; so you have to first specify which list member you want, and then its REQUEST_ID field:
Log To Console ${qa[0]["REQUEST_ID"]
# print the value from all present dictionaries in the list:
FOR ${member} IN #{qa}
Log To Console ${member["REQUEST_ID"]
END

How to deserialize the JSON array?

I am trying to add artifacts to VM in DevTest Lab and i want to pass the artifacts name dynamically.
Below is my parameter
"Artifacts": {
"type": "array"
},
And in Resource section am calling this as
"artifacts":["[parameters('Artifacts')]"]
Am calling this ARM as below from powershell file
$sampleJob += Start-Job -Name $fileName -FilePath $scriptlocation -ArgumentList $artifact
$artifact is defined as object type Object[]
When running the script am getting the following error.
"message": "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Microsoft.DevTestLab.VirtualMachine.Data.Models.Rest.ArtifactInstallProperties' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.
How to fix the above error. Any help can be appreciated. Thank you.
In the Resource section, remove the outer pair of square-brackets. It should look like:
"artifacts": "[parameters('Artifacts')]"

Store Variable Of Response in RobotFramework

I want to Store a Value Of ActivationCode That I see it in response.
This is my response in Pycharm terminal. Notice that I convert it to JSON with
${Data}= Evaluate json.loads("""${Response.content}""") json
Now This is my response after convert it to JSON
{'status': 'created', 'statusCode': '0001', 'message': {'type': 'success', 'text': 'ثبتâ\x80\x8cÙ\x86اÙ\x85 Ù\x85Ù\x88Ù\x81Ù\x82Û\x8cت Ø¢Ù\x85Û\x8cز بÙ\x88د'}, 'error': [], '
data': {'user': {'uuid': 'c6b65427-bbfa-4d25-ac3e-aa190d45f5d9', 'username': 'dropp45', 'phoneNumber': '09203947700', 'activationCode': '88261'}}}
How do I store activationCode in a variable for next step what is keyword to show 88261
Once the value is a normal dictionary, you access it with the keys' names:
${value}= Set Variable ${Data['data']['user']['activationCode']}
Log ${value} # will print 88261
You can just go to the Json Response like this:
${activationCode}= Set Variable ${Data}[data][user][activationCode] #You access activationCode
Log ${activationCode} # This will Log 88261

Why parsing my date succeeds when using request, but fails when using reqwests?

I have a string containing the following date 2015-07-05T11:02:25+00:00.
I use the following code to parse it (using chrono crate)
let pub_date = str::replace(entry.pub_date().unwrap(), "-0000", "+0000");
return DateTime::parse_from_rfc2822(&pub_date)
.unwrap_or_else(|e| {
panic!(
"pub_date for item {:?} (value is {:?}) can't be parsed. {:?}",
&entry, pub_date, e
)
})
.naive_utc();
When I get that content using request crate, the date is correctly parsed.
But when I use the reqwests crate, it fails with the error message
[2019-09-07 10:48:45.472951 +02:00] INFO [rrss2imap::feed] src\feed.rs:152: There should be new entries, parsing HTML content
thread 'main' panicked at 'pub_date for item Item { title: Some("[Avis/BD] Capitaine Trèfle"), link: Some("https://fr.rec.arts.fantasy.narkive.com/Z2rKs6RJ/avis-bd-capitaine-trefle"), description: None, author: None, categories: [], comments: None, enclosure: None,
guid: None, pub_date: Some("2015-07-05T11:02:25+00:00"), source: None, content: None, extensions: {}, itunes_ext: None, dublin_core_ext: None } (value is "2015-07-05T11:02:25+00:00") can't be parsed. ParseError(Invalid)', src\feed.rs:236:17
If I debug (using VSCode Rust debugger), the error value simply contains Invalid (3 '\x3'). But I don't understand where it come from ... (I suppose it can be some kind of encoding issue, but it puzzles me, as the whole object seems totally correct).
And I don't understand what is going wrong. Has anybody any idea ?
Turns out that the answer was a simple RFC2282 non-compliance: the source was sending 2015-07-05T11:02:25+00:00. As the RFC states, timezone information is without the : in this format.

Resources