Searching in Array of complex collection of NSDictionary - nsdictionary

I have an array of dictionaries, similar to the following:
{
"categories": [
{
"category_id": "1",
"parent_id": "0",
"en_name": "Category 1",
"sub": [
{
"category_id": "2",
"parent_id": "1",
"en_name": "Abc Pqr",
"businessCount": "5"
},
{
"category_id": "5",
"parent_id": "1",
"en_name": "Xyz Model",
"businessCount": "7"
}
]
},
{
"category_id": "3",
"parent_id": "0",
"en_name": "Category 2",
"sub": [
{
"category_id": "4",
"parent_id": "1",
"en_name": "Test",
"businessCount": "15"
},
{
"category_id": "6",
"parent_id": "1",
"en_name": "Dummy",
"businessCount": "9"
}
]
}
]
}
I want to search for "en_name" of element "sub". Please suggest me ASAP

i am assuming that you have the categories array...that u got from webApi..
then enumerating that categories array...as it is the array of dictionaries....
then sub also an array of dictioanries..enumerating that will give u that u want...
for(NSDictionary *dict in categories)
{
for(NSDictionary * dict1 in [dict objectForKey :#"sub"])
{
NSString *name = [dict1 objectForKey :#"en_name"];
}
}

Related

Filter Nested Array Items CosmosDb

Is it possible to filter array items in CosmosDb? for example I just need customer info and the first pet(in an array)
Current result:
[
{
"CustomerId": "100",
"name": "John",
"lastName": "Doe",
"pets": [
{
"id": "pet01",
"CustomerId": "100",
"name": "1st pet"
},
{
"id": "pet02",
"CustomerId": "100",
"name": "2nd pet"
}
]
}
]
Expected:
[
{
"CustomerId": "100",
"name": "John",
"lastName": "Doe",
"pets": [
{
"id": "pet01",
"CustomerId": "100",
"name": "1st pet"
}
]
}
]
You can use ARRAY_SLICE function.
SQL:
SELECT c.CustomerId,c.name,c.lastName,ARRAY_SLICE(c.pets,0,1) as pets
FROM c
Result:
[
{
"CustomerId": "100",
"name": "John",
"lastName": "Doe",
"pets": [
{
"id": "pet01",
"CustomerId": "100",
"name": "1st pet"
}
]
}
]

mongodb - filter collection by string array contains ""

For the below document, I want to write mongodb query to get the result.
[{
"id": "1",
"class": "class1",
"value": "xyz"
}, {
"id": "2",
"class": "class2",
"value": "abc"
}, {
"id": "3",
"class": "class3",
"value": "123"
}, {
"id": "4",
"class": "class4"
}, {
"id": "5",
"class": "class5",
"value": ""
}
]
The search parameter is an array of values - ["abc", "xyz", ""] and this is
going to look attribute "value"
The output should be below and in this case, the third item in the search array "" is pointing to collection that has "id" - 4 and 5 :
[{
"id": "1",
"class": "class1",
"value": "xyz"
}, {
"id": "2",
"class": "class2",
"value": "abc"
}, {
"id": "4",
"class": "class4"
}, {
"id": "5",
"class": "class5",
"value": ""
}
]
Please assist to provide the mongodb query to get the result like this
Whenever you have blank string you can add null in array, like this,
db.collection.find({
value: {
$in: ["abc", "xyz", "", null]
}
})

Laravel Eloquent API resource is returning empty array when no data

I am using etrepat baum laravel module to store and generate hierarchy. Also using eloquent api resource to customize the json data returned.
Currently empty children are returned if no children available. Is it possible to have children only if available?
Current Result
[{
"id": "1",
"text": "Vegetables",
"children": [{
"id": "2",
"text": "Onion",
"children": []
}, {
"id": "3",
"text": "Tomato",
"children": []
}, {
"id": "4",
"text": "Chilli",
"children": []
}, {
"id": "5",
"text": "Potato",
"children": []
}]
}, {
"id": "6",
"text": "Fruits",
"children": [{
"id": "7",
"text": "Apple",
"children": [{
"id": "12",
"text": "Red Apple",
"children": []
}, {
"id": "13",
"text": "Green Apple",
"children": []
}]
}, {
"id": "8",
"text": "Banana",
"children": [{
"id": "14",
"text": "Red Banana",
"children": []
}, {
"id": "15",
"text": "Yellow Banana",
"children": []
}]
}, {
"id": "9",
"text": "Orange",
"children": []
}, {
"id": "10",
"text": "Papaya",
"children": []
}, {
"id": "11",
"text": "Guava",
"children": []
}]
}]
Expected Result
[{
"id": "1",
"text": "Vegetables",
"children": [{
"id": "2",
"text": "Onion"
}, {
"id": "3",
"text": "Tomato"
}, {
"id": "4",
"text": "Chilli"
}, {
"id": "5",
"text": "Potato"
}]
}, {
"id": "6",
"text": "Fruits",
"children": [{
"id": "7",
"text": "Apple",
"children": [{
"id": "12",
"text": "Red Apple"
}, {
"id": "13",
"text": "Green Apple"
}]
}, {
"id": "8",
"text": "Banana",
"children": [{
"id": "14",
"text": "Red Banana"
}, {
"id": "15",
"text": "Yellow Banana"
}]
}, {
"id": "9",
"text": "Orange"
}, {
"id": "10",
"text": "Papaya"
}, {
"id": "11",
"text": "Guava"
}]
}]
CategoryResource.php
public function toArray($request) {
return [
'id' => (string) $this->id,
'text' => (string) $this->name,
'children' => CategoryResource::collection($this->children)
];
}
CategoryController.php
public function index()
{
CategoryResource::withoutWrapping();
$categories = Category::all()->toHierarchy()->values();
return new CategoryResourceCollection($categories);
}
You can check before adding it to the array and if it's empty don't add it to the returned array :
public function toArray($request) {
$result = [
'id' => (string) $this->id,
'text' => (string) $this->name
];
$child = CategoryResource::collection($this->children);
if (!$child->isEmpty()) {
$result['children'] = $child;
}
return $result;
}
In Laravel 5.7, you could use:
public function toArray($request) {
return [
'id' => (string) $this->id,
'text' => (string) $this->name,
'children' => CategoryResource::collection($this->whenLoaded('children'))
];
}
Correct answer for Laravel 5.7
public function toArray($request) {
return [
'id' => (string) $this->id,
'text' => (string) $this->name,
'children' => CategoryResource::collection($this->when(!$this->children->isEmpty()))
];
}
In this example, if the children is empty, the children key will be removed from the resource response entirely before it is sent to the client.
If you also need this to work correctly with Conditional Relationships (i.e. not trigger loading of each relationship just to check whether it's empty), this is the best way I could figure out (based partly on the other answers here, plus some trial and error)
public function toArray($request)
{
$children = $this->whenLoaded('children');
return [
'id' => $this->id,
'text' => $this->text,
'children' => $this->when(method_exists($children, 'isEmpty') && !$children->isEmpty(), CategoryResource::collection($children))
];
}

Parse combination of json nested objects and arrays

[
{
"Profile": {
"id": "13",
"user_id": "13",
"first_name": "samm",
"profile_image": "13-IMG_169.png",
"where_from": "abro",
"where_live": "simba",
"age": "24",
"profile_type": null,
"company_name": "nick",
"job_title": "developer",
"industry": "software",
"education": "bscs",
"what_you_do": "developement",
"detail_summery": "summary",
"location": null,
"state_id": "1",
"city_id": "84",
"favorite_music_bands": "",
"favorite_teams": "",
"favorite_books": "",
"favorite_movies": null,
"small_intro": "developer"
}
}
],
{
"LookingData": [
{
"user_looking_id": "675",
"looking_id": "1",
"looking_text": "Expand Professional network"
},
{
"user_looking_id": "456",
"looking_id": "2",
"looking_text": "Prospect new business / sales"
},
{
"user_looking_id": "453",
"looking_id": "3",
"looking_text": "Share trade expertise / stories"
},
{
"user_looking_id": "123",
"looking_id": "5",
"looking_text": "Recruitment"
},
{
"user_looking_id": "654",
"looking_id": "6",
"looking_text": "Seeking business partner"
},
{
"user_looking_id": "123",
"looking_id": "7",
"looking_text": "Advise"
}
]
},
{
"MusicData": [
{
"user_music_id": "54",
"music_id": "2",
"music_name": "Country"
}
]
},
{
"SportData": [
{
"user_sport_id": "234",
"sport_id": "4",
"sport_name": "Hockey"
}
]
},
{
"HobbyData": []
},
[],
{
"MovieData": [
{
"user_movie_id": "645",
"movie_id": "6",
"movie_name": "Drama"
}
]
},
[],
{
"CarrerData": [
{
"user_carrer_id": "34",
"carrer_id": "2",
"carrer_name": "Marketing"
},
{
"user_carrer_id": "645",
"carrer_id": "8",
"carrer_name": "Sales"
}
]
}
]
get json Array using this line...
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data //1
options:0
error:&error];
and get profile data using for loop...
for (NSDictionary *obj in [responseDict valueForKey:#"Profile"])
{
model.userProfileObj.userFirstName = [NSString stringWithFormat:#"%#",[obj valueForKey:#"first_name"]];
}
but in this case loop run many of time and assign null value to variables.
after this I want to parse "LookingData" array but I got null value using this code:
NSDictionary *dictUserLooking=[responseDict valueForKey:#"LookingData"];
What you have (assuming the missing outer []) is a JSON array containing elements that are either arrays or "objects" (dictionaries). The "Profile" data is the contained in the first element of the outer-most array, in this fashion:
outermost array -> single element array -> single element dictionary -> dictionary element for "Profile" -> dictionary containing "Profile" values.
You need to "peel the onion", one layer at a time, to access these values.

Problems with parsing JSON feed in app

The following JSON feed contains orders.
How can I retrieve each order, when it doesn't contain a key (canĀ“t use objectForKey)? Between the opening bracket and the first curly bracket there is no key.
[ <-----
{ <-----
"person": {
"address": "My road",
"city": "My City",
"name": "My Name",
},
"orderDate": "30.05.2012 10:27",
"orderId": 10001,
"orderlines": [
{
"unit": {
"unitId": "RA005",
"name": "My Unit name",
},
"id": 10007,
"deliveryInfo": {
"count": 1,
"date": "30.05.2012",
"format": "MY_FORMAT",
},
"sum": 0,
"details": "Testing",
"priority": 3,
}
],
"received": true,
"status": "REGISTERED",
"sumPrice": 0
},
{
"person": {
"address": "My road 2",
"city": "My City 2",
"name": "My Name 2",
},
"orderDate": "30.04.2012 10:27",
"orderId": 10002,
"orderlines": [
{
"unit": {
"unitId": "RA006",
"name": "My Unit name 2",
},
"id": 10008,
"deliveryInfo": {
"count": 2,
"date": "01.06.2012",
"format": "MY_FORMAT",
},
"sum": 0,
"details": "Testing",
"priority": 2,
}
],
"received": true,
"status": "REGISTERED",
"sumPrice": 0
}
]
whenever you see [] (square brackets), it represents array. so use object at index to return the at indexes. one approach is to pass the object into array like
NSMutableArray *arr = (NSMutableArray*) jsonData;
since the root of your json is an array,
NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &e];
if (!jsonArray) {
NSLog(#"Error parsing JSON: %#", e);
} else {
for(NSDictionary *item in jsonArray) {
NSLog(#"Item: %#", item);
}
}