Mongoose find exactly match 100% with properties of object - express

i am using mongoose currently for my project and i using mongoose find to query document that fit 100% exactly with what i define in the object. This is my router define:
router.get('/warned-students/:classname/:schoolYear/:semester', get_warned_students)
this is the link i typed:
http://localhost:5000/students/warned-students/k61caclc1/2016/1
as you can see, it received 3 params classname, schoolYear and semester. classname is k61clc1, choolYear is 2016 and semester is 1.
and this is my controller
const Student = require('../models/student')
exports.get_warned_students = (req, res, next) => {
const { classname, schoolYear, semester } = req.params
Student.find({
classname,
'accademicTrainningList.schoolYear': schoolYear,
'accademicTrainningList.semester': semester,
'accademicTrainningList.classification': 'Weak'
})
.exec()
.then(students => {
res.status(200).json({ size: students.length, students })
})
.catch(err => {
console.log('error in finding warned students')
res.status(500).json({ err })
})
}
it worked, but not return what i expected:
{
"size": 14,
"students": [
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e1b",
"fullname": "Trịnh Phan Anh ",
"birthday": "19/09/1997 ",
"classname": "k61caclc1",
"code": "16020508",
"vnumail": "16020508#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Good",
"_id": "5f603a126d633e75884e9dc0",
"score": 80,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Weak",
"_id": "5f603b1f3bd07d5cb4d6fea9",
"score": 53,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
},
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e1e",
"fullname": "Đinh Việt Cường ",
"birthday": "13/03/1996 ",
"classname": "k61caclc1",
"code": "16022255",
"vnumail": "16022255#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Good",
"_id": "5f603a126d633e75884e9dc3",
"score": 80,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Weak",
"_id": "5f603b1f3bd07d5cb4d6feac",
"score": 56,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
},
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e23",
"fullname": "Phạm Duy ",
"birthday": "17/12/1998 ",
"classname": "k61caclc1",
"code": "16020542",
"vnumail": "16020542#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Excellent",
"_id": "5f603a126d633e75884e9dc8",
"score": 90,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Weak",
"_id": "5f603b1f3bd07d5cb4d6feb1",
"score": 56,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
},
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e26",
"fullname": "Đặng Ngọc Đam ",
"birthday": "26/11/1998 ",
"classname": "k61caclc1",
"code": "16020518",
"vnumail": "16020518#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Good",
"_id": "5f603a126d633e75884e9dcb",
"score": 80,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Weak",
"_id": "5f603b1f3bd07d5cb4d6feb4",
"score": 55,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
},
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e28",
"fullname": "Nguyễn Hải Đăng ",
"birthday": "09/08/1998 ",
"classname": "k61caclc1",
"code": "16020520",
"vnumail": "16020520#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Good",
"_id": "5f603a126d633e75884e9dcd",
"score": 80,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Weak",
"_id": "5f603b1f3bd07d5cb4d6feb6",
"score": 56,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
},
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e29",
"fullname": "Nguyễn Hải Đăng ",
"birthday": "24/11/1998 ",
"classname": "k61caclc1",
"code": "16020519",
"vnumail": "16020519#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Excellent",
"_id": "5f603a126d633e75884e9dce",
"score": 90,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Weak",
"_id": "5f603b1f3bd07d5cb4d6feb7",
"score": 54,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
},
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e2b",
"fullname": "Đỗ Hồng Giang ",
"birthday": "29/01/1997 ",
"classname": "k61caclc1",
"code": "16020544",
"vnumail": "16020544#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Good",
"_id": "5f603a126d633e75884e9dd0",
"score": 80,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Weak",
"_id": "5f603b1f3bd07d5cb4d6feb9",
"score": 57,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
},
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e36",
"fullname": "Phạm Minh Hiếu ",
"birthday": "17/10/1998 ",
"classname": "k61caclc1",
"code": "16020373",
"vnumail": "16020373#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Good",
"_id": "5f603a126d633e75884e9ddb",
"score": 80,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Weak",
"_id": "5f603b1f3bd07d5cb4d6fec4",
"score": 55,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
},
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e38",
"fullname": "Nguyễn Ngọc Hòa ",
"birthday": "29/04/1997 ",
"classname": "k61caclc1",
"code": "16020566",
"vnumail": "16020566#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Good",
"_id": "5f603a126d633e75884e9ddd",
"score": 80,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Weak",
"_id": "5f603b1f3bd07d5cb4d6fec6",
"score": 57,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
},
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e3b",
"fullname": "Trần Hữu Hoàn ",
"birthday": "25/11/1998 ",
"classname": "k61caclc1",
"code": "16022471",
"vnumail": "16022471#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Good",
"_id": "5f603a126d633e75884e9de0",
"score": 80,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Weak",
"_id": "5f603b1f3bd07d5cb4d6fec9",
"score": 59,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
},
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e3e",
"fullname": "Nguyễn Tấn Hoàng ",
"birthday": "06/01/1998 ",
"classname": "k61caclc1",
"code": "16022261",
"vnumail": "16022261#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Good",
"_id": "5f603a126d633e75884e9de3",
"score": 80,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Weak",
"_id": "5f603b1f3bd07d5cb4d6fecc",
"score": 50,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
},
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e47",
"fullname": "Nguyễn Trung Kiên ",
"birthday": "18/09/1998 ",
"classname": "k61caclc1",
"code": "16020588",
"vnumail": "16020588#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Intermediate",
"_id": "5f603a126d633e75884e9dec",
"score": 75,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Weak",
"_id": "5f603b1f3bd07d5cb4d6fed5",
"score": 55,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
},
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e52",
"fullname": "Nguyễn Văn Mạnh ",
"birthday": "07/07/1998 ",
"classname": "k61caclc1",
"code": "16020606",
"vnumail": "16020606#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Good",
"_id": "5f603a126d633e75884e9df7",
"score": 80,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Weak",
"_id": "5f603b1f3bd07d5cb4d6fee0",
"score": 59,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
},
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e60",
"fullname": "Tạ Văn Quỳnh ",
"birthday": "06/12/1998 ",
"classname": "k61caclc1",
"code": "16020637",
"vnumail": "16020637#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Good",
"_id": "5f603a126d633e75884e9e05",
"score": 80,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Weak",
"_id": "5f603b1f3bd07d5cb4d6feee",
"score": 59,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
}
]
}
As you can see it returns results containing 1 of 3 properties but what I want is it returns only results containing all 3 properties. Thanks for helping me, have a nice day
Update:
this is what i expected: the array "accademicTrainningList" contains an element that fit all 3 property schoolYear: 2016, semester: 1 and classification: weak
{
"gender": "No Record",
"profileImage": "https://kittyinpink.co.uk/wp-content/uploads/2016/12/facebook-default-photo-male_1-1.jpg",
"_id": "5f603a126d633e75884e9e1b",
"fullname": "Trịnh Phan Anh ",
"birthday": "19/09/1997 ",
"classname": "k61caclc1",
"code": "16020508",
"vnumail": "16020508#vnu.edu.vn",
"accademicTrainningList": [
{
"semester": "1",
"classification": "Weak",
"_id": "5f603a126d633e75884e9dc0",
"score": 53,
"schoolYear": "2016"
},
{
"semester": "2",
"classification": "Good",
"_id": "5f603b1f3bd07d5cb4d6fea9",
"score": 80,
"schoolYear": "2016"
}
],
"scoreList": [],
"receiveScholarship": [],
"prizeList": [],
"scienceContestPrizeList": [],
"wentAbroad": [],
"tookTheTest": [],
"punishList": [],
"__v": 0
},
or else, if nothing match it return an empty array

It looks like $elemMatch is what you need. In your case, the query would be something like:
Student.find({
classname: "k61caclc1",
accademicTrainningList: {
$elemMatch: {
semester: "1",
schoolYear: "2016",
classification: "Weak"
}
}
})
Mongo Playground

Related

I keep getting 400 error(travelerId not existing) on calling the CreateOrder API

I am on a test Account.
On printing the travelerPricings Object to console, I have this
[
{
travelerId: '1',
fareOption: 'STANDARD',
travelerType: 'ADULT',
price: {
currency: '',
total: '',
base: '',
taxes: [Array],
refundableTaxes: ''
},
fareDetailsBySegment: [ [Object], [Object], [Object], [Object] ]
},
{
travelerId: '2',
fareOption: 'STANDARD',
travelerType: 'CHILD',
price: {
currency: '',
total: '',
base: '',
taxes: [Array],
refundableTaxes: ''
},
fareDetailsBySegment: [ [Object], [Object], [Object], [Object] ]
}
]
Yet on the Api Response, I keep getting the error pointer as
/data/flightOffers[0]/travelerPricings[1]"
Which is not true. As seen in the log shared above,
travelerPricings[1].travelerId = '2'
Please Why could this error come up? Thanks
Based on the Request, Here is the sample payload
{"data":
{
"flightOffers": [
{
"type": "flight-offer",
"id": "1",
"source": "GDS",
"instantTicketingRequired": false,
"nonHomogeneous": false,
"paymentCardRequired": false,
"lastTicketingDate": "2022-08-01",
"itineraries": [
{
"segments": [
{
"departure": {
"iataCode": "SYD",
"terminal": "1",
"at": "2022-08-01T11:35:00"
},
"arrival": {
"iataCode": "MNL",
"terminal": "2",
"at": "2022-08-01T16:50:00"
},
"carrierCode": "PR",
"number": "212",
"aircraft": {
"code": "333"
},
"operating": {
"carrierCode": "PR"
},
"duration": "PT7H15M",
"id": "15",
"numberOfStops": 0,
"co2Emissions": [
{
"weight": 716,
"weightUnit": "KG",
"cabin": "BUSINESS"
}
]
},
{
"departure": {
"iataCode": "MNL",
"terminal": "1",
"at": "2022-08-01T19:20:00"
},
"arrival": {
"iataCode": "BKK",
"at": "2022-08-01T21:50:00"
},
"carrierCode": "PR",
"number": "732",
"aircraft": {
"code": "320"
},
"operating": {
"carrierCode": "PR"
},
"duration": "PT3H30M",
"id": "16",
"numberOfStops": 0,
"co2Emissions": [
{
"weight": 148,
"weightUnit": "KG",
"cabin": "BUSINESS"
}
]
}
]
},
{
"segments": [
{
"departure": {
"iataCode": "BKK",
"at": "2022-08-05T22:50:00"
},
"arrival": {
"iataCode": "MNL",
"terminal": "2",
"at": "2022-08-06T03:15:00"
},
"carrierCode": "PR",
"number": "733",
"aircraft": {
"code": "321"
},
"operating": {
"carrierCode": "PR"
},
"duration": "PT3H25M",
"id": "59",
"numberOfStops": 0,
"co2Emissions": [
{
"weight": 148,
"weightUnit": "KG",
"cabin": "ECONOMY"
}
]
},
{
"departure": {
"iataCode": "MNL",
"terminal": "1",
"at": "2022-08-06T22:10:00"
},
"arrival": {
"iataCode": "SYD",
"terminal": "1",
"at": "2022-08-07T09:45:00"
},
"carrierCode": "PR",
"number": "211",
"aircraft": {
"code": "333"
},
"operating": {
"carrierCode": "PR"
},
"duration": "PT9H35M",
"id": "60",
"numberOfStops": 0,
"co2Emissions": [
{
"weight": 358,
"weightUnit": "KG",
"cabin": "ECONOMY"
}
]
}
]
}
],
"price": {
"currency": "NGN",
"total": "1479460.00",
"base": "1298255.00",
"fees": [
{
"amount": "0.00",
"type": "SUPPLIER"
},
{
"amount": "0.00",
"type": "TICKETING"
},
{
"amount": "0.00",
"type": "FORM_OF_PAYMENT"
}
],
"grandTotal": "1479460.00",
"billingCurrency": "NGN"
},
"pricingOptions": {
"fareType": [
"PUBLISHED"
],
"includedCheckedBagsOnly": false
},
"validatingAirlineCodes": [
"PR"
],
"travelerPricings": [
{
"travelerId": "1",
"fareOption": "STANDARD",
"travelerType": "ADULT",
"price": {
"currency": "NGN",
"total": "840536",
"base": "740769",
"taxes": [
{
"amount": "384.00",
"code": "G8"
},
{
"amount": "19112.00",
"code": "WY"
},
{
"amount": "896.00",
"code": "E7"
},
{
"amount": "18329.00",
"code": "AU"
},
{
"amount": "42766.00",
"code": "YQ"
},
{
"amount": "9320.00",
"code": "LI"
},
{
"amount": "8960.00",
"code": "TS"
}
],
"refundableTaxes": "108123"
},
"fareDetailsBySegment": [
{
"segmentId": "15",
"cabin": "BUSINESS",
"fareBasis": "DBAU",
"class": "D",
"includedCheckedBags": {
"weight": 40,
"weightUnit": "KG"
}
},
{
"segmentId": "16",
"cabin": "BUSINESS",
"fareBasis": "DBAU",
"class": "D",
"includedCheckedBags": {
"weight": 40,
"weightUnit": "KG"
}
},
{
"segmentId": "59",
"cabin": "ECONOMY",
"fareBasis": "KBAU",
"class": "K",
"includedCheckedBags": {
"weight": 30,
"weightUnit": "KG"
}
},
{
"segmentId": "60",
"cabin": "ECONOMY",
"fareBasis": "KBAU",
"class": "K",
"includedCheckedBags": {
"weight": 30,
"weightUnit": "KG"
}
}
]
},
{
"travelerId": "2",
"fareOption": "STANDARD",
"travelerType": "CHILD",
"price": {
"currency": "NGN",
"total": "638924",
"base": "557486",
"taxes": [
{
"amount": "384.00",
"code": "G8"
},
{
"amount": "19112.00",
"code": "WY"
},
{
"amount": "896.00",
"code": "E7"
},
{
"amount": "42766.00",
"code": "YQ"
},
{
"amount": "9320.00",
"code": "LI"
},
{
"amount": "8960.00",
"code": "TS"
}
],
"refundableTaxes": "89794"
},
"fareDetailsBySegment": [
{
"segmentId": "15",
"cabin": "BUSINESS",
"fareBasis": "DBAU",
"class": "D",
"includedCheckedBags": {
"weight": 40,
"weightUnit": "KG"
}
},
{
"segmentId": "16",
"cabin": "BUSINESS",
"fareBasis": "DBAU",
"class": "D",
"includedCheckedBags": {
"weight": 40,
"weightUnit": "KG"
}
},
{
"segmentId": "59",
"cabin": "ECONOMY",
"fareBasis": "KBAU",
"class": "K",
"includedCheckedBags": {
"weight": 30,
"weightUnit": "KG"
}
},
{
"segmentId": "60",
"cabin": "ECONOMY",
"fareBasis": "KBAU",
"class": "K",
"includedCheckedBags": {
"weight": 30,
"weightUnit": "KG"
}
}
]
}
]
}
],
"travelers": [ {
"id": "1",
"dateOfBirth": "1982-01-16",
"name": {
"firstName": "JORGE",
"lastName": "GONZALES"
},
"gender": "MALE",
"contact": {
"emailAddress": "jorge.gonzales833#telefonica.es",
"phones": [ {
"deviceType": "MOBILE",
"countryCallingCode": "34",
"number": "480080076"
} ]
},
"documents": [ {
"documentType": "PASSPORT",
"birthPlace": "Madrid",
"issuanceLocation": "Madrid",
"issuanceDate": "2015-04-14",
"number": "00000000",
"expiryDate": "2025-04-14",
"issuanceCountry": "ES",
"validityCountry": "ES",
"nationality": "ES",
"holder": true
} ]
} ]
}
MY Request using the NodeSDK
const {data:{flightOffers}} = req.body;
const {result} = await Amadeus.booking.flightOrders.post(
JSON.stringify({
'data': {
'type': 'flight-order',
'flightOffers': [ flightOffers[ 0 ] ],
'travelers': travelers
}
})
);
This is quite lengthy, but I did this so it is easy to just re-create the exact Scenario in case the issue is somewhere in the payload I am sending.
When you request Flight Create Order API, you have only put 1 traveler's information at the end that's why the error comes. you are requesting for flight order for 2 persons (1 adult and 1 child) so both information should be attached at the end.
below is a node example for the entire flow from Flight offer search, pricing, and create order. you may get a different error if some segments are not available, but the issue that you faced regarding travelers is resolved.
amadeus.shopping.flightOffersSearch.get({
originLocationCode: 'SYD',
destinationLocationCode: 'BKK',
departureDate: '2022-08-01',
adults: '1',
children: '1'
}).then(function (flightOffersResponse) {
return amadeus.shopping.flightOffers.pricing.post(
JSON.stringify({
'data': {
'type': 'flight-offers-pricing',
'flightOffers': [flightOffersResponse.data[0]]
}
})
)
}).then(function (pricingResponse) {
return amadeus.booking.flightOrders.post(
JSON.stringify({
'data': {
'type': 'flight-order',
'flightOffers': [pricingResponse.data.flightOffers[0]],
'travelers': [{
"id": "1",
"dateOfBirth": "1982-01-16",
"name": {
"firstName": "JORGE",
"lastName": "GONZALES"
},
"gender": "MALE",
"contact": {
"emailAddress": "jorge.gonzales833#telefonica.es",
"phones": [{
"deviceType": "MOBILE",
"countryCallingCode": "34",
"number": "480080076"
}]
},
"documents": [{
"documentType": "PASSPORT",
"birthPlace": "Madrid",
"issuanceLocation": "Madrid",
"issuanceDate": "2015-04-14",
"number": "00000000",
"expiryDate": "2025-04-14",
"issuanceCountry": "ES",
"validityCountry": "ES",
"nationality": "ES",
"holder": true
}]
},
{
"id": "2",
"dateOfBirth": "2012-10-11",
"gender": "FEMALE",
"contact": {
"emailAddress": "jorge.gonzales833#telefonica.es",
"phones": [
{
"deviceType": "MOBILE",
"countryCallingCode": "34",
"number": "480080076"
}
]
},
"name": {
"firstName": "ADRIANA",
"lastName": "GONZALES"
}
}]
}
})
);
}).then(function (response) {
console.log(response);
}).catch(function (response) {
console.error(response);
});

extra bag information is missing in the flight offer price response

I'm testing self-service APIs. I wonder if this is a bug:
I make a search request, and the response contains the extra bag information in the flight-offer/price/additionalServices
Then I make the offer price request by adding include=bags parameter in the path of Flight Offers Price API.
However there is no any bags information in the response, neither in the offer/price/additionalServices, nor in the included/.
I still try to create order by adding the extra bag. So I use the extra bag information that I got at step 1 (search response). And the order is created successfully.
It seems that the extra bag information is missing in step 2 (response of flight offer price), is it a bug?
Here is an example of my test to reproduce the issue:
search request
{
"currencyCode": "EUR",
"originDestinations": [
{
"id": "1",
"originLocationCode": "PAR",
"destinationLocationCode": "NYC",
"departureDateTimeRange": {
"date": "2020-08-20",
"time": "10:00:00"
}
}
],
"travelers": [
{
"id": "1",
"travelerType": "ADULT"
}
],
"sources": [
"GDS"
],
"searchCriteria": {
"maxFlightOffers": 3
}
}
Then I select the second offer in the response to make a offer price request using include=bags parameter
{
"type": "flight-offer",
"id": "2",
"source": "GDS",
"instantTicketingRequired": false,
"nonHomogeneous": false,
"oneWay": false,
"lastTicketingDate": "2020-08-13",
"numberOfBookableSeats": 8,
"itineraries": [
{
"duration": "PT8H15M",
"segments": [
{
"departure": {
"iataCode": "ORY",
"terminal": "4",
"at": "2020-08-20T19:45:00"
},
"arrival": {
"iataCode": "EWR",
"terminal": "B",
"at": "2020-08-20T22:00:00"
},
"carrierCode": "TX",
"number": "6720",
"aircraft": {
"code": "359"
},
"operating": {
"carrierCode": "BF"
},
"duration": "PT8H15M",
"id": "3",
"numberOfStops": 0,
"blacklistedInEU": false
}
]
}
],
"price": {
"currency": "EUR",
"total": "149.44",
"base": "41.00",
"fees": [
{
"amount": "0.00",
"type": "SUPPLIER"
},
{
"amount": "0.00",
"type": "TICKETING"
}
],
"grandTotal": "149.44",
"additionalServices": [
{
"amount": "70.00",
"type": "CHECKED_BAGS"
}
]
},
"pricingOptions": {
"fareType": [
"PUBLISHED"
],
"includedCheckedBagsOnly": false
},
"validatingAirlineCodes": [
"TX"
],
"travelerPricings": [
{
"travelerId": "1",
"fareOption": "STANDARD",
"travelerType": "ADULT",
"price": {
"currency": "EUR",
"total": "149.44",
"base": "41.00"
},
"fareDetailsBySegment": [
{
"segmentId": "3",
"cabin": "ECONOMY",
"fareBasis": "ULBCOWFR",
"brandedFare": "EBASIC",
"class": "U",
"includedCheckedBags": {
"quantity": 0
}
}
]
}
]
}
There is no extra bag in the response.
Thanks

Transform Multiple rows of JSON format to SQL table

I have JSON file in Blob Storage that looks like this:
{
"Id": "****************************",
"Status": "OK",
"ProviderName": "Xero API Previewer",
"DateTimeUTC": "\/Date(1576561543598)\/",
"Invoices": [
{
"Type": "ACCPAY",
"InvoiceID": "*****************************",
"InvoiceNumber": "457489",
"Reference": "",
"Payments": [],
"CreditNotes": [],
"Prepayments": [],
"Overpayments": [],
"AmountDue": 0.00,
"AmountPaid": 0.00,
"AmountCredited": 0.00,
"CurrencyRate": 1.000000,
"IsDiscounted": false,
"HasAttachments": false,
"HasErrors": false,
"Contact": {
"ContactID": "************************************",
"Name": "********************",
"Addresses": [],
"Phones": [],
"ContactGroups": [],
"ContactPersons": [],
"HasValidationErrors": false
},
"DateString": "2102-11-26T00:00:00",
"Date": "\/Date(4193942400000+0000)\/",
"DueDateString": "2012-11-28T00:00:00",
"DueDate": "\/Date(1354060800000+0000)\/",
"Status": "VOIDED",
"LineAmountTypes": "Inclusive",
"LineItems": [
{
"Description": "Parking ",
"UnitAmount": 465.01,
"TaxType": "INPUT2",
"TaxAmount": 60.65,
"LineAmount": 465.01,
"AccountCode": "274",
"Tracking": [
{
"Name": "Region",
"Option": "New Zealand",
"TrackingCategoryID": "****************************************",
"Options": []
},
{
"Name": "Owner",
"Option": "Head Office",
"TrackingCategoryID": "***************************************",
"Options": []
}
],
"Quantity": 1.0000,
"LineItemID": "**************************************"
}
],
"SubTotal": 404.36,
"TotalTax": 60.65,
"Total": 465.01,
"UpdatedDateUTC": "\/Date(1355876228590+0000)\/",
"CurrencyCode": "NZD"
},
{
"Type": "ACCPAY",
"InvoiceID": "**************************************",
"InvoiceNumber": "176295-01",
"Reference": "",
"Payments": [
{
"PaymentID": "********************************************",
"Date": "\/Date(1576454400000+0000)\/",
"Amount": 137.43,
"Reference": "",
"CurrencyRate": 1.000000,
"HasAccount": false,
"HasValidationErrors": false
}
],
"CreditNotes": [],
"Prepayments": [],
"Overpayments": [],
"AmountDue": 0.00,
"AmountPaid": 137.43,
"AmountCredited": 0.00,
"CurrencyRate": 1.000000,
"IsDiscounted": false,
"HasAttachments": true,
"HasErrors": false,
"Contact": {
"ContactID": "************************************",
"Name": "InkWorks",
"Addresses": [],
"Phones": [],
"ContactGroups": [],
"ContactPersons": [],
"HasValidationErrors": false
},
"DateString": "2019-12-17T00:00:00",
"Date": "\/Date(1576540800000+0000)\/",
"DueDateString": "2020-01-20T00:00:00",
"DueDate": "\/Date(1579478400000+0000)\/",
"Status": "PAID",
"LineAmountTypes": "Inclusive",
"LineItems": [
{
"Description": "general stationery",
"UnitAmount": 137.43,
"TaxType": "INPUT2",
"TaxAmount": 17.93,
"LineAmount": 137.43,
"AccountCode": "273",
"Tracking": [
{
"Name": "Owner",
"Option": "Head Office",
"TrackingCategoryID": "******************************",
"Options": []
}
],
"Quantity": 1.0000,
"LineItemID": "****************************************"
}
],
"SubTotal": 119.50,
"TotalTax": 17.93,
"Total": 137.43,
"UpdatedDateUTC": "\/Date(1576524509820+0000)\/",
"CurrencyCode": "NZD",
"FullyPaidOnDate": "\/Date(1576454400000+0000)\/"
},
I want to store LineItems into a Table with columns as follows:
InvoiceID LineItemID Description LineAmount AccountCode Date
There can be multiple LineItems in a single invoice.
I managed to get the top level only with the following code.
WITH cte AS (
SELECT CAST (BulkColumn AS NVARCHAR(MAX)) AS jsonData
FROM
OPENROWSET(
BULK 'temp/XeroJson.json',
DATA_SOURCE = 'test'
-- FORMATFILE_DATA_SOURCE = 'test'
, SINGLE_CLOB
) AS blob
)
SELECT *
FROM cte
CROSS APPLY
OPENJSON(cte.jsonData) j
How can I make this work?
This is my test .json file, I placed it in my storage account :
{
"Id": "****************************",
"Status": "OK",
"ProviderName": "Xero API Previewer",
"DateTimeUTC": "\/Date(1576561543598)\/",
"Invoices":
[
{
"Type": "ACCPAY",
"InvoiceID": "123456",
"InvoiceNumber": "457489",
"Reference": "",
"Payments": [],
"CreditNotes": [],
"Prepayments": [],
"Overpayments": [],
"AmountDue": 0.00,
"AmountPaid": 0.00,
"AmountCredited": 0.00,
"CurrencyRate": 1.000000,
"IsDiscounted": false,
"HasAttachments": false,
"HasErrors": false,
"Contact": {
"ContactID": "************************************",
"Name": "********************",
"Addresses": [],
"Phones": [],
"ContactGroups": [],
"ContactPersons": [],
"HasValidationErrors": false
},
"DateString": "2102-11-26T00:00:00",
"Date": "\/Date(4193942400000+0000)\/",
"DueDateString": "2012-11-28T00:00:00",
"DueDate": "\/Date(1354060800000+0000)\/",
"Status": "VOIDED",
"LineAmountTypes": "Inclusive",
"LineItems": [{
"Description": "Parking ",
"UnitAmount": 465.01,
"TaxType": "INPUT2",
"TaxAmount": 60.65,
"LineAmount": 465.01,
"AccountCode": "274",
"Tracking": [{
"Name": "Region",
"Option": "New Zealand",
"TrackingCategoryID": "****************************************",
"Options": []
}, {
"Name": "Owner",
"Option": "Head Office",
"TrackingCategoryID": "***************************************",
"Options": []
}
],
"Quantity": 1.0000,
"LineItemID": "**************************************"
},
{
"Description": "Parking2 ",
"UnitAmount": 111.11,
"TaxType": "INPUT2",
"TaxAmount": 60.65,
"LineAmount": 465.01,
"AccountCode": "276",
"Tracking": [{
"Name": "Region",
"Option": "New Zealand",
"TrackingCategoryID": "****************************************",
"Options": []
}, {
"Name": "Owner",
"Option": "Head Office",
"TrackingCategoryID": "***************************************",
"Options": []
}
],
"Quantity": 1.0000,
"LineItemID": "**************************************"
}
],
"SubTotal": 404.36,
"TotalTax": 60.65,
"Total": 465.01,
"UpdatedDateUTC": "\/Date(1355876228590+0000)\/",
"CurrencyCode": "NZD"
},
{
"Type": "ACCPAY",
"InvoiceID": "1234567",
"InvoiceNumber": "176295-01",
"Reference": "",
"Payments": [{
"PaymentID": "********************************************",
"Date": "\/Date(1576454400000+0000)\/",
"Amount": 137.43,
"Reference": "",
"CurrencyRate": 1.000000,
"HasAccount": false,
"HasValidationErrors": false
}
],
"CreditNotes": [],
"Prepayments": [],
"Overpayments": [],
"AmountDue": 0.00,
"AmountPaid": 137.43,
"AmountCredited": 0.00,
"CurrencyRate": 1.000000,
"IsDiscounted": false,
"HasAttachments": true,
"HasErrors": false,
"Contact": {
"ContactID": "************************************",
"Name": "InkWorks",
"Addresses": [],
"Phones": [],
"ContactGroups": [],
"ContactPersons": [],
"HasValidationErrors": false
},
"DateString": "2019-12-17T00:00:00",
"Date": "\/Date(1576540800000+0000)\/",
"DueDateString": "2020-01-20T00:00:00",
"DueDate": "\/Date(1579478400000+0000)\/",
"Status": "PAID",
"LineAmountTypes": "Inclusive",
"LineItems": [{
"Description": "general stationery",
"UnitAmount": 137.43,
"TaxType": "INPUT2",
"TaxAmount": 17.93,
"LineAmount": 137.43,
"AccountCode": "273",
"Tracking": [{
"Name": "Owner",
"Option": "Head Office",
"TrackingCategoryID": "******************************",
"Options": []
}
],
"Quantity": 1.0000,
"LineItemID": "****************************************"
}
],
"SubTotal": 119.50,
"TotalTax": 17.93,
"Total": 137.43,
"UpdatedDateUTC": "\/Date(1576524509820+0000)\/",
"CurrencyCode": "NZD",
"FullyPaidOnDate": "\/Date(1576454400000+0000)\/"
}
]
}
As you can see the first invoice has two lineItems for demo here.
This is my sql script to exract from the json from storage :
CREATE DATABASE SCOPED CREDENTIAL AccessAzureInvoices
WITH
IDENTITY = 'SHARED ACCESS SIGNATURE'
-- REMOVE ? FROM THE BEGINNING OF THE SAS TOKEN
, SECRET = '<YOUR STORAGE SAS TOKEN>'
;
CREATE EXTERNAL DATA SOURCE MyAzureInvoices
WITH
( LOCATION = 'https://<YOUR STORAGE NAME>.blob.core.windows.net/<CONTAINER NAME WHERE YOUR JSON FILE IN>'
, CREDENTIAL = AccessAzureInvoices
, TYPE = BLOB_STORAGE
)
;
DECLARE #jsonVariable NVARCHAR(MAX);
set #jsonVariable = (select * from OPENROWSET(
BULK '<YOUR JSON FILE NAME>.json',
DATA_SOURCE = 'MyAzureInvoices',
SINGLE_CLOB
) AS blob)
select * from (
SELECT *
FROM OPENJSON(#jsonVariable,N'$.Invoices') WITH (
InvoiceID VARCHAR(200) N'$.InvoiceID',
Date VARCHAR(200) N'$.Date',
LineItems NVARCHAR(MAX) N'$.LineItems' as json
))as table1
CROSS APPLY OPENJSON(LineItems) with (
LineAmount VARCHAR(200) '$.LineAmount',
AccountCode VARCHAR(200)'$.AccountCode',
LineItemID VARCHAR(200) '$.LineItemID',
Description VARCHAR(200) '$.Description'
)
Result :
Hope it helps !

How do I use the Google Civic Information API to retrieve members of Congress for a given location?

I've tried using the civicinfo.representatives.representativeInfoByAddress endpoint in the API explorer (https://developers.google.com/apis-explorer/?hl=en_US#p/civicinfo/v2/civicinfo.representatives.representativeInfoByAddress).
Here's my problem: That endpoint returns all state, federal, and local offices EXCEPT the U.S. House of Representatives! I can see the governor, senators, state senators, but NOT members of Congress. I can't find anything in the documentation to explain why this blatantly important data is being omitted, nor can I find any alternative sources (that are actually current) for this data.
Why is Google withholding the Congressional data and how do I get it? That's the ONE THING I need from this API and it seems to be missing from the results. I've tried playing with the parameters and whatnot but I can't find that data.
There has to be a way to find this information. In the Google API Explorer (see above link), try entering "WA" for the "address" field and leave everything else blank (setting the next one to TRUE makes no difference and the others are just filters). Here's the data that's returned:
{
"kind": "civicinfo#representativeInfoResponse",
"normalizedInput": {
"line1": "",
"city": "",
"state": "WA",
"zip": ""
},
"divisions": {
"ocd-division/country:us": {
"name": "United States",
"officeIndices": [
0,
1
]
},
"ocd-division/country:us/state:wa": {
"name": "Washington",
"officeIndices": [
2,
3,
4,
5,
6,
7,
8,
9,
10,
11
]
}
},
"offices": [
{
"name": "President of the United States",
"divisionId": "ocd-division/country:us",
"levels": [
"country"
],
"roles": [
"headOfState",
"headOfGovernment"
],
"officialIndices": [
0
]
},
{
"name": "Vice-President of the United States",
"divisionId": "ocd-division/country:us",
"levels": [
"country"
],
"roles": [
"deputyHeadOfGovernment"
],
"officialIndices": [
1
]
},
{
"name": "United States Senate",
"divisionId": "ocd-division/country:us/state:wa",
"levels": [
"country"
],
"roles": [
"legislatorUpperBody"
],
"officialIndices": [
2,
3
]
},
{
"name": "Governor",
"divisionId": "ocd-division/country:us/state:wa",
"levels": [
"administrativeArea1"
],
"roles": [
"headOfGovernment"
],
"officialIndices": [
4
]
},
{
"name": "Lieutenant Governor",
"divisionId": "ocd-division/country:us/state:wa",
"levels": [
"administrativeArea1"
],
"roles": [
"deputyHeadOfGovernment"
],
"officialIndices": [
5
]
},
{
"name": "State Auditor",
"divisionId": "ocd-division/country:us/state:wa",
"officialIndices": [
6
]
},
{
"name": "State Treasurer",
"divisionId": "ocd-division/country:us/state:wa",
"officialIndices": [
7
]
},
{
"name": "Attorney General",
"divisionId": "ocd-division/country:us/state:wa",
"officialIndices": [
8
]
},
{
"name": "Secretary of State",
"divisionId": "ocd-division/country:us/state:wa",
"officialIndices": [
9
]
},
{
"name": "Insurance Commissioner",
"divisionId": "ocd-division/country:us/state:wa",
"officialIndices": [
10
]
},
{
"name": "Commissioner of Public Lands",
"divisionId": "ocd-division/country:us/state:wa",
"officialIndices": [
11
]
},
{
"name": "State Superintendent of Public Instruction",
"divisionId": "ocd-division/country:us/state:wa",
"officialIndices": [
12
]
}
],
"officials": [
{
"name": "Donald J. Trump",
"address": [
{
"line1": "The White House",
"line2": "1600 Pennsylvania Avenue NW",
"city": "Washington",
"state": "DC",
"zip": "20500"
}
],
"party": "Republican",
"phones": [
"(202) 456-1111"
],
"urls": [
"http://www.whitehouse.gov/"
],
"photoUrl": "https://www.whitehouse.gov/sites/whitehouse.gov/files/images/45/PE%20Color.jpg",
"channels": [
{
"type": "GooglePlus",
"id": "+whitehouse"
},
{
"type": "Facebook",
"id": "whitehouse"
},
{
"type": "Twitter",
"id": "potus"
},
{
"type": "YouTube",
"id": "whitehouse"
}
]
},
{
"name": "Mike Pence",
"address": [
{
"line1": "The White House",
"line2": "1600 Pennsylvania Avenue NW",
"city": "Washington",
"state": "DC",
"zip": "20500"
}
],
"party": "Republican",
"phones": [
"(202) 456-1111"
],
"urls": [
"http://www.whitehouse.gov/"
],
"photoUrl": "https://www.whitehouse.gov/sites/whitehouse.gov/files/images/45/VPE%20Color.jpg",
"channels": [
{
"type": "GooglePlus",
"id": "+whitehouse"
},
{
"type": "Facebook",
"id": "whitehouse"
},
{
"type": "Twitter",
"id": "VP"
}
]
},
{
"name": "Maria Cantwell",
"address": [
{
"line1": "511 Hart Senate Office Building",
"city": "Washington",
"state": "DC",
"zip": "20510"
}
],
"party": "Democratic",
"phones": [
"(202) 224-3441"
],
"urls": [
"https://www.cantwell.senate.gov/"
],
"photoUrl": "http://bioguide.congress.gov/bioguide/photo/C/C000127.jpg",
"channels": [
{
"type": "Facebook",
"id": "senatorcantwell"
},
{
"type": "Twitter",
"id": "senatorcantwell"
},
{
"type": "YouTube",
"id": "SenatorCantwell"
}
]
},
{
"name": "Patty Murray",
"address": [
{
"line1": "154 Russell Senate Office Building",
"city": "Washington",
"state": "DC",
"zip": "20510"
}
],
"party": "Democratic",
"phones": [
"(202) 224-2621"
],
"urls": [
"http://www.murray.senate.gov/public/"
],
"photoUrl": "http://bioguide.congress.gov/bioguide/photo/M/M001111.jpg",
"channels": [
{
"type": "GooglePlus",
"id": "+pattymurray"
},
{
"type": "Twitter",
"id": "pattymurray"
},
{
"type": "YouTube",
"id": "SenatorPattyMurray"
},
{
"type": "Facebook",
"id": "pattymurray"
},
{
"type": "YouTube",
"id": "pattymurray"
}
]
},
{
"name": "Jay Inslee",
"address": [
{
"line1": "PO Box 40002",
"city": "Olympia",
"state": "WA",
"zip": "98504"
}
],
"party": "Democratic",
"phones": [
"(360) 902-4111"
],
"urls": [
"http://www.governor.wa.gov/"
],
"photoUrl": "http://www.governor.wa.gov/sites/default/files/images/720px-for-web_0.jpg",
"emails": [
"Governor.JayInslee#governor.wa.gov"
],
"channels": [
{
"type": "Facebook",
"id": "WaStateGov"
},
{
"type": "Twitter",
"id": "GovInslee"
},
{
"type": "YouTube",
"id": "UCJhWBqWVdVnPro7tx2t7j3w"
}
]
},
{
"name": "Cyrus Habib",
"address": [
{
"line1": "PO Box 40400",
"city": "Olympia",
"state": "WA",
"zip": "98504"
}
],
"party": "Democratic",
"phones": [
"(360) 786-7700"
],
"urls": [
"http://www.ltgov.wa.gov/"
],
"photoUrl": "http://www.ltgov.wa.gov/wp-content/uploads/2017/01/Senator-Habib-2015_CROPPED-225x300.jpg",
"emails": [
"ltgov#ltgov.wa.gov"
],
"channels": [
{
"type": "Twitter",
"id": "waltgov"
},
{
"type": "Facebook",
"id": "Lt-Governor-Cyrus-Habib-347384975292728"
}
]
},
{
"name": "Pat (Patrice) McCarthy",
"address": [
{
"line1": "Insurance Building Capitol Campus",
"line2": "302 Sid Snyder Avenue SW",
"city": "Olympia",
"state": "WA",
"zip": "98504"
}
],
"party": "Democratic",
"phones": [
"(360) 902-0370"
]
},
{
"name": "Duane Davidson",
"address": [
{
"line1": "PO Box 40200",
"city": "Olympia",
"state": "WA",
"zip": "98504"
}
],
"party": "Republican",
"phones": [
"(360) 902-9001"
]
},
{
"name": "Bob Ferguson",
"address": [
{
"line1": "PO Box 40100",
"city": "Olympia",
"state": "WA",
"zip": "98504"
}
],
"party": "Democratic",
"phones": [
"(360) 753-6200"
],
"urls": [
"http://www.atg.wa.gov/"
],
"channels": [
{
"type": "Facebook",
"id": "WAStateAttorneyGeneral"
},
{
"type": "Twitter",
"id": "AGOWA"
}
]
},
{
"name": "Kim Wyman",
"address": [
{
"line1": "PO Box 40220",
"city": "Olympia",
"state": "WA",
"zip": "98504"
}
],
"party": "Republican",
"phones": [
"(360) 902-4151"
],
"urls": [
"http://www.sos.wa.gov/"
],
"emails": [
"kim.wyman#sos.wa.gov"
],
"channels": [
{
"type": "Facebook",
"id": "WaSecretaryOfState"
},
{
"type": "Twitter",
"id": "secstatewa"
}
]
},
{
"name": "Mike Kreidler",
"party": "Democratic",
"phones": [
"(360) 725-7000"
],
"urls": [
"http://www.insurance.wa.gov/"
],
"channels": [
{
"type": "Facebook",
"id": "wsoic"
},
{
"type": "Twitter",
"id": "WAinsuranceblog"
}
]
},
{
"name": "Hilary Franz",
"address": [
{
"line1": "PO Box 47000",
"city": "Olympia",
"state": "WA",
"zip": "98504"
}
],
"party": "Democratic",
"phones": [
"(360) 902-1000"
]
},
{
"name": "Chris Reykdal",
"address": [
{
"line1": "Mail stop: 47200 Old Capitol Building P.O. Box 47200",
"city": "Olympia",
"state": "WA",
"zip": "98504"
}
],
"party": "Nonpartisan",
"phones": [
"(360) 725-6115"
]
}
]
}
Now, do a text search for "Adam Smith" in that JSON. He's a current member of Congress in WA. You'll notice that string is not present, nor are any other members of Congress, nor can I find any reference links in the return to look them up.
What am I missing?? It seems utterly insane to me that everything is included there except members of the U.S. House (but federal and state Senates are included). That means it's possible that the data is available somehow but I'm just not figuring it out. Can anybody help?
Thanks!
This is a couple of months late but you need to specify the to look for the roles legislatorUpperBody and legislatorLowerBody.
If you just want to have these two peices of infomation try running the query
https://www.googleapis.com/civicinfo/v2/representatives?address=[address]&levels=country&roles=legislatorUpperBody&roles=legislatorLowerBody&key=[api_key]
Instead of returning all possible results for representatives for a given area, the API seems to only return exact matches.
Assuming 2 requests that both use levels=country&roles=legislatorLowerBody.
I get 0 results using the address Boston, MA.
I get the proper representative using the address 10 Summer St, Boston, MA.
Congressional districts can be pretty specific. In some cases the street number along the same street will dictate one district versus another.

JSON Objective-c - Objects and Arrays Combination

My JSON url is http://api.kivaws.org/v1/loans/search.json?status=fundraising
My Objective- code displays page_size : 20 using the following code
NSData *allCourseData=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]];
NSError *error;
NSMutableDictionary *JsonObject = [NSJSONSerialization
JSONObjectWithData:allCourseData options:NSJSONReadingMutableContainers
error:&error];
//NSLog(#"json object is: - %#", JsonObject);
//Print Page size
NSLog(#"Page size is %#", [[JsonObject objectForKey:#"paging"] objectForKey:#"page_size"]);
My problem is how do i loop to Loans array to display the following
Example it will display name, display ALL languages, status so output will look like
============================
Name : "Maria Luisa"
languages: es,en
status : "fundraising"
sector : "Retail"
location
country_code : "NI"
Name : "Florence"
languages: en
status : "fundraising"
sector : "Retail"
location
country_code : "KE"
MyJSON Feed
{
"paging": {
"page": 1,
"total": 7279,
"page_size": 20,
"pages": 364
},
"loans": [
{
"id": 941933,
"name": "Maria Luisa",
"description": {
"languages": [
"es",
"en"
]
},
"status": "fundraising",
"funded_amount": 0,
"basket_amount": 0,
"image": {
"id": 1969857,
"template_id": 1
},
"activity": "General Store",
"sector": "Retail",
"use": "to buy rice, chicken, sausages, corn, beans, and sugar.",
"location": {
"country_code": "NI",
"country": "Nicaragua",
"town": "Esteli",
"geo": {
"level": "town",
"pairs": "13.166667 -86.333333",
"type": "point"
}
},
"partner_id": 176,
"posted_date": "2015-09-04T09:20:02Z",
"planned_expiration_date": "2015-10-04T09:20:02Z",
"loan_amount": 600,
"borrower_count": 1,
"lender_count": 0,
"bonus_credit_eligibility": true,
"tags": []
},
{
"id": 942181,
"name": "Florence",
"description": {
"languages": [
"en"
]
},
"status": "fundraising",
"funded_amount": 0,
"basket_amount": 0,
"image": {
"id": 1970181,
"template_id": 1
},
"activity": "Retail",
"sector": "Retail",
"use": "to buy more stocks of maize.",
"location": {
"country_code": "KE",
"country": "Kenya",
"town": "Bondo",
"geo": {
"level": "town",
"pairs": "1 38",
"type": "point"
}
},
"partner_id": 138,
"posted_date": "2015-09-04T09:10:08Z",
"planned_expiration_date": "2015-10-04T09:10:08Z",
"loan_amount": 200,
"borrower_count": 1,
"lender_count": 0,
"bonus_credit_eligibility": true,
"tags": []
},
{
"id": 942146,
"name": "Naziha",
"description": {
"languages": [
"en"
]
},
"status": "fundraising",
"funded_amount": 0,
"basket_amount": 0,
"image": {
"id": 1970129,
"template_id": 1
},
"activity": "Fruits & Vegetables",
"sector": "Food",
"use": "to increase her merchandise and diversify the products in her shop.",
"location": {
"country_code": "LB",
"country": "Lebanon",
"town": "Ain Al Helwi",
"geo": {
"level": "town",
"pairs": "33.833333 35.833333",
"type": "point"
}
},
"partner_id": 77,
"posted_date": "2015-09-04T09:10:02Z",
"planned_expiration_date": "2015-10-04T09:10:02Z",
"loan_amount": 2500,
"borrower_count": 1,
"lender_count": 0,
"bonus_credit_eligibility": true,
"tags": []
},
{
"id": 942180,
"name": "Sokean's Group",
"description": {
"languages": [
"en"
]
},
"status": "fundraising",
"funded_amount": 0,
"basket_amount": 0,
"image": {
"id": 1970180,
"template_id": 1
},
"activity": "Home Appliances",
"sector": "Personal Use",
"themes": [
"Water and Sanitation"
],
"use": "to buy a water filter to provide safe drinking water for her family.",
"location": {
"country_code": "KH",
"country": "Cambodia",
"town": "Kandal",
"geo": {
"level": "town",
"pairs": "13 105",
"type": "point"
}
},
"partner_id": 311,
"posted_date": "2015-09-04T09:00:05Z",
"planned_expiration_date": "2015-10-04T09:00:05Z",
"loan_amount": 100,
"borrower_count": 2,
"lender_count": 0,
"bonus_credit_eligibility": false,
"tags": [
{
"name": "volunteer_like"
}
]
},
{
"id": 941931,
"name": "Lucia Ingrid",
"description": {
"languages": [
"es",
"en"
]
},
"status": "fundraising",
"funded_amount": 0,
"basket_amount": 0,
"image": {
"id": 1969852,
"template_id": 1
},
"activity": "Personal Housing Expenses",
"sector": "Housing",
"themes": [
"Conflict Zones"
],
"use": "to invest in: electric wires, a fuse box, sockets, and labor to install electricity in her home.",
"location": {
"country_code": "SV",
"country": "El Salvador",
"geo": {
"level": "country",
"pairs": "13.833333 -88.916667",
"type": "point"
}
},
"partner_id": 81,
"posted_date": "2015-09-04T09:00:03Z",
"planned_expiration_date": "2015-10-04T09:00:03Z",
"loan_amount": 500,
"borrower_count": 1,
"lender_count": 0,
"bonus_credit_eligibility": true,
"tags": []
},
{
"id": 941930,
"name": "La Manzanita Group",
"description": {
"languages": [
"es",
"en"
]
},
"status": "fundraising",
"funded_amount": 0,
"basket_amount": 0,
"image": {
"id": 1969849,
"template_id": 1
},
"activity": "Fruits & Vegetables",
"sector": "Food",
"use": "to invest in buying papayas and nance berries in bulk.",
"location": {
"country_code": "NI",
"country": "Nicaragua",
"town": "Masaya",
"geo": {
"level": "town",
"pairs": "11.966667 -86.1",
"type": "point"
}
},
"partner_id": 120,
"posted_date": "2015-09-04T08:50:02Z",
"planned_expiration_date": "2015-10-04T08:50:02Z",
"loan_amount": 450,
"borrower_count": 3,
"lender_count": 0,
"bonus_credit_eligibility": true,
"tags": []
},
{
"id": 941926,
"name": "Manuel",
"description": {
"languages": [
"es",
"en"
]
},
"status": "fundraising",
"funded_amount": 0,
"basket_amount": 0,
"image": {
"id": 1969843,
"template_id": 1
},
"activity": "Property",
"sector": "Housing",
"use": "to buy two acres of land.",
"location": {
"country_code": "GT",
"country": "Guatemala",
"town": "Solola",
"geo": {
"level": "town",
"pairs": "14.666667 -91.25",
"type": "point"
}
},
"partner_id": 246,
"posted_date": "2015-09-04T08:10:02Z",
"planned_expiration_date": "2015-10-04T08:10:02Z",
"loan_amount": 800,
"borrower_count": 1,
"lender_count": 0,
"bonus_credit_eligibility": true,
"tags": [
{
"name": "volunteer_like"
}
]
},
{
"id": 942158,
"name": "Tahira",
"description": {
"languages": [
"en"
]
},
"status": "fundraising",
"funded_amount": 150,
"basket_amount": 0,
"image": {
"id": 1969161,
"template_id": 1
},
"activity": "Sewing",
"sector": "Services",
"themes": [
"Underfunded Areas"
],
"use": "buy a new sewing machine for her clothes sewing center.",
"location": {
"country_code": "PK",
"country": "Pakistan",
"town": "Multan",
"geo": {
"level": "town",
"pairs": "30.195556 71.475278",
"type": "point"
}
},
"partner_id": 247,
"posted_date": "2015-09-04T07:00:05Z",
"planned_expiration_date": "2015-10-04T07:00:05Z",
"loan_amount": 400,
"borrower_count": 1,
"lender_count": 5,
"bonus_credit_eligibility": false,
"tags": []
},
{
"id": 942166,
"name": "Sareoun's Group",
"description": {
"languages": [
"en"
]
},
"status": "fundraising",
"funded_amount": 0,
"basket_amount": 0,
"image": {
"id": 1970161,
"template_id": 1
},
"activity": "Home Appliances",
"sector": "Personal Use",
"themes": [
"Water and Sanitation"
],
"use": "to buy a water filter to provide safe drinking water for their family.\t.",
"location": {
"country_code": "KH",
"country": "Cambodia",
"town": "Kandal",
"geo": {
"level": "town",
"pairs": "13 105",
"type": "point"
}
},
"partner_id": 311,
"posted_date": "2015-09-04T07:00:05Z",
"planned_expiration_date": "2015-10-04T07:00:05Z",
"loan_amount": 225,
"borrower_count": 5,
"lender_count": 0,
"bonus_credit_eligibility": false,
"tags": []
},
{
"id": 942156,
"name": "Aiza",
"description": {
"languages": [
"en"
]
},
"status": "fundraising",
"funded_amount": 75,
"basket_amount": 0,
"image": {
"id": 1970147,
"template_id": 1
},
"activity": "Farming",
"sector": "Agriculture",
"use": "to buy fertilizers and other farm supplies",
"location": {
"country_code": "PH",
"country": "Philippines",
"town": "Kalibo, Aklan",
"geo": {
"level": "town",
"pairs": "13 122",
"type": "point"
}
},
"partner_id": 145,
"posted_date": "2015-09-04T07:00:03Z",
"planned_expiration_date": "2015-10-04T07:00:03Z",
"loan_amount": 225,
"borrower_count": 1,
"lender_count": 2,
"bonus_credit_eligibility": true,
"tags": []
},
{
"id": 942160,
"name": "Magu Group",
"description": {
"languages": [
"en"
]
},
"status": "fundraising",
"funded_amount": 0,
"basket_amount": 0,
"image": {
"id": 1970122,
"template_id": 1
},
"activity": "Used Clothing",
"sector": "Clothing",
"use": "to buy fast moving second hand clothes to meet her customer requirement.",
"location": {
"country_code": "TZ",
"country": "Tanzania",
"town": "Dar es Salaam",
"geo": {
"level": "town",
"pairs": "-6.8 39.283333",
"type": "point"
}
},
"partner_id": 87,
"posted_date": "2015-09-04T07:00:02Z",
"planned_expiration_date": "2015-10-04T07:00:02Z",
"loan_amount": 525,
"borrower_count": 2,
"lender_count": 0,
"bonus_credit_eligibility": true,
"tags": []
},
{
"id": 942142,
"name": "Mwanamiraji",
"description": {
"languages": [
"en"
]
},
"status": "fundraising",
"funded_amount": 0,
"basket_amount": 0,
"image": {
"id": 1970130,
"template_id": 1
},
"activity": "Food Stall",
"sector": "Food",
"use": "to purchase a stock of tomatoes, onions, mangoes and green vegetables for resale",
"location": {
"country_code": "KE",
"country": "Kenya",
"town": "Tiribe",
"geo": {
"level": "town",
"pairs": "1 38",
"type": "point"
}
},
"partner_id": 164,
"posted_date": "2015-09-04T06:30:04Z",
"planned_expiration_date": "2015-10-04T06:30:04Z",
"loan_amount": 200,
"borrower_count": 1,
"lender_count": 0,
"bonus_credit_eligibility": true,
"tags": []
},
{
"id": 940065,
"name": "Tubogorane Group",
"description": {
"languages": [
"fr",
"en"
]
},
"status": "fundraising",
"funded_amount": 0,
"basket_amount": 0,
"image": {
"id": 1967147,
"template_id": 1
},
"activity": "Food",
"sector": "Food",
"themes": [
"Conflict Zones"
],
"use": "to increase his working capital and buy a large quantity of fruit and cheese for sale.",
"location": {
"country_code": "BI",
"country": "Burundi",
"town": "Bujumbura",
"geo": {
"level": "town",
"pairs": "-3.5 30",
"type": "point"
}
},
"partner_id": 162,
"posted_date": "2015-09-04T06:20:05Z",
"planned_expiration_date": "2015-10-04T06:20:05Z",
"loan_amount": 3050,
"borrower_count": 19,
"lender_count": 0,
"bonus_credit_eligibility": false,
"tags": []
},
{
"id": 940076,
"name": "Wend Loiim Taore 2 Group",
"description": {
"languages": [
"fr",
"en"
]
},
"status": "fundraising",
"funded_amount": 50,
"basket_amount": 0,
"image": {
"id": 1967173,
"template_id": 1
},
"activity": "Clothing Sales",
"sector": "Clothing",
"use": "to buy pagnes (traditional, wrap-around clothing) and soap for sale.",
"location": {
"country_code": "BF",
"country": "Burkina Faso",
"town": "Ouagadougou Boulmiougou",
"geo": {
"level": "town",
"pairs": "13 -2",
"type": "point"
}
},
"partner_id": 398,
"posted_date": "2015-09-04T06:20:05Z",
"planned_expiration_date": "2015-10-04T06:20:05Z",
"loan_amount": 1850,
"borrower_count": 5,
"lender_count": 2,
"bonus_credit_eligibility": false,
"tags": []
},
{
"id": 942145,
"name": "Adham",
"description": {
"languages": [
"en"
]
},
"status": "fundraising",
"funded_amount": 25,
"basket_amount": 0,
"image": {
"id": 1970127,
"template_id": 1
},
"activity": "Barber Shop",
"sector": "Services",
"use": "to buy new salon equipment.",
"location": {
"country_code": "PS",
"country": "Palestine",
"town": "Bethlehem",
"geo": {
"level": "town",
"pairs": "31.92157 35.203285",
"type": "point"
}
},
"partner_id": 289,
"posted_date": "2015-09-04T06:10:04Z",
"planned_expiration_date": "2015-10-04T06:10:04Z",
"loan_amount": 3000,
"borrower_count": 1,
"lender_count": 2,
"bonus_credit_eligibility": false,
"tags": [
{
"name": "user_favorite"
}
]
},
{
"id": 941929,
"name": "Silvia Yesenia",
"description": {
"languages": [
"es",
"en"
]
},
"status": "fundraising",
"funded_amount": 0,
"basket_amount": 0,
"image": {
"id": 1969848,
"template_id": 1
},
"activity": "Retail",
"sector": "Retail",
"themes": [
"Conflict Zones"
],
"use": " to buy vegetables, legumes, staple grains and meat, amongst other items, to offer a better service to her customers",
"location": {
"country_code": "SV",
"country": "El Salvador",
"geo": {
"level": "country",
"pairs": "13.833333 -88.916667",
"type": "point"
}
},
"partner_id": 81,
"posted_date": "2015-09-04T06:10:03Z",
"planned_expiration_date": "2015-10-04T06:10:03Z",
"loan_amount": 500,
"borrower_count": 1,
"lender_count": 0,
"bonus_credit_eligibility": true,
"tags": []
}
]
}
Well the loans array is in the top-level JSON object, so:
NSArray *loans = jsonObject[#"loans"];
for (NSDictionary *loan in loans) {
NSNumber *ident = loan[#"id"];
NSString *name = loan[#"name"];
NSDictionary *description = loan[#"description"];
NSDictionary *location = loan[#"location"];
NSString *country = location[#"country"];
// etc.
}
NSData *allCourseData=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]];
NSError *error;
NSMutableDictionary *JsonObject = [NSJSONSerialization
JSONObjectWithData:allCourseData options:NSJSONReadingMutableContainers
error:&error];
// Get loans array from JsonObject
NSArray *loanJsonObject = JsonObject[#"loans"];
// Create a new loans array for store the new items
NSMutableArray *newLoans = [NSMutableArray array];
// Loop loanJsonObject to get items and create new ones
// for then store them in the new loans array
for (NSDictionary *loan in loanJsonObject) {
NSDictionary *newLoan = #{
#"name": loan[#"name"],
#"status": loan[#"status"],
#"sector": loan[#"sector"],
#"languages": loan[#"description"][#"languages"],
#"location": #{
#"country": loan[#"location"][#"country"],
#"geo": #{
#"pairs": loan[#"location"][#"geo"][#"pairs"]
}
}
};
[newLoans addObject:newLoan];
}
// the new loans
NSLog(#"newLoans: %#", newLoans);
well your loans object is an array of dictionaries.
By [JsonObject objectForKey:#"loans"] you are getting an array.
then you need to iterate for desired result.
NSArray *arrloans = [JsonObject objectForKey:#"loans"];
for (NSDictionary *dicloans in arrloans) {
NSString *name = [JsonObject objectForKey:#"name"];
NSString *countryCode = [[JsonObject objectForKey:#"location"]objectForKey:#"country_code"]; }