WebClient and VB.NET - Adding headers that have arrays - vb.net

So I'm looking at something similar to this:
POST https://xxxx.io/testAPI
{
"test": "string",
"data": {
"name": "",
"description": "string",
}
}
Adding general headers is fine:
NCLient3.QueryString.Add("test", "Test string")
But for data -> name and data -> description, how would I do it in this regard as it's an array?

Well, First, this is not a valid JSON.
{
"test": "string",
"data": {
"name": "",
"description": "string"
}
}
You can get their values using Linq.
Dim json As String = "{'test': 'string','data': {'name': '','description': 'string'}}"
Dim jsn = Newtonsoft.Json.Linq.JObject.Parse(json)
Dim finalvalue As String = jsn.GetValue("data").Item("description").ToString()
and you'll get the value => string

Related

Get the value from the response based on a condition and store it to a variable

I would like to get the value from the response based on a condition and store it to a variable.
In the below JSON, I would like to store the value when the name matches to something I prefer. Is there a way to achieve this using Karate API?
{
"results": [
{
"name": "Sample1",
"email": "sample1#text.com",
"id": "U-123"
},
{
"name": "Sample2",
"email": "sample2#text.com",
"id": "U-456"
},
{
"name": "Sample3",
"email": "sample3#text.com",
"id": "U-789"
}
]
}
So after reading the comment, my interpretation is to "find" the id where name is Sample2. Easy, just use a filter() operation, refer the docs: https://github.com/karatelabs/karate#jsonpath-filters
Instead of using a filter, I'm using the JS Array find() method as a very concise example below:
* def response =
"""
{ "results": [
{ "name": "Sample1", "email": "sample1#text.com", "id": "U-123" },
{ "name": "Sample2", "email": "sample2#text.com", "id": "U-456" },
{ "name": "Sample3", "email": "sample3#text.com", "id": "U-789" }
]
}
"""
* def id = response.results.find(x => x.name == 'Sample2').id
* match id == 'U-456'
Take some time to understand how it works. Talk to someone who knows JS if needed.

Jmeter extracting response using JSON Path

Hi Can someone help me simulate this scenario, Example this is the response I got, I want to extract all alertId with the name parameter contains test. You response is highly appreciated. Thank you so much.
Response:
[
{
"duplicateCount": 0,
"fqdn": "qa-ubuntu14-4",
"appName": "TEST_APD_UB14",
"stateString": "OPEN",
"category": "FILESCAN",
"alkey": {
"agentId": "8470ea64-a710-3e46-ba6b-ccd37ebc4074",
"role": "AD SERVER",
"alertId": "0258a7ca-bc72-3a53-aa98-3098c87411ba",
"id": "6695a7fa-ab9f-43fa-871b-620cd1eeb75054af7770-604b-11e9-b486-8d59ab9344597cea0ea2-d897-3696-852d-5f3cb36f270e8470ea64-a710-3e46-ba6b-ccd37ebc4074/var/log/test321.txttest321.txtA",
"applicationContextId": "7cea0ea2-d897-3696-852d-5f3cb36f270e"
},
"properties": {
"name": "test321.txt",
"acl": ""
}
},
{
"duplicateCount": 0,
"fqdn": "qa-ubuntu14-4",
"appName": "TEST_APD_UB18",
"stateString": "OPEN",
"category": "FILESCAN",
"alkey": {
"agentId": "8470ea64-a710-3e46-ba6b-ccd37ebc4074",
"role": "AD SERVER",
"alertId": "0258a7ca-bc72-3a53-aa98-3098c8741CDA",
"id": "6695a7fa-ab9f-43fa-871b-620cd1eeb75054af7770-604b-11e9-b486-8d59ab9344597cea0ea2-d897-3696-852d-5f3cb36f270e8470ea64-a710-3e46-ba6b-ccd37ebc4074/var/log/test321.txttest321.txtA",
"applicationContextId": "7cea0ea2-d897-3696-852d-5f3cb36f270e"
},
"properties": {
"name": "test555.txt",
"acl": ""
}
}
]
Screenshot:
Expected Result:
I want to extract all alertId with the name parameter contains test
You could use the following JSON query to extract the values:
[*].[?(#.properties.name contains 'test')]alkey.agentId
I found this reference with JSON Path Syntax is really useful.

how can convert string into json result

I am using function to get the data as a tree format. the data return from GetDepotWithDepartment as a string datatype. how can I convert the string into JsonResult
public JsonResult GetDepotDepartemntsForMap()
{
string lson = _unitOfWork.Department.GetDepotWithDepartment();
return lson // error is coming cannot convert string into mvc.jsonREsult
}
The data coming from the repository linq as given
[
{
"id": 1,
"title": "1-Depot1",
"subs": [
{
"id": "1.1",
"title": "1.Advt"
},
{
"id": "1.2",
"title": "1.Admin"
}
]
},
{
"id": 2,
"title": "2-Depot2",
"subs": [
{
"id": "2.1",
"title": "2.Sales"
},
{
"id": "2.2",
"title": "2.Admin"
}
]
}
]
According to your description, I suggest you could try below codes to return the json result:
public JsonResult GetDepotDepartemntsForMap()
{
string lson = _unitOfWork.Department.GetDepotWithDepartment();
return new JsonResult(lson) // error is coming cannot convert string into mvc.jsonREsult
}
Result:

Is there a way to set property value format requirements based on a condition of the property name?

I have a simple JSON schema:
{
"properties": {
"name": {
"type": "string"
}
},
"type": "object"
}
It requires that name property is a string. This schema does not restrict additional properties, e.g.
{
name: 'foo',
url: 'http://foo'/
}
The latter is a valid input.
Is there a way to set a property value format requirement based on a conditional property name match?, e.g. any property that contains url string in it must correspond to the following schema:
{
"type": "string",
"format": "url"
}
Therefore, an input:
{
name: 'foo',
location_url: 'not-a-valid-url'
}
would cause an error because location_url does not contain a valid URL?
I'd imagine, a schema for something like this would look like:
{
"properties": {
"name": {
"type": "string"
}
},
"matchProperties": {
"/url/i": {
"type": "string",
"format": "url"
}
}
"type": "object"
}
where matchProperties is a keyword I made up.

Parsing JSON data into list of objects

I'm new at JSON and i am currently struggling with a problem parsing JSON data in a list of objects.
The data that i am trying to parse is generated by the facebook graph api, and looks like this :
{
"100001621071794": {
"id": "100001621071794",
"name": "TEST1",
"username": "test1",
"link": "http://www.facebook.com/test1",
"gender": "male",
"picture": "http://profile.ak.fbcdn.net/test1.jpg"
},
"534237692": {
"id": "534237692",
"name": "TEST2",
"username": "test2",
"link": "http://www.facebook.com/test2",
"gender": "female",
"picture": "http://profile.ak.fbcdn.net/test2.jpg"
}
}
I am using the following code to parse :
Dim MyFacebookUsers As List(Of FacebookUser) = MyTwitterSerializer.Deserialize(Of List(Of FacebookUser))(FBData)
The class FacebookUser looks like this :
Public Class FacebookUser
Public id As String
Public name As String
Public username As String
Public link As String
Public gender As String
Public picture As String
End Class
I know that this is not an array, because it is missing '[' and ']'. But when i replace the '{' and '}' with '[' and ']' i'm getting an error because of an invlaid matrix.
Can someone please point me in the right direction?
You're right, that's not a JSON Array, it's an Object containing FacebookUser Objects where the id value is also the element key.
In .NET the Dictionary class is the JSON Object equivalent, so presumably something like
Dim MyFacebookUsers As List(Of FacebookUser) = MyTwitterSerializer.Deserialize(Of Dictionary(Of FacebookUser))(FBData)
is what you need.
As you discovered, you can't turn it into a Array (list) by just changing the braces. However, if you actually had an Array, it would look like this:
[
{"id": "100001621071794",
"name": "TEST1",
"username": "test1",
"link": "http://www.facebook.com/test1",
"gender": "male",
"picture": "http://profile.ak.fbcdn.net/test1.jpg"
},
{"id": "534237692",
"name": "TEST2",
"username": "test2",
"link": "http://www.facebook.com/test2",
"gender": "female",
"picture": "http://profile.ak.fbcdn.net/test2.jpg"
}
]
and your code would have worked fine.