Convert two dimentional array into the json object in Laravel - json

I am converting a two-dimensional array into a JSON object.
Now my controller returning this output:
[
[
{
"id": 1,
"title": "V1",
"project_id": 1
}
],
[]
]
Want to convert it like:
[
{
"id": 1,
"name": "Laravel"
},
{
"id": 2,
"name": "Wordpress"
}
]
My controller code:
$user = auth()->user();
$projectVersions = array();
foreach($user->projects as $project)
{
$projectId = $project->pivot->project_id;
$projectVersions[] = Version::where('project_id', $projectId)
->where('created_by', $user->id)
->get();
}
return $projectVersions;
When I am using below code which is suggested by #John Lobo, it is returning below output:
{
"id": 2,
"name": "Hadayat Niazi",
"email": "niazicr801#gmail.com",
"is_admin": 0,
"email_verified_at": null,
"created_at": "2021-06-11T15:13:00.000000Z",
"updated_at": "2021-06-11T15:13:00.000000Z",
"projects": [
{
"id": 1,
"name": "Laravel",
"pivot": {
"user_id": 2,
"project_id": 1
},
"versions": [
{
"id": 1,
"title": "V1"
}
]
}
]
}
Displaying the user projects and versions:
Again I am updating the code for #John Lobo, he want to look the output
$user = User::with(['projects', 'projects.versions'])
->find(auth()->user()->id);
$result = $user->projects->map(function ($project) {
return [
$project->versions,
];
});
return $result;
Output the code
[
[
[
{
"id": 1,
"title": "V1",
"project_id": 1,
"version": "1.22",
"created_by": 2,
"is_publish": 0,
"feature": "<b>In this ipdate asdsdasd</b>",
"bug_fix": "<p><b>sdfdffffffffff</b></p>",
"created_at": "2021-06-12T08:08:21.000000Z",
"updated_at": "2021-06-12T08:08:21.000000Z"
},
{
"id": 2,
"title": "v2",
"project_id": 1,
"version": "2.12",
"created_by": 2,
"is_publish": 0,
"feature": "adsdasdsadas",
"bug_fix": "sddasdas",
"created_at": null,
"updated_at": null
}
]
],
[
[]
]
]

In controller you can do the following.if you have relation in project model called version
$projectVersions=User::with(['projects','projects.version'])->find(auth()->user()->id);
return $projectVersions->projects;
I believe Project model has below relationship
public function version(){
return $this->hasMany(Version::class);
}
Updated
$user=User::with(['projects','projects.versions'])->find(auth()->user()->id);
$result = $user->projects->map(function ($project) {
return $project->versions;
});
dd($result);

Use like below
$json = json_encode($responseOfController);
echo "<pre>";print_r($json);die;

Related

How to modify json using JRS223 preprocessor in jmeter

Json body looks like this:
{
"access_key": "",
"erid": "",
"ch_sms": {
"messages": [
{
"urlsh": false,
"sp": "123",
"vp": 20,
"heid": 1,
"teid": ,
"peid": ,
"tmid": 4,
"msg": {
"txt": "hello"
},
"da": [
{
"number": "97278",
"cc": "IN",
"uid": "uid1",
"tags": [
"key",
"Value"
]
}
]
}
],
"metadata": {
"chver": "1.0",
"cburl": "",
"heid": 1,
"teid": ,
"peid": ,
"tmid": 4,
"Oa": "",
"flash": false,
"tags": [
"key",
"tag1"
]
}
}
}
We need to add multiple "da" sections based on user input. For example if I give input from a CSV file as 3 then "da" section will repeat 3 times and it will look like as follow:
{
"access_key": "",
"erid": "",
"ch_sms": {
"messages": [
{
"urlsh": false,
"sp": "123",
"vp": 20,
"heid": 1,
"teid": ,
"peid": ,
"tmid": 4,
"msg": {
"txt": "hello"
},
"da": [
{
"number": "97278",
"cc": "IN",
"uid": "uid1",
"tags": [
"key",
"Value"
]
},
{
"number": "97278",
"cc": "IN",
"uid": "uid1",
"tags": [
"key",
"Value"
]
},
{
"number": "97278",
"cc": "IN",
"uid": "uid1",
"tags": [
"key",
"Value"
]
}
]
}
],
"metadata": {
"chver": "1.0",
"cburl": "",
"heid": 1,
"teid": ,
"peid": ,
"tmid": 4,
"Oa": "",
"flash": false,
"tags": [
"key",
"tag1"
]
}
}
}
You can try this,
import org.json.simple.JSONObject
JSONObject data = new JSONObject()
data.put("number", "97278")
data.put("cc", "IN")
data.put("uid", "uid1")
data.put("tags", ["key","Value"])
int da = Integer.parseInt(vars.get("da"))
ArrayList<Object> listData = new ArrayList<Object>()
for(int i=0; i < da; i++)
{
listData.add(data.toString())
}
log.info("da: " + listData )
Result log:
da: [{"cc":"IN","number":"97278","uid":"uid1","tags":["key","Value"]}, {"cc":"IN","number":"97278","uid":"uid1","tags":["key","Value"]}, {"cc":"IN","number":"97278","uid":"uid1","tags":["key","Value"]}]

How to match the no of responses returned with a value displayed in the response body

I have the below response in my postman Body .
{
"count": 3,
"next": null,
"previous": null,
"results": [
{
"id": "c2004ef952894279920417ba8283d26a",
"name": "attrib_inv_bu",
"modified_date": "2020-07-06T02:15:06.839577",
"display_group": {
"id": "attr_dis_267a405426184764816e504b4f0a565b",
"name": "Custom"
},
"api_url": "https://abc.xyz.com/api/v1/attributes/attr_c2004ef952894279920417ba8283d26a/",
"url": "https://abc.xyz.com/manage/matter_attributes/2707/"
},
{
"id": "c2004ef95289422324232a8283d26a",
"name": "attrib_idsdw_sfs",
"modified_date": "2020-07-06T02:11:06.839577",
"display_group": {
"id": "attr_dis_267a4054261dfsf64816e504b4f0a565b",
"name": "Custom"
},
"api_url": "https://abc.xyz.com/api/v1/attributes/attr_c2004ef952894279920417ba8ssd3d26a/",
"url": "https://abc.xyz.com/manage/matter_attributes/2709/"
},
{
"id": "c2004ef9we22e2e8283d26a",
"name": "attrib_iasa_ac",
"modified_date": "2020-07-06T02:17:06.839577",
"display_group": {
"id": "attr_dis_267a405426184764816e5dsds4f0a565b",
"name": "Custom"
},
"api_url": "https://abc.xyz.com/api/v1/attributes/attr_c2004ef952894279920417ba8283sds26a/",
"url": "https://abc.xyz.com/manage/matter_attributes/2708/"
}
]
}
I want to assert whether I have 3 different ids match with the count 3 that is displayed in the body.
res = pm.response.json()
res.count === res.results.length
you can use in expect as :
pm.expect(res.count).to.be.equal(res.results.length)
If you want to verify they are indeed unique:
let resultidlits = []
result.forEach((a) => {
resultidlits.includes(a.id)? null : resultidlits.push(a.id)
})
pm.expect(res.count).to.be.equal(resultidlits.length)

Angular HTTP Res JSON Data print in html

Below is the HTTP get request response. how can i print same in view using *ngFor . iam trying to print single object print like data[0].value1 in console. it returns undefined. inside data array 3 JSON Objects. and some arrays also
Any help much appreciated...
getEmp(){
this.EmpService.getAccounts().subscribe((res : any[])=>{
this.products = JSON.stringify(res);
});
.
{
"data": [
{
"value1": "3546786908765432",
"category": "Clothing (Brand)",
"category_list": [
{
"id": "001",
"name": "Clothing (Brand)"
},
{
"id": "39324324230",
"name": "Boutique Store"
},
{
"id": "12342324324",
"name": "Website"
}
],
"name": "wqeqws",
"id": "qwsqwsq w",
"tasks": [
"ANALYZE",
"ADVERTISE",
"MODERATE",
"CREATE_CONTENT",
"MANAGE"
]
},
{
"value1": "111-9=-09876543",
"category": "Education",
"category_list": [
{
"id": "23456",
"name": "Education"
}
],
"name": "sdcsfcsdfvsd",
"id": "111111111111",
"tasks": [
"ANALYZE",
"ADVERTISE",
"MODERATE",
"CREATE_CONTENT",
"MANAGE"
]
}
],
"paging": {
"cursors": {
"before": "dsfsds",
"after": "sdfsfs"
}
}
}
this.products = res.data;
In your page
< *ngFor="let item of products >

OData filter on array property to return empty array or array

I have a JSON object similar to this one:
{
"customers": [
{
"id": 1,
"name": "bob",
"orders": [{"id": 1, "customerId": 1, "itemId": 6}]
},
{
"id": 2,
"name": "jane",
"orders": [{"id": 2, "customerId": 2, "itemId": 7}]
}
]
}
I'd like to query it like....
...odata/customers?$expand=orders&$filter=orders/any(order: order/itemId eq 6)
...and get...
{
"customers": [
{
"id": 1,
"name": "bob",
"orders": [{"id": 1, "customerId": 1, "itemId": 6}]
},
{
"id": 2,
"name": "jane",
"orders": []
}
]
}
but what I get is...
{
"customers": [
{
"id": 1,
"name": "bob",
"orders": [{"id": 1, "customerId": 1, "itemId": 6}]
}
]
}
I want to get all the customers and I only want the filter to apply to the "orders".
What can I do to get this result?
Just got the same problem!
Solved by filtering my expand by the same condition in any().
applications?$expand=accounts($filter=idUser eq 5)&$filter=accounts/any(account: account/idUser eq 5)
Maybe to late but hope it will help someone else ;)

Return json from two classes in model

I'm trying to return json with two classes in my model
public function both()
{
return $this->belongsToMany(Car::class)->and(Driver::class)->withPivot('type'); // this is wrong
}
the reason I'm trying to get this like that is because in my function
public function check()
{
$user = JWTAuth::parseToken()->authenticate();
$id = $user->id;
$result = User::with('both')->where('id', $id)->get();
return response()->json($result);
}
right now in my model I have this
public function both()
{
return $this->belongsToMany(Car::class)->withPivot('type');
}
public function driver()
{
return $this->belongsToMany(Driver::class)->withPivot('type');
}
But the function returns json that has a different structure when both ends.
My question is can I have the function return a json with the same structure?
like this
[
{
"id": 4,
"name": null,
"phone": "9000",
"size_of_house": null,
"created_at": "2016-12-04 13:55:52",
"updated_at": "2017-03-08 14:03:44",
"deleted_at": null,
"both": [
{
"id": 177,
"name": "NIS",
"created_at": "2016-12-27 10:28:29",
"updated_at": "2016-12-27 10:28:29",
"pic": "http://localhost:8000/images/1482833485.jpg",
"pivot": {
"user_id": 4,
"car_id": 177,
"type": "car"
}
},
"both": [
{
"name": "Abdulaziz Alomhsen",
"age": "30",
"created_at": "2017-02-28 09:36:15",
"updated_at": "2017-03-08 08:46:06",
"status": "جايه",
"pic": "http://localhost:8000/images/1488714520.jpg",
"id": 2,
"pivot": {
"user_id": 4,
"driver_id": 2,
"type": "driver"
}
},