How to use conditional statement in Postman Body section - json

I have a scenario to validate multiple datasets with the same logic. Only change is dataset location.
Eg:
Apple, Sony, MI are the datasets, their data is placed in separate folder. If I pass the variable name as Apple, then in postman body part it should check which data to pass.
Body Section in Postman using JSON
{
"if" : { {{Mobile}} : "Apple"},
"then" : {"location"},
"else":
{
"if" : { {{Mobile}} : "Sony"},
"then" : {"location"}
}
}
If I use the above code, I got the response as Undefined and 200 OK
My expected response should be some ID value (eg: 1,2,3 etc.)

change variable from pre-request, set body as :
{
"mobile":"{{mobile}}"
"location":"{{location}}"
}
and in pre-request
const mobile = pm.variables.get("mobile")
if(mobile ==="Apple"){
pm.variables.set("location","somelocation1")
}else if ( mobile === "sony"){
pm.variables.set("location","somelocation2")
}

Related

How to update the whole array of objects using Put http request with a json raw body?

I have a json Array something like below and I want to make an update to the whole list without passing by a specefic ID.
List before update
[ { id:"1", name : "name_1" }, { id:"2", name : "name_2" } ]
Wanted list after update
[ { id:"1", name : "name_3" }, { id:"2", name : "name_4" } ]
I tried using PUT request and passing the target list in a json raw body, but it always return "404 not found"
I tried it with Postman but it returns the same error. Is it possible to do like so ?

handling a well-formed JSON file of an array of objects

A JSON string string passes the jsonlint test.
response = [
{
"article" : {
"info" : {
"initial" : {
"articleIds" : [
"7461221587662919569"
],
}
},
"text" : "where they would 'transfer to' next.",
"lang" : "en",
}
},
{
"article" : {
"info" : {
"initial" : {
"articleIds" : [
"6613144915874808065"
],
}
},
"text" : "produto regional.",
"lang" : "pt"
}
}
]
However, after processing
require 'json'
file = File.read('/Users/main/jugg//article_samples.js')
data_hash = JSON.parse(file)
One is left with an array, whereas more frequently a hash with a name labels a subsequent array, where one works with that nomenclature such as response['data']
But in this case the array is not accessible via response[0]. How can this be considered as an array in order to process each individual element collection.each do |member|?
A curiosity: data_hash.class => NilClass
The response = ... code from article_samples.js is JavaScript, not JSON. This initializes a variable named response with a JavaScript array.
To use this as JSON, then rename the file to article_samples.json and remove response = from the file. The first line should start with [.
Now your second block of code should work just fine as long as the article_samples.json file is in the correct path.
On a side note, I suggest that you find a way to make the path more flexible. The way you have it currently hard coded is tied directly to your current machine's file system. This won't work if you want to run this code from another machine because the folder /Users/main/jugg probalby won't exist.
If this is a web server with ruby on rails, then one solution is to create an environment variable with the path where this file is stored.

How to increment json object properties values to dynamically populate the json body of a jmeter request

Using JMeter I need to send a request which contains a body in such a format:
{
"key1":"value1",
"key2":{
"key21":[
{
"key211_1":"valueA",
"key212_1":"valueToIncrement",
"key213_1":{
"key2131_1":"valueB",
"key2132_1":"valueC",
},
"key214_1":{
"key2141_1":"valueD",
"key2142_1":"valueE"
}
},
{
"key211_2":"valueA",
"key212_2":"valueToIncrement+1",
"key213_2":{
"key2131_2":"valueB",
"key2132_2":"valueC",
},
"key214_2":{
"key2141_2":"valueD",
"key2142_2":"valueE"
}
}
]
}
}
Inside the body I can have a list of many json objects and each object will have some property with an unique id inside the request.
Something like:
{
"key212_1":"1"
},
{
"key212_2":"2"
}
...
{
"key212_2":"n"
}
Because I can have hundreds of objects inside a request body, can I create a logic in Jmeter, so that each time a request is sent, each value from the target property would have a value incremented by 1 in each of these properties without having to manually hardcode values?
You will have to maintain Counter. At the request where you want to increment the value, add a JSR223 Pre Processor. In that you will have to code to increment the value and save it back to the variable and also assign it your Json object.
EDIT :- I tried to simulate your setup as below.
Setup
RESULT

Best Schema for a Data List in JSON for RestFul API to use in Angular

I've been wondering for some days what kind of scheme would be more appropriate to use a data list in json in a web application.
I'm developing a REST Web Application, and im using Angular for front end, i should order, filter and print these data list also in xml ...
For you what scheme is better and why?
1) {
"datas": [
{ "first":"","second":""},
{ "first":"","second":""},
{ "first":"","second":""}
]
}
2) {
"datas": [{
"data": { "first":"","second":""},
"data": { "first":"","second":""},
"data": { "first":"","second":""}
}]
}
3) [
{ "first":"","second":""},
{ "first":"","second":""},
{ "first":"","second":""}
]
Thanks so much.
The first and third notations are quite similar because the third notation is included in your first.
So the question is "Should I return my datas as an array or should I return an object with a property that contain the array ?
It will depend on either you want to have more information alongside your datas or not.
For exemple, if your API might return an error, you will want to manage it from the front end.
In case of error, the JSON will looks like this :
{
"datas": null,
"error": "An error occured because of some reasons..."
}
At the opposite, if everything goes well and your API actually return the results, it will looks like this :
{
"datas": [
{ "first":"","second":""},
{ "first":"","second":""},
{ "first":"","second":""}
],
"error": null
}
Then your front end can use the error property to manage errors sent from the API.
var result = getDatas(); // Load datas from the API
if(result.error){
// Handle the error, display a message to the user, ...
} else {
doSomething(result.datas); // Use your datas
}
If you don't need to have extra properties like error then you can stick with the third schema.
The second notation is invalid. The datas array will contain only one object which will have one property named data. In this case data is a property that is defined multiple times so the object in the array will contain only the last occurence:
var result = {
"datas": [{
"data": { "first":"a","second":"b"},
"data": { "first":"c","second":"d"},
"data": { "first":"e","second":"f"}
}]
}
console.log("Content of result.datas[0].data : ")
console.log(result.datas[0].data)
Obviously the first option would be easy to use. Once you will access datas it'll give you an array. Any operation (filter, sort, print) on that array will be easy in comparison to anything else. Everywhere you just need to pass datas not datas.data.

Replace every array in object by first element of that array

I have a json like this:
"Client" : {
"ClientId" : "eertertwetw",
"Username" : "c.client",
"Names" : [
{
"Family" : "ClientFamilyName",
"Given" : [
"ClientGivenName"
]
}
]
}
This json is not fixed, so sometimes there are some properties and sometimes not.
I need to replace every array inside this Json with the first element of that array. So, for example, in this case it would be like
"Client" : {
"ClientId" : "eertertwetw",
"Username" : "c.client",
"Names" :
{
"Family" : "ClientFamilyName",
"Given" :
"ClientGivenName"
}
]
}
Can anyone help me to find a way to do this with Typescript?
Ok, I tried something (haven't tested it against all possible cases) but it seems like it's working.
Stackblitz - check the console for result.
let data = { Client: {...} }; // your data
data = data.map(client => {
if (!Object.hasOwnProperty(client.Names, 'Prefix')) {
client.Names.Prefix = null;
}
return client;
});
You need get the firt element in array in object.
like : Client.Names[0].Given[0].ClientGivenName