How to iterate over json array - objective-c

I have this json code
{
"apps": [
{
"com.eeenmachine.tinytowers": [
{
"text": "one"
},
{
"text": "two"
}
]
}
]
}
And i am trying to get all the text values.
Trying with this code
NSArray *titles = [jsArray[#"apps"] valueForKey:game];
NSLog(#"[DEBUG]titles %#",titles);
for (id obje in titles){
NSLog(#"[DEBUG]obje %#",obje);
NSLog(#"[DEBUG]obje_class %#",[obje class]);
}
Problem is that I don't get each text value , instead i get all the values.
Output:
obje (
{
text = one;
},
{
text = two;
}
)

Looking at your sample data there are a lot of arrays ([, ]) as well as dictionaries ({, }). For example the value for key apps is an array containing a single dictionary. Your code does not appear to be dealing with these extra levels.
HTH

Related

Flatten complex json using Databricks and ADF

I have following json which I have flattened partially using explode
{
"result":[
{
"employee":[
{
"employeeType":{
"name":"[empName]",
"displayName":"theName"
},
"groupValue":"value1"
},
{
"employeeType":{
"name":"#bossName#",
"displayName":"theBoss"
},
"groupValue":[
{
"id":"1",
"type":{
"name":"firstBoss",
"displayName":"CEO"
},
"name":"Martha"
},
{
"id":"2",
"type":{
"name":"secondBoss",
"displayName":"cto"
},
"name":"Alex"
}
]
}
]
}
]
}
I need to get following fields:
employeeType.name
groupValue
I am able to extract those fields and value. But, if name value starts with # like in "name":"#bossName#", I am getting groupValue as string from which I need to extract id and name.
"groupValue":[
{
"id":"1",
"type":{
"name":"firstBoss",
"displayName":"CEO"
},
"name":"Martha"
},
{
"id":"2",
"type":{
"name":"secondBoss",
"displayName":"cto"
},
"name":"Alex"
}
]
How to convert this string to json and get the values.
My code so far:
from pyspark.sql.functions import *
db_flat = (df.select(explode("result.employee").alias("emp"))
.withColumn("emp_name", col(emp.employeeType.name))
.withColumn("emp_val",col("emp.groupValue")).drop("emp"))
How can I extract groupValue from db_flat and get id and name from it. Maybe use python panda library.
Since you see they won't be dynamic. You can traverse through the json while mapping like as below. Just identify record and array, specify index [i] as needed.
Example:
id --> $['employee'][1]['groupValue'][0]['id']
name --> $['employee'][1]['groupValue'][0]['type']['name']

'match each' one element in the array [duplicate]

This question already has an answer here:
Using match each contains for json array items assertion
(1 answer)
Closed 1 year ago.
My question about selective asserting with 'match each'.
Below is a sample json body:
* def data =
"""
{
"companies": [
{
"companyDetails": {
"name": "companyName",
"location": {
"address": "companyAddress",
"street": "companyStreet"
}
}
},
{
"companyDetails": {
"name": "companyName",
"location": {
"address": "companyAddress",
"street": "companyStreet"
}
}
}
]
}
"""
How can we assert whether each 'companyDetails' object in the response contains a certain element, e.g. name only?
* match each data.companies contains { companyDetails: { name: '#notnull' } }
When I use above step, this returns below error:
$[0] | actual does not contain expected | all key-values did not match, expected has un-matched keys
So, is there any way we can assert only one field inside this object? For example, assert whether each companyDetails object contains name, but ignores the other elements such as location? Thanks
This will work:
* match each data.companies == { companyDetails: { name: '#notnull', location: '#notnull' } }
This will also work:
* match data.companies contains deep { companyDetails: { name: '#notnull' } }
Sometimes it is better to transform the JSON before a match:
* match each data..companyDetails contains { name: '#notnull' }

Query Mongo collection for all documents with empty array nested in an array

I have documents that look like this in a collection called movies:
{
"_id" : ObjectId("51c272623021490007000001"),
"movies": [
{
"name": "Booty Call"
"play_times": []
},
{
"name": "Bulletproof"
"play_times": [{...},{...}]
}
]
}
I would like to query for documents where "play_times" is not empty or null. Basically I only care about movies with play times.
If you want to query for separate array elements within documents, that is not possible AFAIK. If you want to get documents which have non-empty play_times, then use $size operator:
movies.play_times : { $size : { $gt : 0} }
To check if field exists, there is an $exists operator.

.getjson from external domain. Print from the Array

I'm currently getting 2 arrays from the following get statement.
$.getJSON("http://domain.com?callback=?", function(data){
$("#results").html(JSON.stringify(data));
I'm appending the results in a div with the id results.
All that is doing is just displaying the array. I need to pull certain objects from the array but I can't do that because I am just printing the whole array. How do I get specific information from that array?
Here is some of the data that is being appended to the DIV.
{"me":[{"player":{"high_score":110345,"rank":2}}],"all":[{"player":{"#score := s.score":110345,"avatar":"http://profile.ak.fbcdn.net/hprofile-ak-snc6/195312_789328764_1121893995_q.jpg","guid":"FE4EC535-B74F-4B68-8F4D-2CA0EBC28FAF","name":"Charles Chase","rank":1,"score":110345}},
Thanks!
Provided that the following is your returned JSON data
{
"me": [
{
"player": {
"high_score": 110345,
"rank": 2
}
}
],
"all": [
{
"player": {
"#score := s.score": 110345,
"avatar": "http://profile.ak.fbcdn.net/hprofile-ak-snc6/195312_789328764_1121893995_q.jpg",
"guid": "FE4EC535-B74F-4B68-8F4D-2CA0EBC28FAF",
"name": "Charles Chase",
"rank": 1,
"score": 110345
}
}
]
};
You can use the following to grab the values from the JSON object
CODE
$.getJSON("http://domain.com?callback=?", function(data){
alert(data.me[0].player.high_score);
alert(data.all[0].player.name);
));
Note me and all is returned as array. I hope it helps.

How to parse the geolocation json from google for lng and lat in objective-c

I'm creating a web request to pull lng and lat data based on zip (using the following url)
http://maps.google.com/maps/geo?q=50266
Doing so I get back the following json
{
"name": "50266",
"Status": {
"code": 200,
"request": "geocode"
},
"Placemark": [ {
"id": "p1",
"address": "West Des Moines, IA 50266, USA",
"AddressDetails": {
"Accuracy" : 5,
"Country" : {
"AdministrativeArea" : {
"AdministrativeAreaName" : "IA",
"Locality" : {
"LocalityName" : "West Des Moines",
"PostalCode" : {
"PostalCodeNumber" : "50266"
}
}
},
"CountryName" : "USA",
"CountryNameCode" : "US"
}
},
"ExtendedData": {
"LatLonBox": {
"north": 41.6005010,
"south": 41.5254700,
"east": -93.7347000,
"west": -93.8435030
}
},
"Point": {
"coordinates": [ -93.7353858, 41.5998115, 0 ]
}
} ]
}
What I'm trying to pull from this is simply the coordinates section. Here is my current attempt that throws and exception when I hit the last line shown below
//after I get the response I turn it into json and this is working
NSArray* json = [items JSONValue];
NSString* coords = [json objectForKey:#"Placemark.Point.coordinates"];
What I'm I missing here for a quick pull of the coordinates?
The overall value return is not an array, it's a dictionary. Note that the first character is {. If it were an array, it would be [.
NSDictionary * json = [string JSONValue];
Now, you want the stuff under the "Placemarks" key. Note that this returns an array, since the character after the "Placemarks" key (and colon) is a [.
NSArray * placemarks = [json objectForKey:#"Placemark"];
From this array, you want the first element, which is another NSDictionary:
NSDictionary *firstPlacemark = [placemarks objectAtIndex:0];
From this dictionary, you want the dictionary under the key "Point":
NSDictionary *point = [firstPlacemark objectForKey:#"Point"];
From this dictionary, you want the array under the key "coordinates":
NSArray * coordinates = [point objectForKey:#"coordinates"];
At this point, you have the array that contains 3 NSNumber objects. Voilá!
For the advanced user, you can probably use key-value coding to get at it:
NSArray * coordinates = [json valueForKeyPath:#"Placemark[0].Point.coordinates"];
Though I wouldn't recommend that unless you clearly understand what's going on there.
That does not work. Never mind! :)
Keep in mind that "[ -93.7353858, 41.5998115, 0 ]" is not a string