I have the JSON. How can I fetch student_id, first_name, and second_name? - swift3

This is JSON I parsed from a URL. Now I want student_id, first_name, and second_name from this JSON. How can i get that?
[{
"timestamp": "2017-06-29 05:37:23",
"student_id": "6",
"first_name": "SACHIN",
"second_name": "SINGH",
"sex": "male",
"student_course": "TECH-NON-TECH",
"email_id": "sachinsinghchahar9058841073#gmail.com",
"password": "kbmss#10002",
"phone": "8879505608",
"login_id": "kbmss",
"status": "2",
"roll_no": "",
"payment": "cash",
"amount_description": "cash",
"amount": "8500",
"refrence_id": "",
"child_id": "0",
"plan": "advance",
"valid_upto": "2017-12-14"
}]

switch response.result{
case .success(let data) :
let json = JSON(data)
For more explanation you can go to this link.
How to get values of an array of dictionary from alamofire response using swift3

Related

In DRF, how to get dictionary format in JSON response rather than List of dcitionaries

How to get dictionary format in JSON response rather than List of dictionaries. Right now am getting response like list of dictionaries and I want to get response without list.
#Response I get for example
[
{
"id": 40,
"full_name": "Don Ser 1",
"email": "Donny#sfds.com",
"phone": 12345,
"address_line_1": "sdsdsdsd",
"address_line_2": "dsdsdsd",
},
{
"id": 41,
"full_name": "Don Ser 2",
"email": "Donny#sfds.com",
"phone": 121356456,
"address_line_1": "sdsdsdsd",
"address_line_2": "dsdsdsd",
}
]
#Response I Like to get is without List.
[
{
"id": 40,
"full_name": "Don Ser 1",
"email": "Donny#sfds.com",
"phone": 12345,
"address_line_1": "sdsdsdsd",
"address_line_2": "dsdsdsd",
},
{
"id": 41,
"full_name": "Don Ser 2",
"email": "Donny#sfds.com",
"phone": 121356456,
"address_line_1": "sdsdsdsd",
"address_line_2": "dsdsdsd",
}
}
How do I achieve this?
Additionally I like to know why it returned it in List rather than dictionary?
If you want to return a dict you must define a structure, e.g.
{'results': [
{
"id": 40,
"full_name": "Don Ser 1",
"email": "Donny#sfds.com",
"phone": 12345,
"address_line_1": "sdsdsdsd",
"address_line_2": "dsdsdsd",
},
{
"id": 41,
"full_name": "Don Ser 2",
"email": "Donny#sfds.com",
"phone": 121356456,
"address_line_1": "sdsdsdsd",
"address_line_2": "dsdsdsd",
}
]}
But just return a dict without a key is not possible.
If you are returning your response in DRF with serializer.data the simplest way to achieve that is:
return Response(serializer.data[0], status=status.HTTP_200_OK)
If you want to include the whole dictionary in another dictionary you'd have to give it a key so it would something like this:
return Response({"results":serializer.data[0]}, status=status.HTTP_200_OK)
Otherwise if you are using it on a simple item you can just specify it the way you want i.e. :
serializer = Serializer(data=request.data)
if serializer.is_valid():
device = serializer.save()
return Response({"pk": device.id}, status=status.HTTP_201_CREATED)

How to get the last createdAt item with TypeORM

Hi friends I need help with this query that I want to make to return only the last 'Case' by 'User'.
On my PostMan request I have my service that returns all the cases that the user have made.
users.services.ts
async findCasesbyUserId(id: number) {
return await this.usersRepository.findOne(id, { relations: ['cases']})
}
users.controller.ts
#Get('/findCase/:id')
async findCasesByUserId(#Param('id', ParseIntPipe) id: number,) {
return this.usersService.findCasesbyUserId(id)
}
When I made my GET request to 'http://localhost:3000/users/findCase/3'. This is the result that I get.
The user with id=3 have the cases with the id=1, id=2, id=3
So I want only to return only the case with the id=3 because is the last created case. I need a way to get the last case by the field createdAt or another way could be great for me.
Im using NestJs with TypeORM for thew querys to my PostgreSQL DB.
{
"id": 3,
"createdAt": "1617983079621",
"updatedAt": "1617983079621",
"name": "John",
"lastname": "Doe",
"email": "jdoe#email.com",
"username": "JohnDoe",
"password": "**************",
"role": "Admin",
"isActive": true,
"document": "12345",
"address": "Test",
"phone": "12345",
"cases": [
{
"id": 1,
"createdAt": "1618504530817",
"updatedAt": "1618504530817",
"message": "Test Case 1",
},
{
"id": 2,
"createdAt": "1618504530817",
"updatedAt": "1618504530817",
"message": "Test Case 1",
}, {
"id": 3,
"createdAt": "1618546146772",
"updatedAt": "1618546146772",
"message": "Test Case 1",
},
]
}

How to pass json data as queryset object to django template?

I have this json data in views.py
[
{
"id": 6,
"first_name": "Star",
"last_initial": "W",
"phone": "+918893972142",
"email": "star#gmail.com",
"date_created": "2020-10-12T17:17:17.629123Z",
"orders": []
},
{
"id": 4,
"first_name": "Sam",
"last_initial": "0",
"phone": "+918766897214",
"email": "sam#gmail.com",
"date_created": "2020-10-12T17:13:33.435065Z",
"orders": []
},
{
"id": 3,
"first_name": "Gaara",
"last_initial": "W",
"phone": "+918668972789",
"email": "gaara#gmail.com",
"date_created": "2020-10-12T17:08:44.906809Z",
"orders": [
{
"order_id": "9",
"customer_id": "3",
"customer_name": "Gaara W",
"product_id": "3",
"product_name": "Jet",
"date_created": "2020-10-12T17:18:18.823289Z",
"status": "Delivered"
}
]
}
]
I want to pass this data as a context object to the template from views using render function. so I can display this data using {% for %} loop in my template.
Pass the data into the template.
return render(request, "app/template.html", {
"data": data
})
Then you can use for loops to render the data you need.

Querying data on S3 Object

Trying to query data on json file using S3-Select
{
"groups_id":
{
"307225":
{
"created_at": "2015-02-10T17:24:15-08:00",
"updated_at": "2017-09-06T17:25:22-07:00",
"name": "Company 1",
"company": true,
"contact_name": "User 1",
"email": "",
"phone_number": "",
"address": "",
"website": "",
"notes": "Testing",
"id": "307225"
},
"1058565":
{
"created_at": "2017-04-02T23:44:10-07:00",
"updated_at": "2017-07-18T17:39:21-07:00",
"name": "Company 3",
"company": true,
"contact_name": "User 1",
"email": "",
"phone_number": "",
"address": "",
"website": "",
"notes": null,
"id": "1058565"
}
}
}
Can someone help to get the desired output using s3 select, based on the condition as WHERE contact_name='User 1'.
{"id": "307225", "name": "Company 1"},
{"id": "1058565","name": "Company 3"}
below are the queries we had tried,
Select s.groups_id['307225'].id from s3object s
Select s.groups_id['1058565'].id from s3object s
in the above queries, we had hardcoded the group_id and we are able to fetch below
{
"id": "1058565"
}
but in our case groupid is dynamic so i am not understanding. how to handle that?

Script Mediator setPayloadJSON formatting numeric string as Number

We are using a script mediator to create a payload to our aggregation mediator, we have a numeric string that as typed as string, but mc.setPayloadJSON always formatting string as number in json output.
Desired:
{
"id": "29fcf271-a077-4bd9-abb0-f6e7f8bef801",
"scheme": "EMAIL",
"date": "30/10/1986",
"name": "Name",
"document": "36105126875",
"type": "CODE",
"value": "c*********a#******.com",
"mat": "553179"
},
Result:
{
"id": "29fcf271-a077-4bd9-abb0-f6e7f8bef801",
"scheme": "EMAIL",
"date": "30/10/1986",
"name": "Name",
"document": 36105126875,
"type": "CODE",
"value": "c*********a#******.com",
"corporateid": 553179
},
JS Example:
var payload = { "methods": [{ "reference": id, "type": "CODE", "value": email_mask, "scheme": "EMAIL", "name": "Name", "document": numdoc, "mat": mat, "date": dt_birth }] }
mc.setPayloadJSON(payload);
There was a way to formatting numeric string as string with script mediator mc.setPayloadJSON?
Thank you