Parse combination of json nested objects and arrays - iphone

[
{
"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.

Related

powershell - filter nested object which contains data from list

I am looking for a way to filter nested objects with PowerShell. Let's say I have a list that contains UPN addresses.
$UPN = "user1#xyz.com", "user5#xyz.com"
In the below object converted to JSON, I want to get only those objects where "Attr1" within whole object contains any address from the above list.
[
{
"Id": "1",
"Name": "XYZ",
"Attr1": [
{
"Id": "1",
"UPN": "user1#xyz.com"
},
{
"Id": "2",
"UPN": "user2#xyz.com"
},
{
"Id": "3",
"UPN": "user3#xyz.com"
}
],
"Attr2": [
{
"Id": "1",
[...]
}
],
"Attr3": [
{
"Id": "1",
[...]
},
{
"Id": "2",
[...]
}
]
},
{
"Id": "2",
"Name": "XYZ",
"Attr1": [
{
"Id": "1",
"UPN": "user2#xyz.com"
}
],
"Attr2": [
],
"Attr3": [
{
"Id": "1",
[...]
}
]
}
]
How to filter such an object? The standard method to not work for me (I am not getting the desired result, losing data):
$CollectionOfObjects | Where-Object {$_.Attr1.UPN -in $UPN}
How to handle this?
Regards!
You need to nest Where-Object calls to get the desired result, or, for better performance with in-memory collections, .Where() method calls:
$CollectionOfObjects.Where({ $_.Attr1.Where({ $_.UPN -in $UPN }, 'First') })
Tip of the hat to Manuel Batsching for his help.
Note: The 'First' argument isn't strictly necessary, but can improve performance, as it stops enumerating the elements of the array in .Attr1 as soon as a match is found.
As an aside: Where-Object has no equivalent feature - enumeration invariably continues.
GitHub issue #13834 suggests bringing all the extra features .Where() supports to Where-Object too.
A self-contained example with simplified data:
$UPN = 'user1#xyz.com', 'user6#xyz.com'
$CollectionOfObjects = ConvertFrom-Json #'
[
{
"Id": "1",
"Name": "XYZ1",
"Attr1": [
{
"Id": "1",
"UPN": "user1#xyz.com"
},
{
"Id": "2",
"UPN": "user2#xyz.com"
},
{
"Id": "3",
"UPN": "user3#xyz.com"
}
],
"Attr2": [ ],
"Attr3": [ ]
},
{
"Id": "2",
"Name": "XYZ2",
"Attr1": [
{
"Id": "1",
"UPN": "user4#xyz.com"
},
{
"Id": "2",
"UPN": "user5#xyz.com"
}
],
"Attr2": [ ],
"Attr3": [ ]
},
{
"Id": "3",
"Name": "XYZ3",
"Attr1": [
{
"Id": "1",
"UPN": "user6#xyz.com"
}
],
"Attr2": [ ],
"Attr3": [ ]
}
]
'#
$CollectionOfObjects.Where({ $_.Attr1.Where({ $_.UPN -in $UPN }, 'First') })
Output, showing that the two objects of interest were successfully identified:
Id : 1
Name : XYZ1
Attr1 : {#{Id=1; UPN=user1#xyz.com}, #{Id=2; UPN=user2#xyz.com}, #{Id=3; UPN=user3#xyz.com}}
Attr2 : {}
Attr3 : {}
Id : 3
Name : XYZ3
Attr1 : {#{Id=1; UPN=user6#xyz.com}}
Attr2 : {}
Attr3 : {}

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]
}
})

Clean docs in collection by comparing them with reference

I have a lot of complex JSON objects which are placed in the collection. For example:
{
"name": "Mike",
"price": "444",
"distance": 881,
"someFiend": 123,
"lots": [
{
"aa": "111",
"bb": "222"
},
{
"xx": "000"
}
],
"apps": [
{
"app": 1
},
{
"app": 2
}
]
}
I only want to project only those fields which are present in the following reference document:
{
"name": "",
"price": "",
"lots": [
{
"aa": "",
"bb": ""
}
]
}
Expected output:
{
"name": "Mike",
"price": "444",
"lots": [
{
"aa": "111",
"bb": "222"
}
]
}
Is there any way to iterate all documents in the collection and then filter out fields that are not present in the reference doc?

Extracting unique elements from array within different arrays

I have a collection with this structure:
{
"name": "1",
"array1": [
{
"arrayname1A" : "A",
"array2": [
{ "value": "1" },
{ "value": "3" }
]
},
{
"arrayname1B": "B",
"array2": [
{ "value": "5" },
]
}
]
},
{
"name": "2",
"array1": [
{
"arrayname1A": "A",
"array2": [
{ "value": "1" },
{ "value": "7" }
]
}
]
}
How can I extract the unique "value" from every different array2? The end result I am looking for would be something like "1","3","5","7" with no repeated values.
Simply use the distinct() method and the dot notation to access the "value" field.
db.collection.distinct('array1.array2.value')
which yields:
[ "1", "3", "5", "7" ]

Searching in Array of complex collection of 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"];
}
}