Sequelize distinct in include throws syntax error at or near \"DISTINCT\" - express

I am trying to filter out the duplicated row after performing an include
My code
const courses = await Course.findAll({
subQuery: false,
order: [ [ 'courseID', 'ASC' ] ],
include: [
{
model: CourseAvailable,
as: 'courseAvailable',
required: false
}
]
});
Output
{
"success": true,
"data": [
{
"id": 132,
"courseID": "CSS112",
"courseName": "COMPUTER PROGRAMMING",
"required": null,
"createdAt": "2020-05-21T07:29:58.596Z",
"updatedAt": "2020-05-21T07:29:58.596Z",
"courseAvailable": [
{
"id": 508,
"courseID": "CSS112",
"semester": 2,
"credit": 3,
"totalSeat": 55,
"section": "1",
"allowedGroup": "CSS 1 A(0)",
"day": "อ.",
"start": "08.30",
"end": "10.30",
"classroom": "SCL607",
"createdAt": "2020-05-21T07:30:10.417Z",
"updatedAt": "2020-05-21T07:30:10.417Z"
},
{
"id": 509,
"courseID": "CSS112",
"semester": 2,
"credit": 3,
"totalSeat": 55,
"section": "1",
"allowedGroup": "OTHER (0)",
"day": "อ.",
"start": "08.30",
"end": "10.30",
"classroom": "SCL607",
"createdAt": "2020-05-21T07:30:10.417Z",
"updatedAt": "2020-05-21T07:30:10.417Z"
},
{
"id": 510,
"courseID": "CSS112",
"semester": 2,
"credit": 3,
"totalSeat": 55,
"section": "1",
"allowedGroup": "OTHER (0)",
"day": "อ.",
"start": "10.30",
"end": "12.30",
"classroom": "SCL703",
"createdAt": "2020-05-21T07:30:10.417Z",
"updatedAt": "2020-05-21T07:30:10.417Z"
}
]
}
]
}
As you can see in the courseAvailable it returned 3 elements, And I wanted to do the distinction by distinct the element if the (section,start,end) are the same
so this is my expected output
{
"success": true,
"data": [
{
"id": 132,
"courseID": "CSS112",
"courseName": "COMPUTER PROGRAMMING",
"required": null,
"createdAt": "2020-05-21T07:29:58.596Z",
"updatedAt": "2020-05-21T07:29:58.596Z",
"courseAvailable": [
{
"id": 508,
"courseID": "CSS112",
"semester": 2,
"credit": 3,
"totalSeat": 55,
"section": "1",
"allowedGroup": "CSS 1 A(0)",
"day": "อ.",
"start": "08.30",
"end": "10.30",
"classroom": "SCL607",
"createdAt": "2020-05-21T07:30:10.417Z",
"updatedAt": "2020-05-21T07:30:10.417Z"
},
{
"id": 510,
"courseID": "CSS112",
"semester": 2,
"credit": 3,
"totalSeat": 55,
"section": "1",
"allowedGroup": "OTHER (0)",
"day": "อ.",
"start": "10.30",
"end": "12.30",
"classroom": "SCL703",
"createdAt": "2020-05-21T07:30:10.417Z",
"updatedAt": "2020-05-21T07:30:10.417Z"
}
]
}
]
}
(MY EXPECTATION) You can see that the id 509 is disappeared because the (section,start,end) have been queried by the distinct
What I've tried
const courses = await Course.findAll({
subQuery: false,
order: [ [ 'courseID', 'ASC' ] ],
include: [
{
model: CourseAvailable,
as: 'courseAvailable',
attributes: [ [ sequelize.fn('DISTINCT', sequelize.col('section')), 'section' ] ].concat(
Object.keys(CourseAvailable.rawAttributes)
), // was trying to do distinct only one column for checking if the distinct working or not working at all, and I am not sure if this is the right way to do it also, after I've been researching it for a day but found nothings that will solves my problem
required: false
}
]
});
But it threw me an error => syntax error at or near \"DISTINCT\".
I also tried using sequelize.literal to do the distinct as well but the error still the same

Related

Is it correct to see a different cabin value aside that passed in the get FlightOffers APi as travelerClass?

From the documentation of the Get Route for the flight offers Api, I can add travelerClass as a filter to make a request. If I add say travelerClass as ECONOMY?
I expect to see only offers with ECONOMY. However I see that within the fareDetailsBySegment section in the result, I see that some Offers have their cabin values as other than ECONOMY. I see some with value of BUSINESS while others as PREMIUM_ECONOMY.
Is this proper?
My Concern
I want to be able to get all bookings that where ECONOMY based only. So I need to be sure that the flight offers returned in the first place are strictly limited by the travelerClass.
I will appreciate any clarity on the this issue. Also if there is a better way to go about this concern above, it will be most appreciated.
An example of my request is below:
Amadeus.shopping.flightOffersSearch.get({
originLocationCode: 'SYD',
destinationLocationCode: 'BKK',
departureDate: '2022-11-01',
adults: '1',
travelClass: 'ECONOMY'
}).then(...)
Offer with BUSINESS instead of ECONOMY
{
"type": "flight-offer",
"id": "22",
"source": "GDS",
"instantTicketingRequired": false,
"nonHomogeneous": false,
"oneWay": false,
"lastTicketingDate": "2022-11-01",
"numberOfBookableSeats": 6,
"itineraries": [
{
"duration": "PT28H20M",
"segments": [
{
"departure": {
"iataCode": "SYD",
"terminal": "1",
"at": "2022-11-01T12:00:00"
},
"arrival": {
"iataCode": "PVG",
"terminal": "2",
"at": "2022-11-01T19:30:00"
},
"carrierCode": "MU",
"number": "562",
"aircraft": {
"code": "77W"
},
"operating": {
"carrierCode": "MU"
},
"duration": "PT10H30M",
"id": "13",
"numberOfStops": 0,
"blacklistedInEU": false
},
{
"departure": {
"iataCode": "PVG",
"terminal": "1",
"at": "2022-11-02T08:45:00"
},
"arrival": {
"iataCode": "BKK",
"at": "2022-11-02T12:20:00"
},
"carrierCode": "MU",
"number": "541",
"aircraft": {
"code": "320"
},
"operating": {
"carrierCode": "MU"
},
"duration": "PT4H35M",
"id": "14",
"numberOfStops": 0,
"blacklistedInEU": false
}
]
}
],
"price": {
"currency": "EUR",
"total": "4048.84",
"base": "3858.00",
"fees": [
{
"amount": "0.00",
"type": "SUPPLIER"
},
{
"amount": "0.00",
"type": "TICKETING"
}
],
"grandTotal": "4048.84"
},
"pricingOptions": {
"fareType": [
"PUBLISHED"
],
"includedCheckedBagsOnly": true
},
"validatingAirlineCodes": [
"MU"
],
"travelerPricings": [
{
"travelerId": "1",
"fareOption": "STANDARD",
"travelerType": "ADULT",
"price": {
"currency": "EUR",
"total": "4048.84",
"base": "3858.00"
},
"fareDetailsBySegment": [
{
"segmentId": "13",
"cabin": "ECONOMY",
"fareBasis": "YSE0WDNQ",
"class": "Y",
"includedCheckedBags": {
"quantity": 2
}
},
{
"segmentId": "14",
"cabin": "BUSINESS",
"fareBasis": "QSE0WCNL",
"class": "Q",
"includedCheckedBags": {
"quantity": 2
}
}
]
}
]
}
in order to apply the filter for cabin class, I suggest you to try with POST method.
in POST method has more search criteria you can apply, below example is something that I have tried and it works: response only contains ECONOMY classes by updating values under cabinRestrictions.
details of the request model is available in API reference page
amadeus.shopping.flightOffersSearch.post(JSON.stringify({
"currencyCode": "USD",
"originDestinations": [
{
"id": "1",
"originLocationCode": "SYD",
"destinationLocationCode": "BKK",
"departureDateTimeRange": {
"date": "2022-11-01"
}
}
],
"travelers": [
{
"id": "1",
"travelerType": "ADULT"
}
],
"sources": [
"GDS"
],
"searchCriteria": {
"maxFlightOffers": 10,
"flightFilters": {
"cabinRestrictions": [
{
"cabin": "ECONOMY",
"coverage": "ALL_SEGMENTS",
"originDestinationIds": [
1
]
}
]
}
}
})).then(...)

Include with where condition , but keep all result before including

I am having a problem in querying inside the include
What I do is
const courses = await Course.findAll({
subQuery: false,
order: [ [ 'courseID', 'ASC' ] ],
include: [
{
model: CourseAvailable,
as: 'courseAvailable',
where: {
id: 350 // dummy condition only for asking here ( I had my purpose )
}
}
]
});
and the output
{
"success": true,
"data": [
{
"id": 20,
"courseID": "MTH102",
"courseName": "MATHEMATICS II",
"required": "{MTH101}",
"createdAt": "2020-05-19T17:30:44.085Z",
"updatedAt": "2020-05-19T17:30:44.085Z",
"courseAvailable": [
{
"id": 350,
"courseID": "MTH102",
"semester": 2,
"credit": 3,
"totalSeat": 150,
"section": "7",
"allowedGroup": "EET 1 A(0)",
"day": "จ.",
"start": "10.30",
"end": "12.30",
"classroom": "CB1103",
"createdAt": "2020-05-20T15:22:50.176Z",
"updatedAt": "2020-05-20T15:22:50.176Z"
}
]
}
]
}
From the output, I know that querying inside the include will only return the courses that found with the query inside the include ( as you can see there is only 1 Course in the returned data )
But I still want to query inside the include to make the include show only if it meets my condition ( like filtering the include )
But the thing is I still want to keep my other courses also which doesn't meet any condition
Expected Output
{
"success": true,
"data": [
{
"id": 19,
"courseID": "MTH101",
"courseName": "MATHEMATICS I",
"required": null,
"createdAt": "2020-05-19T17:30:44.085Z",
"updatedAt": "2020-05-19T17:30:44.085Z",
"courseAvailable": [] // showing empty array if none has met my query => still keep the data above
},
{
"id": 20,
"courseID": "MTH102",
"courseName": "MATHEMATICS II",
"required": "{MTH101}",
"createdAt": "2020-05-19T17:30:44.085Z",
"updatedAt": "2020-05-19T17:30:44.085Z",
"courseAvailable": [
{
"id": 350,
"courseID": "MTH102",
"semester": 2,
"credit": 3,
"totalSeat": 150,
"section": "7",
"allowedGroup": "EET 1 A(0)",
"day": "จ.",
"start": "10.30",
"end": "12.30",
"classroom": "CB1103",
"createdAt": "2020-05-20T15:22:50.176Z",
"updatedAt": "2020-05-20T15:22:50.176Z"
}
]
}
]
}
When using where inside an include, Sequilize will perform an inner join, as mentioned here. You would need to set the attribute required: false inside your include to perform a left join. Change your query to
const courses = await Course.findAll({
subQuery: false,
order: [ [ 'courseID', 'ASC' ] ],
include: [
{
model: CourseAvailable,
as: 'courseAvailable',
where: {
id: 350 // dummy condition only for asking here ( I had my purpose )
},
required: false
}
]
});

Decode bitcoin transaction vin address

I'm building a graph of blockchain transactions and have a problem to extract source address for a transaction (output addresses are easy,bundled in the getrawtransaction result).
for example this transaction https://www.blockchain.com/btc/tx/8d787db12e04630a10191772f068fae43d30fbb93e6198e7efa636ce03f16222?show_adv=true
the getrawtransaction has an output of
{
"result": {
"txid": "8d787db12e04630a10191772f068fae43d30fbb93e6198e7efa636ce03f16222",
"hash": "7a0a667bf96d65a11eeab8ec17e232c5afdbe724b6e5e60697790bde3037c2c8",
"version": 2,
"size": 248,
"vsize": 166,
"weight": 662,
"locktime": 597359,
"vin": [
{
"txid": "2a1dda8fb17cdcefb26a377492aff6076bd03a93f742d595df1d0ff4951a4305",
"vout": 0,
"scriptSig": {
"asm": "00140d9cad5ccd91bf6ed56ec6bcb7762ec52ecb7b8d",
"hex": "1600140d9cad5ccd91bf6ed56ec6bcb7762ec52ecb7b8d"
},
"txinwitness": [
"3045022100f2900ede33aab8838464cfb127a962960c29cd97ba62e830c69a38621514d2980220706aa5c28d6e1a134f74b99e6280b357dc1f20ae00f4fcca12ab4ccfab5ceac701",
"033b18a261c30edaf859dc523700f3a6085a413ab51b4f63ca84d26073e8e7c709"
],
"sequence": 4294967294
}
],
"vout": [
{
"value": 0.16957303,
"n": 0,
"scriptPubKey": {
"asm": "OP_HASH160 d45c146bdb6ad9c61f3300341e2524e8e64394ac OP_EQUAL",
"hex": "a914d45c146bdb6ad9c61f3300341e2524e8e64394ac87",
"reqSigs": 1,
"type": "scripthash",
"addresses": [
"3M3sV2VBqPXzZHkbdBXo9dMpEw7xxHCFow"
]
}
},
{
"value": 0.07866618,
"n": 1,
"scriptPubKey": {
"asm": "OP_HASH160 69f37417a9fb77744ec258ac2ff22e99266ac354 OP_EQUAL",
"hex": "a91469f37417a9fb77744ec258ac2ff22e99266ac35487",
"reqSigs": 1,
"type": "scripthash",
"addresses": [
"3BMEX8XxUxyEKfRDAJmSMhyd3Y7QWh7PQV"
]
}
}
],
"hex": "0200000000010105431a95f40f1ddf95d542f7933ad06b07f6af9274376ab2efdc7cb18fda1d2a00000000171600140d9cad5ccd91bf6ed56ec6bcb7762ec52ecb7b8dfeffffff0277bf02010000000017a914d45c146bdb6ad9c61f3300341e2524e8e64394ac87fa0878000000000017a91469f37417a9fb77744ec258ac2ff22e99266ac3548702483045022100f2900ede33aab8838464cfb127a962960c29cd97ba62e830c69a38621514d2980220706aa5c28d6e1a134f74b99e6280b357dc1f20ae00f4fcca12ab4ccfab5ceac70121033b18a261c30edaf859dc523700f3a6085a413ab51b4f63ca84d26073e8e7c7096f1d0900",
"blockhash": "00000000000000000002726a175241b0f200c561daf30c668d8652cf3b949343",
"confirmations": 18,
"time": 1569914555,
"blocktime": 1569914555
},
"error": null,
"id": null
}
the source address per blockchain explorer is 36UuEDoDQX5JppSVnpaccV5DCKiquRyPxB. Was not able to derive it from any of the vin fields.
You can decode scriptSig to get the address.
There is a RPC call in bitcoin core that can help you achieve this: decodescript.

Stock Quantity of ProductVariant not decreased in shopify shop when placing order via API

I am posting the following Order to the shopify api Order endpoint. The Order shows up in the shop and everything works as it should, except that the stock quantity of the variants in orders placed via the API are not decreased automatically by shopify. When I place an order within the admin console, they are decreased automatically. Shopify inventory tracking is turned on for the products. Any ideas would be greatly appreciated.
{
"order": {
"email": "someName#yahoo.com",
"financial_status": "paid",
"fulfillment_status": null,
"send_receipt": true,
"send_fulfillment_receipt": true,
"note": "Created by someName",
"line_items": [
{
"variant_id": 21718275463,
"quantity": 1,
"price": 99,
"requires_shipping": true,
"product_id": 6820646151
},
{
"variant_id": 21717700871,
"quantity": 1,
"price": 1000,
"requires_shipping": true,
"product_id": 6820646151
},
{
"variant_id": 21717690055,
"quantity": 1,
"price": 555,
"requires_shipping": true,
"product_id": 6821668807
}
],
"processing_method": "offsite",
"shipping_address": {
"first_name": "Chris",
"address1": "111 Love Road",
"phone": "9999999999",
"city": "St. Louis",
"zip": "63123",
"province": "MO",
"country": "United States",
"last_name": "Becker",
"name": "Chris Becker",
"country_code": "US",
"province_code": "MO"
},
"source_name": "someName",
"taxes_included": false,
"shipping_lines": [
{
"title": "standard",
"price": 0.00,
"code": null,
"source": "brand owner on shopify",
"carrier_identifier": null,
"tax_lines": null
}
],
"tags": "someName"
}
}
{
"variant": {
"id": 21718275463,
"product_id": 6820646151,
"title": "m / red",
"price": "99.00",
"sku": "",
"position": 2,
"grams": 0,
"inventory_policy": "deny",
"compare_at_price": "900.00",
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "m",
"option2": "red",
"option3": null,
"created_at": "2016-05-27T13:16:26-04:00",
"updated_at": "2016-05-28T13:28:20-04:00",
"taxable": false,
"barcode": "",
"image_id": 13217378823,
"inventory_quantity": 1,
"weight": 0,
"weight_unit": "lb",
"old_inventory_quantity": 1,
"requires_shipping": true
}
}
{
"variant": {
"id": 21717700871,
"product_id": 6820646151,
"title": "s / green",
"price": "1000.00",
"sku": "",
"position": 1,
"grams": 0,
"inventory_policy": "deny",
"compare_at_price": "1111.00",
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "s",
"option2": "green",
"option3": null,
"created_at": "2016-05-27T13:05:56-04:00",
"updated_at": "2016-05-28T12:17:22-04:00",
"taxable": true,
"barcode": "",
"image_id": 13160712135,
"inventory_quantity": 2,
"weight": 0,
"weight_unit": "lb",
"old_inventory_quantity": 2,
"requires_shipping": true
}
}
{
"variant": {
"id": 21717690055,
"product_id": 6821668807,
"title": "Default Title",
"price": "555.00",
"sku": "",
"position": 1,
"grams": 0,
"inventory_policy": "deny",
"compare_at_price": "666.00",
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "Default Title",
"option2": null,
"option3": null,
"created_at": "2016-05-27T13:05:39-04:00",
"updated_at": "2016-05-28T12:17:22-04:00",
"taxable": true,
"barcode": "",
"image_id": null,
"inventory_quantity": 2,
"weight": 0,
"weight_unit": "lb",
"old_inventory_quantity": 2,
"requires_shipping": true
}
}
You created a fake order using the API. Fake orders like that don't transact money or trigger the usual internal checks and balances without some extra effort. Maybe if you tried adding a fulfillment to the order, Shopify might ding inventory levels? Seems like something to try anyway.

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"]; }