Can I send 2 dimension array as parameter in POSTMAN? - arrays

I need to send parameters as array of objects in POSTMAN.
"array": [
{"field1": "html", "field2": "5"},
{"field1": "css", "field2": "3"}
]
I know that array must send as array[] but how can I set one item of the array as an object?
I tried this
"array[]" : "{"field1": "html", "field2": "5"}"
But I'm getting a 500 response error.

Just send it in raw format(in json) and specify data type as application/json. Worked for me

If array of arrays works for you as well, you can send them like this:

Go to bulk edit
userdata:[{"name":"name1","email":"email1#gmail.com"},{"name":"name2","email":"email2#gmail.com"},{"name":"name3","email":"email3#gmail.com"}]

I found the solution adding raw data as JSON.
from body > raw
[
{
"text": "Buy fish",
"completed": true
},
{
"text": "Go for walk check",
"completed": false
},
{
"text": "Pick baby from school",
"completed": false
}
]

I know this ia an old post but this might help someone out there.... it's what worked for me:
In your controller: (Laravel/PHP)
$validator = Validator::make($request->all(), [
'fields' => 'nullable|array|min:1',
'fields.key1.*' => 'integer',
'fields.key2.*' => 'integer'
]);
if ($validator->fails())
{
return ...
}
Then in Postman:
fields[0]['key1']
fields[0]['key2']
.... and you can repeat this as many times as you need to, just increment the index
fields[1]['key1']
fields[1]['key2']
If you dd(fields) in your controller, you'd get:
0 => [
key1 => value1
key2 => value2
]
1 => [
key1 => value3
key2 => value4
]
.....

Related

how to use json feeders in gatling when the json structure is complicated

In the scenario, how to give the EL$ for the field 'location' and 'ID'.
I tried giving as below but getting error as Attribute not defined for 'ID' and 'Location'
.body(
StringBody(
"""{"name": "${name}", "Url": "${Url}", “Product”: [ { "ID": "${ID}", "location": "${location}" } ] }"""
)
).asJson
JSON:
[
{
"name":"xyz",
"url":"test1.com",
"Product":[
{
"ID":111111,
"location":"NewYork"
}
]
},
{
"name":"abc",
"url":"test2.com",
"Product":[
{
"ID":22222,
"location":"Texas"
}
]
}
]
The ID and location fields are inside a Product object, so the correct Gatling EL String would be respectively #{Product.ID} and #{Product.location}.

A way to access json data

I am importing a json file as 'data' to use it as a initial state of chatList. However,
Property does not exist on type
keeps showing up. Is there a way that I can access chats in data? ->
data is an array too, so it should be for example data[0].chats –
Apostolos : Thanks a lot!
[
{
"partnerId": "user1",
"chats": [
{
"userId": "user0",
"message": "Olaf, you are melting!",
"msgId": 1644809969390
},
{
"userId": "user1",
"message": "Some people are worth melting for",
"msgId": 1644809969387
}
]
},
{
"partnerId": "user2",
"chats": [
{
"userId": "user2",
"message": "11111",
"msgId": 1644809969392
},
How can I access certain partnerId so that I can filter chats and set as the initial state of chatList?
If you want to filter by a specific partnerId, then you should do the following:
const [chatList, setChatList] = useState<IChat[]>(data
.filter(dt => dt.partnerId === 'ABCD')
.map(t => t.chats).flat()
.filter(user => user.userId === 'XYZ')

Get the value of 2nd index by mapping 1st index through Hash of Arrays in Perl

I have a Perl file code state.pl where I am trying to retrieve full name of State based on State code from Hash of Arrays. Following is the code:
my $all_state_details = {
IN => [
[
"AP",
"Andhra Pradesh"
],
[
"AN",
"Andaman and Nicobar Islands"
],
[
"AR",
"Arunachal Pradesh"
],
],
US => [
[
"AL",
"Alabama"
],
[
"AK",
"Alaska"
],
[
"AS",
"American Samoa"
],
],
};
my $state = 'AN';
my $country = 'IN';
my #states = $all_state_details->{$country};
my #state_name = grep { $_->[0] eq $state } #states;
print #state_name;
When I run the script, I get the blank output
I want the output as just:
Andaman and Nicobar Islands
The #{ ... } dereference operation is necessary to convert the array reference in $all_state_details->{$country} into an array suitable for use with grep.
print map { $_->[1] }
grep { $_->[0] eq $state } #{$all_state_details->{$country}};
The right way to do this kind of lookup is with a hash of hashes.
e.g.
%all_state_details = (
IN => {
"AP" => "Andhra Pradesh",
"AN" => "Andaman and Nicobar Islands",
},
);
Then you just do a direct lookup.
print $all_state_details{IN}{AN};
Read https://perldoc.perl.org/perldsc.html#HASHES-OF-HASHES
HTH

How can i make two objects out of one object, dividing them by a value in the object with lodash

Consider this object, with two channels with NL language and one with EN language:
[
{
"name": "De Redactie",
"channels": [
{
"name": "headlines",
"pubDate": "2017-05-15 09:15:00",
"language": "nl",
"items": [
]
},
{
"name": "headlines English",
"pubDate": "2017-05-14 18:05:00",
"language": "en",
"items": [
]
},
{
"name": "politiek",
"pubDate": "2017-05-14 20:11:00",
"language": "nl",
"items": [
]
}
]
}
]
How can i divide them so that i can get this result:
[
{
"name": "De Redactie",
"channels": [
{
"name": "headlines",
"pubDate": "2017-05-15 09:15:00",
"language": "nl",
"items": [
]
},
{
"name": "politiek",
"pubDate": "2017-05-14 20:11:00",
"language": "nl",
"items": [
]
}
]
}
{
"name": "De Redactie",
"channels": [
{
"name": "headlines English",
"pubDate": "2017-05-14 18:05:00",
"language": "en",
"items": [
]
}
]
}
]
Mind you that this is dummyData. The actual data can contain x amount of one language and y amount of the second or third or fourth ...
I have tried looking in the lodash documentation for the correct combination of functions. Also tried various complex forEach structures, but could not wrap my head around it.
Preferably a solution with lodash or typescript, as i'm working in Angular 4.
Iterate the array with Array#map. For each object, extract the array of channels using destructuring with object rest. Iterate the channels using Array#reduce and group channels with the same language to a Map. Convert back to an array by spreading the Map's values iterator.
Create an array of objects, by mapping them and assigning the group as the channels prop of the object. Flatten the array by spreading into Array#concat:
const data = [{"name":"De Redactie","channels":[{"name":"headlines","pubDate":"2017-05-15 09:15:00","language":"nl","items":[]},{"name":"headlines English","pubDate":"2017-05-14 18:05:00","language":"en","items":[]},{"name":"politiek","pubDate":"2017-05-14 20:11:00","language":"nl","items":[]}]}];
const result = [].concat(...data.map(({ channels, ...rest }) => {
const channelGroups = [...channels.reduce((m, channel) => {
m.has(channel.language) || m.set(channel.language, []);
m.get(channel.language).push(channel);
return m;
}, new Map()).values()];
return channelGroups.map((channels) => ({
...rest,
channels
}));
}));
console.log(result);
way with lodash:
const res = _.chain(arr)
.flatMap(item => _.map( // get array of channels with parent name
item.channels,
channel => _.assign({}, channel, { parentName: item.name })
))
.groupBy('language') // group channels by language
.values() // get channel arrays for each lang
.map(langArrs => ({ // set finished structure
name: _.first(langArrs).parentName, // get name for lang channels
channels: _.omit(langArrs, ['parentName']) // set channels without parent name
}))
.value();

How to modify a complex JSON Object in using Immutable

I have below JSON and wanted to update the value depending on Aid, Bid and Cid using Immutable.js
e.g.
Below input provided.
Aid= A, Bid = 1, Cid= 4, NewValue = 'FOUR'
If above input is provided the value "One" needs to be changed to "FOUR"
let sampleJson = {
Aid: 'A', detail:"sample", list: [
{
"Bid": "1",
"group": [
{
"name": "Group A",
"Cid": "4",
"value": "One"
},
{
"name": "Group A",
"Cid": "41",
"value": "1"
},
]
},
{
"Bid": "2",
"group": [
{
"name": "Group A",
"Cid": "4",
"value": "1"
},
{
"name": "Group A",
"Cid": "4",
"value": "1"
},
]
};
I was able to access the value using below code. How can i return the entire JSON with updated value?
let variale = Immutable.fromJS(sampleJson).
getIn(['list']).
find(allocation => allocation.get("Bid") === "1").
getIn(['group']).
find(fun => fun.get("Cid") === "4").set('value',"FOUR");
Anyone has any suggestions on how to resolve this problem?
I think you can try to do this like so:
let immutable = Immutable.fromJS(sampleJson);
immutable = immutable.setIn(['list', 0, 'group', 0, 'value'], 'FOUR');
This monstrosity is how I would do it:
const newData = originalData.update('list', list => {
const itemIndex = list.findIndex(item => item.get('Bid') === '2');
return list.update(itemIndex, listItem => {
return listItem.update('group', groupList => {
const groupIndex = list.findIndex(group => group.get('Cid') === '4');
return groupList.update(groupIndex, group => {
return group.set('value', 'FOUR');
});
});
});
});
https://jsbin.com/latupo/7/edit?html,js,console
Personally I stopped using Immutable, I always found it a bit painful (not to mention those docs!). I now use redux and good old cloning to not mutate state. Less performant in theory but if you've got nothing that runs over a few milliseconds anyway, save yourself the trouble...

Resources