Grouping from Json data, which contain array of dictionaries in Swift 4 - ios

I've local json file, which contains array of dictionaries. I want to group based on key name from below json. Means same name in one group. Please tell me how can I achieve that. Thank you.
Json Data:
[
{
"name": "Abc",
"number": 123,
"marks": 78
},
{
"name": "xyz",
"number": 456,
"marks": 50
},
{
"name": "Abc",
"number": 789,
"marks": 78
}
]
Code:
code and error message

init(grouping:by:)
You should return value of name key in the closure.
let arr = [ [ "name": "Abc", "number": 123, "marks": 78 ], [ "name": "xyz", "number": 456, "marks": 50 ], [ "name": "Abc", "number": 789, "marks": 78 ] ]
let dict = Dictionary(grouping: arr) { $0["name"] as! String }
print(dict)
//["Abc": [["name": "Abc", "number": 123, "marks": 78], ["name": "Abc", "number": 789, "marks": 78]], "xyz": [["name": "xyz", "number": 456, "marks": 50]]]

Related

Schema definition in Swagger

I'm very new to OpenAPI and I'm using http://editor.swagger.io to design an API.
I'm stuck in Schema with a JSON looking like following
{
"CORRELATION_ID": "10",
"CONTROL":
{
"DAS_IS_RECIPIENT": "123",
"DOCTPYE": "ert",
"PROCESS_INDICATOR": "nord"
},
"HEADER":
{
"ID": "456",
"INVOICE_NUMBER": "678",
"DMS_DOC_ID": "876",
"INVOICE_DATE": "10082020"
},
"ITEMS": [
{
"SHORT_TEXT": "123",
"LSTAR": 0,
"QUANTITY": "23"
},
{
"SHORT_TEXT": "456",
"LSTAR": 234,
"QUANTITY": "21"
}
],
"DEBITOR":
{
"ID": "444",
"FIRSTNAME": "nick",
"LASTNAME": "cantre"
},
"CREDITOR":
{
"ID": "454",
"FIRSTNAME": "ava",
"LASTNAME": "pierre"
}
}
How to create a schema according to this JSON structure?

How to generate the pyarrow schema for the dynamic values

I am trying to write a parquest schema for my json message that needs to be written back to a GCS bucket using apache_beam
My json is like below:
data = {
"name": "user_1",
"result": [
{
"subject": "maths",
"marks": 99
},
{
"subject": "science",
"marks": 76
}
],
"section": "A"
}
result array in the above example can have many value minimum is 1.
This is the schema you need:
import pyarrow as pa
schema = pa.schema(
[
pa.field("name", pa.string()),
pa.field(
"result",
pa.list_(
pa.struct(
[
pa.field("subject", pa.string()),
pa.field("marks", pa.int32()),
]
)
),
),
pa.field("section", pa.string()),
]
)
If you have a file containing one record per line:
{"name": "user_1", "result": [{"subject": "maths", "marks": 99}, {"subject": "science", "marks": 76}], "section": "A"}
{"name": "user_2", "result": [{"subject": "maths", "marks": 10}, {"subject": "science", "marks": 75}], "section": "A"}
You can load it using:
from pyarrow import json as pa_json
table = pa_json.read_json('filename.json', parse_options=pa_json.ParseOptions(explicit_schema=schema))

How to post the nested dictionary as JSON body using swift

{
"data": [{
"_id": "1558946471217677c352281",
"title": "ssds",
"user_id": "",
"user_name": "",
"checklist": {
"id": "5ccad234e47ab8565a45b6a2",
"name": "task edited 2"
},
"idc": "5ccad234e47ab8565a45b6a2",
"completed": 0,
"created": "2019-05-27T22:41:11+14:00",
"tps": 1,
"tpe": 0
}],
"tz": "2d626e1289dafb70b0e890eb97bf5c4910b0a47edad9624b5973cdb244abe8fed5e6",
"uID": "7c24d0c8c6034017556946c418bbf0b182ba6a0a62230a860c3319c8f137c06920da465df3d789d1"
}
You can prepare your dictionary format as follows:
let checklist = [
"id": "5ccad234e47ab8565a45b6a2",
"name": "task edited 2"
]
let data = [
[
"_id": "1558946471217677c352281",
"title": "ssds",
"user_id": "",
"user_name": "",
"checklist": "",
"idc": "5ccad234e47ab8565a45b6a2",
"completed": 0,
"created": "2019-05-27T22:41:11+14:00",
"tps": 1,
"tpe": 0
],
[
"_id": "1558946471217677c352281",
"title": "Abcd",
"user_id": "",
"user_name": "",
"checklist": "",
"idc": "5ccad234e47ab8565a45b6a2",
"completed": 0,
"created": "2019-05-27T22:41:11+15:00",
"tps": 1,
"tpe": 0
]
] as [[String : Any]]
let paramDict = [
"data": [data],
"tz":"2d626e1289dafb70b0e890eb97bf5c4910b0a47edad9624b5973cdb244abe8fed5e6",
"uID":"7c24d0c8c6034017556946c418bbf0b182ba6a0a62230a860c3319c8f137c06920da465df3d789d1"
] as [String : Any]

Swift Alamofire get data to equal

Hi Im get json data with Alamofire and get like this:
{
"prices": [
{
"id": 1,
"value": 1.327,
"stationId": 24,
"type": 0,
"score": 5
},
{
"id": 2,
"value": 1.319,
"stationId": 25,
"type": 0,
"score": 4
},...],
"stations": [
{
"id": 24,
"name": "...",
"address": "...",
"brandId": 1,
"location": ".."
},
{
"id": 25,
"name": "..",
"address": "..",
"brandId": 1,
"location": ".."
},..],
"brands": [
{
"id": 6,
"name": "AGIP"
},
{
"id": 2,
"name": "EKO"
}, ...]
How can I get all data with "type": 0
And then when get all data with type compare id from prices stations and brands and put to array or dictionary
You could map your response using this pod AlamofireObjectMapper and then you can filter this object using for cycle.

create a dictionary contains Array and Dictionary in swift 4

I just want to create an API JSON Structure. The following are the post body keys and objects. Are there any methods like object with keys and values similar to Objective C in Swift 4?
{
"name": "switch 1",
"type": "Switch",
"gatewayId":515,
"serialKey": "98:07:2D:48:D3:56",
"noOfGangs": 4,
"equipments": [
{
"name": "light",
"type": "Light",
"port": "1"
},
{
"name": "television",
"type": "Television",
"port": "3"
}
]
}
You can create the dictionary literally by annotating the type and replace the curly braces with square brackets
let dict : [String:Any] = ["name": "switch 1", "type": "Switch", "gatewayId":515, "serialKey": "98:07:2D:48:D3:56", "noOfGangs": 4, "equipments": [[ "name": "light", "type": "Light", "port": "1" ], ["name": "television", "type": "Television", "port": "3" ]]]
Or build it:
var dict : [String:Any] = ["name": "switch 1", "type": "Switch", "gatewayId":515, "serialKey": "98:07:2D:48:D3:56", "noOfGangs": 4]
var equipments = [[String:String]]()
equipments.append(["name": "light", "type": "Light", "port": "1" ])
equipments.append(["name": "television", "type": "Television", "port": "3" ])
dict["equipments"] = equipments
how to create Dictionary
var populatedDictionary = ["key1": "value1", "key2": "value2"]
this how to create Array
var shoppingList: [String] = ["Eggs", "Milk"]
you can create Dictionary by this type
var dictionary = [Int:String]()
dictionary.updateValue(value: "Hola", forKey: 1)
dictionary.updateValue(value: "Hello", forKey: 2)
dictionary.updateValue(value: "Aloha", forKey: 3)
// anather example
var dict = [ 1 : "abc", 2 : "cde"]
dict.updateValue("efg", forKey: 3)
print(dict)
your JSON
let dic :[String:Any] = ["name": "switch 1", "type": "Switch", "gatewayId":515, "serialKey": "98:07:2D:48:D3:56", "noOfGangs": 4, "equipments": [ [ "name": "light", "type": "Light", "port": "1" ],
[ "name": "television", "type": "Television", "port": "3" ] ] ]

Resources