merge objects inside array - arrays

My Code here return the response like this
{
"code": 422,
"message": "The given data was invalid.",
"errors": {
"0": {
"first_name": [
"The first name field is required."
]
},
"1": {
"last_name": [
"The last name field is required."
]
},
"2": {
"mobile": [
"The mobile must be an integer.",
"The mobile must be at least 9."
]
}
}
}
I need to combine objects and remove numbers to return like this
{
"code": 422,
"message": "The given data was invalid.",
"errors": {
"first_name": [
"The first name field is required."
],
"last_name": [
"The last name field is required."
],
"mobile": [
"The mobile must be an integer.",
"The mobile must be at least 9."
]
}
}
private function transformErrors(ValidationException $exception)
{
$errors = [];
foreach ($exception->errors() as $field => $message) {
$errors[] = [
$field => $message
];
}
return (object)$errors;
}
what is best way to handle response in this way

If I understood correctly, you just want to transform multidimensionnal array into a simple array.
You can try to use :
private function transformErrors(ValidationException $exception)
{
return array_values($exception->errors());
}

You can use array_values() function to gather the values from your errors array. Something like this:
$yourArray['errors'] = array_values($yourArray['errors']);

Related

How can i filter the un matched array elements alone when compare with another array objects in angular 8

I have a two array response and I would like to compare the two responses and have to filter the unmatched array elements into a new array object.
Condition to compare the two response and filter is: we have to filter when code and number are not matched exactly with the response two then we have to filter such an array element into a new array object which I need as an output.
The Array element present in the Response two example is also present in the Response of Array One example which I don't want and I need to filter the array elements which is not matched with the Response of Array One.
Final Output which we filtered from the response two array will be like below which is unmatched with the response 1 array object:
{
"unmatchedArrayRes": [
{
"code": "08",
"number": "2323232323",
"id": "432",
"value": "value432"
}
]
}
Response of Array One
{
"MainData": [
{
"DataResponseOne": [
{
"viewData": {
"number": "11111111111111",
"code": "01"
},
"name": "viewDataOne"
},
{
"viewData": {
"number": "22222222222222",
"code": "01"
},
"name": "viewDataTwo"
},
{
"viewData": {
"number": "3333333333333",
"code": "02"
},
"name": "viewDataThree"
}
]
},
{
"DataResponseTwo": [
{
"viewData": {
"number": "5555555555555",
"code": "9090"
},
"name": "viewDataFour"
},
{
"viewData": {
"number": "6666666666666",
"code": "01"
},
"name": "viewDataFive"
},
{
"viewData": {
"number": "8888888888888",
"code": "01"
},
"name": "viewDataSix"
}
]
}
]
}
Response Two Example :
{
"compareRes": [
{
"code": "01",
"number": "11111111111111",
"id": "123",
"value": "value123"
},
{
"code": "9090",
"number": "5555555555555",
"id": "345",
"value": "value567"
},
{
"code": "08",
"number": "2323232323",
"id": "432",
"value": "value432"
}
],
"metaData": "343434343434"
}
First, create a combined list of all the view items from response one.
const combinedList = [];
res1["MainData"].forEach(data => {
// console.log(data);
for( let key in data) {
// console.log(key);
data[key].forEach(innerData => {
console.log(innerData)
combinedList.push(innerData["viewData"]);
})
}
});
In the above method, It is done in such a way that it can handle multiple viewData responses like DataResponseOne, DataResponseTwo, and so on.
And then filter second response Items like this:
const unfilteredListItems = res2["compareRes"].filter(data => {
return !combinedList.some(listItem => {
return listItem.code === data.code && listItem.number === data.number;
});
});
console.log(unfilteredListItems);
Working Stackblitz link: https://stackblitz.com/edit/ngx-translate-example-aq1eik?file=src%2Fapp%2Fapp.component.html

CastError: Cast to ObjectId failed for value 'xxx' at path "_id"

I have an array in request as:
{
"acceptedBookings": [
{
"id": "e1f66d7a852986709f665c3",
"Date": "2020-02-04T05:03:25.332Z"
}
]
}
I want to update the "date" for every "id". However If I search as
await Booking.findById( acceptedBookings[0].id ) or
await Booking.findOne({_id : acceptedBookings[0].id})
It does not give a response
You're accessing wrong member, what you want is:
let's assume your map is something like
const acceptedBookings = {
"accepted": [{
"id": "e1f66d7a852986709f665c3",
"Date": "2020-02-04T05:03:25.332Z"
},
{
"id": "i123",
"Date": "2020-02-04T05:03:25.332Z"
},
{
"id": "i123",
"Date": "2020-02-04T05:03:25.332Z"
}
]
};
console.log(acceptedBookings.accepted[0].id); // e1f66d7a852986709f665c3
console.log(acceptedBookings.accepted[1].id); // i123
await Booking.findById( acceptedBookings.accepted[0].id ) //should work fine
Remember the object you've created that is not an array it's map/object with a key: value pair
thus get the right array/element first then access it's members

How to decode json array to find the last value of a variable using Perl?

I have a Json array of objects that I am trying to decode in perl. The json array looks like this :
[
{
"name": "a123",
"enroll": "12a123",
"cs": {
"year1": {
"status": {
"oldvalue": "pending",
"new value": "complete"
}
}
}
},
{
"name": "b123",
"enroll": "12b123",
"ecm": {
"year1": {
"flag": {
"oldvalue": "null",
"new value": "ok"
}
}
}
},
{
"name": "c123",
"enroll": "12c123",
"cs": {
"year1": {
"status": {
"oldvalue": "complete",
"new value": "run new"
}
}
}
}
]
I want to find the value of the {"status"}->{"new value"} from the last occurrence in the Json file.The output here should be "run new". FYI : not all fields are present in every object. Any help on how to parse this array will be highly appreciated.
I'd use a module for this - CPAN has a module called JSON:Parse which looks to do what you want:
use JSON::Parse 'parse_json';
my $json = loadJsonString(); # Load the JSON string from somewhere
my $courses = parse_json($json);
my $lastCourse = $courses->[-1];
my $newValue = $lastCourse->{cs}{year1}{status}{"new value"};

How to print a json object

Hi Im new to JSON this may be a basic question, im having data as a JSON object and i want to print the "message" data alone. My sample JSON data, in this how can i access message?
{
"name": "Usingtagproject",
"fan_count": 0,
"category": "Product/Service",
"feed": {
"data": [
{
"created_time": "2017-05-02T18:24:27+0000",
"message": "hii",
},
{
"created_time": "2017-05-02T09:26:37+0000",
"message": "Hi Google",
},
{
"created_time": "2017-05-02T09:24:26+0000",
"message": "Hi Demoproject",
}
],
}
JSON is essentially a map (keys with values), that has a specific syntax.
It completely depends on what language you're accessing it in, but you can assume that most languages will have key indexing, so that you can say:
string name = my_json['name']
list<map> data = my_json['data']
for data_map in data:
print "found message: " + data_map['message']

Updating a field with a nested array in Elastic Search

I am trying to update a field in a document with an array. I want to add an array to the field "products". I tried this:
POST /index/type/1/_update
{
"doc" :{
"products": [
{
"name": "A",
"count": 1
},
{
"name": "B",
"count": 2
},
{
"name": "c",
"count": 3
}
]
}
}
this is the error response I am getting when I try and run the code:
{
"error": {
"root_cause": [
{
"type": "mapper_parsing_exception",
"reason": "failed to parse [products]"
}
],
"type": "mapper_parsing_exception",
"reason": "failed to parse [products]",
"caused_by": {
"type": "illegal_state_exception",
"reason": "Can't get text on a START_OBJECT at 1:2073"
}
},
"status": 400
}
Anyone know what I am doing wrong?
The message "Can't get text on a START_OBJECT" means that Elasticsearch was expecting an entry of type "string" but you are trying to give an object as input.
If you check Kibana you will find that the field "products" exists there and is defined as a string. But since you are entering a list of dictionaries then the field "products" should have been defined from the beginning as an object (preferably with dynamic fields in it). An example would be (see full example at https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic.html )
"products": {
"dynamic": true,
"properties": {}
}
However since you already have the index then you can't change the mapping so you would need to delete the index, do the mapping beforehand and then do the update.

Resources