Parse JSON using Swift / SwiftyJSON - json

I've followed several tutorials on this, and as far as I can tell what I'm doing should be working. I have the following json response from an api call
{
"Id": "1",
"Name": "Test User",
"Email": "test#email.com",
"ProfileImage": null,
"IsAdmin": true,
"TakesJobs": false,
"IsLocationUser": false,
"IsCompanyAdmin": true,
"LocationUsers": [],
"CompanyUsers": [{
"CompanyName": "Test Company",
"Id": 6,
"CompanyId": 5,
"UserId": "1",
"Admin": true,
"TakesJobs": false,
"UserName": null,
"UserEmail": null,
"AssignedJobs": null
}]
}
Essentially I just want to check if the Id value is blank or not. Here is the code I'm using
The res return type is JSON
ApiConnector.sharedInstance.login(emailText.text!, password: passwordText.text!) { (res) in
if let id = res["Id"] as? String {
if id != "" {
}
else
{
}
}
}
I get a warning that says Cast from 'JSON' to unrelated type 'String' always fails.
What do I need to change to see the value of Id?
This is the code from the ApiConnector class
func login(username: String, password: String, onCompletion: (JSON) -> Void) {
let route = baseURL + "auth?Email=" + username + "&Password=" + password
makeHTTPPostRequest(route, body: ["Email":username, "Password": password], onCompletion: { json, err in
onCompletion(json as JSON)
})
}

I think you want something like this:
ApiConnector.sharedInstance.login(emailText.text!, password: passwordText.text!) { (res) in
if let id = res["Id"].string {
// Do something.
}
}

What library are you using for handle json? If swift json you can do something like
res["id"]?.string
If you don't say anything on the type of "res" we can't answer you.

Related

How to define a model class in angular8(TypeScript) or above for nested JSON response coming from SpringBoot backend code

I want to define a model class in angular8 for the below nested JSON response.
JSON Response:
{
"message": "All Users fetched",
"status": "Success",
"data": [
{
"userid": "abc22",
"password": "abc22",
"role": 1,
"sessionid": "AWS1",
"sessiontype": "RC01",
"status": null
}
]
}
I am not sure how to do it for the nested JSON response. Could someone help me with it.
Lets assume that all the datatypes of the above fields are String. Thanks in advance
I assume you often have the response with message and status fields, but that the structure of the data field is different for different requests. In that case, I'd recommend you do a generic interface like this:
export interface ApiResponse<T> {
message: string,
status: "Success" | "Error", // Add more possible status responses if needed
data: T
}
The user model would look something like this:
export interface User {
userId: string,
password: string,
role: number,
sessionid: string
sessiontype: string,
status: string // Or whatever type this should be
}
Now you can create a type alias like this:
type UserResponse = ApiResponse<User[]>
And use it with Angular's http service like this:
this.http.get<UserResponse>('/api/endpoint/to/userlist')
.subscribe(resp => {
console.log(resp.data) // This should print out the list of users
})
*Edit*
It should be noted that I'm using interfaces instead of classes, because that's usually what you want to do in angular. Interfaces don't generate any javascript code when compiled, they just help you with the typechecking during development. There will be no checking of the data structure during runtime, so if your response doesn't actually look like that you may run into problems.
You could do something like this:
class User {
userId: string;
password: string;
role: number;
sessionid: string;
sessiontype: string;
status: string;
}
class Response {
message: string;
status: string;
data: User[];
}
const jsonData = {
"message": "All Users fetched",
"status": "Success",
"data": [
{
"userid": "abc22",
"password": "abc22",
"role": 1,
"sessionid": "AWS1",
"sessiontype": "RC01",
"status": null
}
]
}
const response = new Response();
response.message = jsonData.message;
response.status = jsonData.status;
response.data = jsonData.data.map(userData => {
const user = new User();
user.userId = userData.userId;
});

Casting dictionary of sets to JSON Object

I'm trying to build request body like this form:
{
"user": {
"id": 1,
"id": 2,
"id": 4
}
}
My first idea was built json from string and cast it to dictionary String and Any, but this solution have a issue. Some "id" are missing on casting by JSONSerialization step.
I tried to use:
var dictionary: [String : Any] = ["name" : "John"]()
var selectedIDs = Set<NSDictionary>()
// Adding values to selectedIDs set
let userIDDict = ["id" : id] as NSDictionary
selectedIDs.insert(userIDDict)
dictionary.updateValue(selectedIDs, forKey: "user")
But it cannot be cast by JSONSerialization (Invalid type in JSON write).
How can i resolve problem, which i'm facing?
Creating this request is not a problem; it's just not proper JSON, so you shouldn't try to use JSONSerialization. It's just a string, and so you can create that string:
let idKeyValues = ids.map { "\"id\": \($0)" }.joined(separator: ",\n ")
let request = """
{
"user": {
\(idKeyValues)
}
}
"""
===>
{
"user": {
"id": 1,
"id": 2,
"id": 4
}
}
The proper way to express this in JSON would be:
{
"user": {
"ids": [1, 2, 4]
}
}
With that, a Codable implementation should be very straightforward.

Parse data using Alamofire and SwiftyJson

My JSON -
"documents": {
"driver": [
{
"id": 1,
"name": "Driving Licence",
"type": "DRIVER",
"provider_document": {
"id": 9,
"provider_id": 165,
"document_id": "1",
"url": "https://boucompany.com/storage/provider/documents/b92cf551a62b6b8c183997b41b9543c6.jpeg",
"unique_id": null,
"status": "ACTIVE",
"expires_at": null,
"created_at": "2019-04-26 19:05:58",
"updated_at": "2019-04-27 06:37:56"
}
},
{
"id": 2,
"name": "Bank Passbook",
"type": "DRIVER",
"provider_document": null
},
{
"id": 3,
"name": "Joining Form",
"type": "DRIVER",
"provider_document": null
},
{
"id": 4,
"name": "Work Permit",
"type": "DRIVER",
"provider_document": null
},
{
"id": 8,
"name": "Test Document",
"type": "DRIVER",
"provider_document": null
},
{
"id": 9,
"name": "NID Card",
"type": "DRIVER",
"provider_document": null
},
{
"id": 10,
"name": "MatrĂ­cula",
"type": "DRIVER",
"provider_document": null
}
],
I want to parse the url name.I have used Alamofire and SwiftyJson in my project. So far i have tried -
self.documentsDriver = json["documents"]["driver"][0]["provider_document"]["url"].stringValue
How can i print the value or "url" using swiftyjson
You can use Encodable to parse this response as below,
struct Response: Codable {
let documents: Documents
}
struct Documents: Codable {
let driver: [Driver]
}
struct Driver: Codable {
let id: Int
let name, type: String
let providerDocument: ProviderDocument?
enum CodingKeys: String, CodingKey {
case id, name, type
case providerDocument = "provider_document"
}
}
struct ProviderDocument: Codable {
let id, providerID: Int
let documentID: String
let url: String
let status: String
let createdAt, updatedAt: String
enum CodingKeys: String, CodingKey {
case id
case providerID = "provider_id"
case documentID = "document_id"
case url
case status
case createdAt = "created_at"
case updatedAt = "updated_at"
}
}
To parse the response,
let jsonData = Data() // Your API response data.
let response = try? JSONDecoder().decode(Response.self, from: jsonData)
response.documents.driver.forEach { driver in
print(driver.providerDocument?.url)
}
Parse JSON data using SwiftyJSON
func convertJSONToDriverModel(json: JSON) {
if let driver = json["documents"]["driver"].array {
for driverJson in driver {
let driverObj = convertToDriverJSONModel(json: driverJson)
print(driverObj)
}
}
}
func convertToDriverJSONModel(json: JSON) {
let name = json["name"].string ?? ""
if let providerDetails = json["provider_document"].dictionary {
let url = convertToProductDetailsJSONModel(json: JSON(providerDetails))
print("URL is: \(url)")
}
}
// Method to parse data inside provider_document (Here I have parsed only url)
func convertToProviderDocumentJSONModel(json: JSON) -> String {
let url = json["url"].string ?? ""
return url
}

How to serialize / represent database objects as JSON returned by API in node

I'm using express.js and sequelize.js to build an API. Once I retrieved an object from the DB using sequelize, I want to
filter out object attributes (e.g. retrieve the user, but don't render the User's password hash to the returned JSON)
add new object attributes
before I return it from the API as JSON.
Similar to what these Rails libraries do:
Roar
Grape Entity
What's the most common framework to do that in node? Or do sequelize.js / express.js contain functionality to do that?
UPDATE
Ok, there is a basic example, passport.js gets the authenticated user's object from the DB and attaches it to req.user;
router.get('/me/data',
passport.authenticate('bearer', { session: false }),
function(req, res) {
res.status(200).send(req.user);
}
);
That would return the following JSON response:
{
"id": 24,
"first_name": "John",
"last_name": "Doe",
"email": "mymail#example.com",
"password": "8d23cb9c4827bc06bb30ac47c06af0efbdbeb575001ab7de5387da4085f7184a381335c0f04b45f4a40e5a7042d47ae1e2d29d28fd5be1d534f09ba3db04e8ca",
"updatedAt": "2016-01-25T09:19:07.422Z",
"createdAt": "2016-01-25T09:19:07.422Z",
"data": null
}
But I want to return something like this:
{
"id": 24,
"full_name": "John Doe",
"email": "mymail#example.com",
"data": null
}
And not just for this one case, but in any case a user object is rendered.
The simplest solution would be to edit req.user before sending it:
function
render (user) {
callback({
"id": user.id,
"full_name": user.first_name + ' ' + user.last_name,
"email": user.email,
"data": null
});
}
router.get('/me/data',
passport.authenticate('bearer', { session: false }),
function(req, res) {
render(req.user, function(user) {
res.status(200).send(user);
});
}
);
This module will helper you with Grape Entity manner:
https://github.com/baijijs/entity
For example:
const Entity = require('baiji-entity');
const userEntity = new Entity({
id: true,
fullname: true,
email: true,
data: true
});
userEntity.parse(req.user);
// And the parsed result will be as below
{
"id": 24,
"full_name": "John Doe",
"email": "mymail#example.com",
"data": null
}
Hope this can help you.
Ok so you would like to chop of some of the fields? You could chop it of before the server is sending the response. For example with a module like this https://www.npmjs.com/package/json-chop or if you use mongo db you can hide the fields like described here https://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/
Without knowing more details about the setup, the following (untested) code has the potential to work. You could specify the attributes field in your query against the model in order to filter out results (based on certain conditions):
router.get('me/data',function(req, res) {
db.UserModel.find({
attributes:['id','first_name','last_name','email','data'],
where: { id: req.user.id}, //put in here whatever you are using to get the info from the database
})
.then (function(user) {
var formatUser = {
id: user.id,
full_name: user.first_name + ' ' + user.last_name,
email: user.email,
data: user.data,
};
res.json(formatUser);
}
});
See the following docs for more information:
http://docs.sequelizejs.com/en/latest/docs/models-usage/
http://docs.sequelizejs.com/en/latest/docs/querying/

Ember Data One to One Relationship Record Creation Fails

I receive the following error when I use Ember Data to create records from a JSON response. What gives? I am following what the docs state.
Uncaught Error: Assertion Failed: Ember Data expected a number or string to represent the record(s) in the `user` relationship instead it found an object. If this is a polymorphic relationship please specify a `type` key. If this is an embedded relationship please include the `DS.EmbeddedRecordsMixin` and specify the `user` property in your serializer's attrs object.
JSON being parsed:
[
{
"id": 76,
"title": "Title",
"shipped": 0,
"date": "2015-05-21T05:00:00.000Z",
"user": {
"firstName": "First Name",
"lastName": "Last Name",
"email": "hellothere#gmail.com",
"id": 1
}
}
]
Shipment Model:
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
user: DS.belongsTo('user', { async: false })
});
Route:
import Ember from 'ember';
export default Ember.Route.extend({
beforeModel: function() {
if(!localStorage.accessToken) {
this.transitionTo('login');
}
},
model: function() {
var shipmentObjects = [];
var App = this;
Ember.$.getJSON('http://localhost:1337/subscription/1/shipments/upcoming', function(shipments) {
shipments.forEach(function(data) {
var shipment = App.store.push('shipment', data);
shipmentObjects.pushObject(shipment);
});
});
return shipmentObjects;
}
});
You can create a custom serializer, if you can't modify your json response and manage to arrange data in other way
App.MODELNAMESerializer = DS.ActiveModelSerializer.extend({
extract: function(store, type, payload, id, requestType){
var shipments = [];
//CREATE A NEW PAYLOAD THAT EMBER CAN READ
var _payload = { };
return this._super(store, type, _payload, id, requestType);
}
});
Your json should look something like this
{
shipments: [
{
"id": 76,
"title": "Title",
"shipped": 0,
"date": "2015-05-21T05:00:00.000Z",
"user_id": 1,
}
],
"users": [
{
"firstName": "First Name",
"lastName": "Last Name",
"email": "hellothere#gmail.com",
"id": 1
}
]
}
Read the error message. It could hardly be clearer. By default, Ember Data expects an association to be represented by an ID. If the association is instead embedded, you must tell Ember Data that. You'll need something like:
// serializers/shipment.js
export default ApplicationSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
user: { embedded: 'always' }
}
});
And remove the {async: false}, since the data is embedded right there.
See http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html.