Repetitive entries in SQL JOIN query - sql

I have three tables posts, comments, and users. posts table includes two types of posts, question and answer. Comments are made on questions and answers. My purpose is to get a single question with comments on it, answers on it and comments on that answers. Also I need username from the users table as author on each question, answer and comment I fetch. I am using Postgres 9.5, and making use of json_agg() function.
While example output I need should be something similar to the first following, I get repeated entries.
What is the thing I am missing here? Correct group by clauses may be. Or the subquery to gather the answers with their comments is not the way to do it. When I comment out the left join from posts on the comments table I get desired results without comments on questions. Also when I cancel the left join containing the subquery I get non-repetative results as I expect, and again that is not complete dataset I desire. These are things I have collected so far to solve my issue.
What I need:
[
{
"post_id": "10",
"created_at": "2016-05-10T00:16:54.469Z",
"post_type": "question",
"post_title": "qwerty",
"post_text": "asdasd asda sdasd",
"post_author_id": 1,
"author": "isikfsc",
"parent_post_id": null,
"is_accepted": null,
"acceptor_id": null,
"answers": [
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"author": "isikfsc",
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 17
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"author": "isikfsc",
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 14
}
]
}
],
"comments": [
{
"id": 1,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 10
}
]
}
]
My query is:
SELECT
q.*,
json_agg(ac.*) AS answers,
json_agg(c.*) AS comments --comments on posts of post_id questions
FROM posts q
LEFT JOIN
(
SELECT
a.*,
json_agg(c.*) AS comments -- comments on posts of post_id answers
FROM posts a
LEFT JOIN comments c
ON a.post_id = c.parent_post_id
GROUP BY a.post_id
) ac
ON q.post_id = ac.parent_post_id
LEFT JOIN comments c
ON q.post_id = c.parent_post_id
WHERE q.post_id = 10
GROUP BY q.post_id
What I get:
[
{
"post_id": "10",
"created_at": "2016-05-10T00:16:54.469Z",
"post_type": "question",
"post_title": "qwerty",
"post_text": "asdasd asda sdasd",
"post_author_id": 1,
"parent_post_id": null,
"is_accepted": null,
"acceptor_id": null,
"answers": [
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"parent_post_id": 17
}
]
},
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"parent_post_id": 17
}
]
},
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"parent_post_id": 17
}
]
},
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"parent_post_id": 17
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"parent_post_id": 14
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"parent_post_id": 14
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"parent_post_id": 14
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"parent_post_id": 14
}
]
}
],
"comments": [
{
"id": 1,
"created_at": "2016-05-10T05:25:28.200327",
"text": "asadasdad",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T10:25:23.381177",
"text": "werwer",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 1,
"created_at": "2016-05-10T05:25:28.200327",
"text": "asadasdad",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T10:25:23.381177",
"text": "werwer",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 1,
"created_at": "2016-05-10T05:25:28.200327",
"text": "asadasdad",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T10:25:23.381177",
"text": "werwer",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 1,
"created_at": "2016-05-10T05:25:28.200327",
"text": "asadasdad",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T10:25:23.381177",
"text": "werwer",
"author_id": 1,
"parent_post_id": 10
}
]
}
]

Grouping happens once all of the parties have been joined, so aggregates will depend on the resulting cardinality. Joining posts with answers AND comments causes a full join between them, duplicating all values. They need to be separated and performed individually, one way you can do it is the following:
SELECT
q.*,
(SELECT json_agg(ac.*)
FROM (
SELECT a.*, json_agg(c.*) AS comments
FROM posts a
LEFT JOIN comments c ON (a.post_id = c.parent_post_id)
WHERE a.parent_post_id = q.post_id
GROUP BY a.post_id
) ac
) AS answers,
json_agg(c.*) AS comments --comments on posts of post_id questions
FROM posts q
LEFT JOIN comments c ON (q.post_id = c.parent_post_id)
WHERE q.post_id = 10
GROUP BY q.post_id;
Alternatively:
SELECT q.*, qa.answers, qc.comments
FROM posts q
LEFT JOIN (
SELECT ac.parent_post_id, json_agg(ac.*) AS answers
FROM (
SELECT ac.*, json_agg(c.*) AS comments
FROM posts ac
LEFT JOIN comments c ON (c.parent_post_id = ac.post_id)
GROUP BY ac.post_id
) ac
GROUP BY ac.parent_post_id
) qa ON (qa.parent_post_id = q.post_id)
LEFT JOIN (
SELECT c.parent_post_id, json_agg(c.*) AS comments
FROM comments c
GROUP BY c.parent_post_id
) qc ON (qc.parent_post_id = q.post_id)
WHERE q.post_id = 10;

Related

Get specific value from json field based on last created_at date

I am using retool, which uses AlaSQL to query JSON, trying to get the value from a field that has multiple items; I only want to return the value of the field stock for the key with the latest created_at date.
select tracked_products
from {{ean.data}}
where created_at=(select MAX(created_at) from {{ean.data}});
I can actually get to the fields I want to like this but I do not understand how to filter further
select tracked_products
from {{ean.data}}
where price < 40
result from above query in plain text
[
{
"id": 172165913,
"offer_data_id": 2208536,
"stock": 2,
"sold": 0,
"revenue": "0.00",
"price": 39.99,
"status": "success",
"created_at": "2022-12-16T12:12:26.000000Z",
"updated_at": "2022-12-16T12:12:26.000000Z"
},
{
"id": 173409443,
"offer_data_id": 2208536,
"stock": 2,
"sold": 0,
"revenue": "0.00",
"price": 34.99,
"status": "success",
"created_at": "2022-12-17T03:15:58.000000Z",
"updated_at": "2022-12-17T03:15:58.000000Z"
},
{
"id": 174659591,
"offer_data_id": 2208536,
"stock": 2,
"sold": 0,
"revenue": "0.00",
"price": 34.99,
"status": "success",
"created_at": "2022-12-18T16:22:45.000000Z",
"updated_at": "2022-12-18T16:22:45.000000Z"
},
{
"id": 175895075,
"offer_data_id": 2208536,
"stock": 2,
"sold": 0,
"revenue": "0.00",
"price": 34.99,
"status": "success",
"created_at": "2022-12-19T03:18:53.000000Z",
"updated_at": "2022-12-19T03:18:53.000000Z"
},
{
"id": 177134025,
"offer_data_id": 2208536,
"stock": 2,
"sold": 0,
"revenue": "0.00",
"price": 34.99,
"status": "success",
"created_at": "2022-12-20T15:35:48.000000Z",
"updated_at": "2022-12-20T15:35:48.000000Z"
},
{
"id": 178391290,
"offer_data_id": 2208536,
"stock": 2,
"sold": 0,
"revenue": "0.00",
"price": 34.99,
"status": "success",
"created_at": "2022-12-21T03:09:22.000000Z",
"updated_at": "2022-12-21T03:09:22.000000Z"
},
{
"id": 179654380,
"offer_data_id": 2208536,
"stock": 1,
"sold": 1,
"revenue": "34.99",
"price": 34.99,
"status": "success",
"created_at": "2022-12-22T03:13:50.000000Z",
"updated_at": "2022-12-22T03:13:50.000000Z"
},
{
"id": 180918092,
"offer_data_id": 2208536,
"stock": 1,
"sold": 0,
"revenue": "0.00",
"price": 34.99,
"status": "success",
"created_at": "2022-12-23T03:19:42.000000Z",
"updated_at": "2022-12-23T03:19:42.000000Z"
}
]
try this :
SELECT DISTINCT
(array_agg(j->>'stock') OVER (PARTITION BY tracked_products ORDER BY j->>'created_at' DESC)[1] AS stock
FROM ean.data
CROSS JOIN LATERAL jsonb_path_query(tracked_products :: jsonb, '$[*]') AS j
WHERE price < 40
This query relies on a text sorting on created_at which should be compliant with the actual date format of this text field.
jsonb_path_query is used instead of json_array_elements so that to avoid an error when tracked_products is not a json of type array.
see dbfiddle
Refer to the manual for more info about json manipulation

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

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

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.

using bookshelfjs to order results by a related table column

I have two tables I am querying with bookshelfjs using 'withRelated'. How do I sort the results by a column in the related table? For example I want to order all the results by rankingLinks.id
.fetchAll({withRelated: ['tickerSymbolLinks.tickerSymbol', 'rankingLinks', 'logoLinks.logo']})
Seems simple but banging my head against the wall on this one...
You can actually order stuff by withRelated, however, it will only order the result within this specific property. I.e.:
new Participant().query(function(qb) {
qb.orderBy('participants.id', 'desc'); // You don't have access to any "withRelated" models
}).fetchAll({
withRelated: [{
'courses': function(qb) {
qb.orderBy('courses.id', 'desc');
}
}]
}).then(function(result) {
res.json(result.toJSON());
});
This code returns all participants, order by their id (descending). Within this result, I order courses by courses.id desc. The result is as follows:
[
{
"id": 3,
"name": "Susan Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"courses": [
{
"id": 3,
"course_name": "Advanced JavaScript",
"duration": 80,
"_pivot_participant_id": 3,
"_pivot_course_id": 3,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 1,
"course_name": "iOS development",
"duration": 120,
"_pivot_participant_id": 3,
"_pivot_course_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
},
{
"id": 2,
"name": "Richard Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"courses": [
{
"id": 2,
"course_name": "Android development",
"duration": 60,
"_pivot_participant_id": 2,
"_pivot_course_id": 2,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 1,
"course_name": "iOS development",
"duration": 120,
"_pivot_participant_id": 2,
"_pivot_course_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
},
{
"id": 1,
"name": "John Doe",
"created_at": 1429685077495,
"updated_at": 1429685077495,
"courses": [
{
"id": 3,
"course_name": "Advanced JavaScript",
"duration": 80,
"_pivot_participant_id": 1,
"_pivot_course_id": 3,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 1,
"course_name": "iOS development",
"duration": 120,
"_pivot_participant_id": 1,
"_pivot_course_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
}
]
Now if I'd like to order the results by courses, I'd have to change the query a bit. I.e.:
new Course().query(function(qb) {
qb.orderBy('courses.id', 'desc');
}).fetchAll({
withRelated: ['participants']
}).then(function(result) {
res.json(result.toJSON());
});
This produces a slightly different result, but with desired ordering:
[
{
"id": 3,
"course_name": "Advanced JavaScript",
"duration": 80,
"participants": [
{
"id": 1,
"name": "John Doe",
"created_at": 1429685077495,
"updated_at": 1429685077495,
"_pivot_course_id": 3,
"_pivot_participant_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 3,
"name": "Susan Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"_pivot_course_id": 3,
"_pivot_participant_id": 3,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
},
{
"id": 2,
"course_name": "Android development",
"duration": 60,
"participants": [
{
"id": 2,
"name": "Richard Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"_pivot_course_id": 2,
"_pivot_participant_id": 2,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
},
{
"id": 1,
"course_name": "iOS development",
"duration": 120,
"participants": [
{
"id": 1,
"name": "John Doe",
"created_at": 1429685077495,
"updated_at": 1429685077495,
"_pivot_course_id": 1,
"_pivot_participant_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 2,
"name": "Richard Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"_pivot_course_id": 1,
"_pivot_participant_id": 2,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 3,
"name": "Susan Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"_pivot_course_id": 1,
"_pivot_participant_id": 3,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
}
]
I hope this helps a bit!

Problems parsing generated JSON

I got a JSON object that I have generated via Rails 3 (via my API). I need to either put a named surrounding tag around it or loose the first "layer". I know it sounds very strange, but I cannot loop it in the client.
This is the parsing code:
#results = RestClient.get "http://localhost:5000/emails?token=#{get_token}", :accept => :json
#array = JSON.parse(#results);
Turn this:
[
{
"email": {
"created_at": "2011-03-02T12:23:59Z",
"updated_at": "2011-03-02T12:23:59Z",
"value_1": "intevisa#sss.com",
"value_2": null,
"id": 4,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "intevisa",
"privacy": null
}
},
{
"email": {
"created_at": "2011-03-02T15:19:39Z",
"updated_at": "2011-03-02T15:19:39Z",
"value_1": "another#yahoo.com",
"value_2": null,
"id": 5,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "Some text",
"privacy": null
}
},
{
"email": {
"created_at": "2011-03-02T15:20:17Z",
"updated_at": "2011-03-02T15:20:17Z",
"value_1": "my#email.com",
"value_2": null,
"id": 6,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "Some text",
"privacy": null
}
},
{
"email": {
"created_at": "2011-03-02T15:21:03Z",
"updated_at": "2011-03-02T15:21:03Z",
"value_1": "An email#google.com",
"value_2": null,
"id": 7,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "Hello world",
"privacy": null
}
}
]
Into this:
[
"email": {
"created_at": "2011-03-02T12:23:59Z",
"updated_at": "2011-03-02T12:23:59Z",
"value_1": "intevisa#sss.com",
"value_2": null,
"id": 4,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "intevisa",
"privacy": null
},
"email": {
"created_at": "2011-03-02T15:19:39Z",
"updated_at": "2011-03-02T15:19:39Z",
"value_1": "another#yahoo.com",
"value_2": null,
"id": 5,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "Some text",
"privacy": null
},
"email": {
"created_at": "2011-03-02T15:20:17Z",
"updated_at": "2011-03-02T15:20:17Z",
"value_1": "my#email.com",
"value_2": null,
"id": 6,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "Some text",
"privacy": null
},
"email": {
"created_at": "2011-03-02T15:21:03Z",
"updated_at": "2011-03-02T15:21:03Z",
"value_1": "An email#google.com",
"value_2": null,
"id": 7,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "Hello world",
"privacy": null
}
]
This is how I try to loop through it in the client.
<% #array['email'].each do |item| %>
<%= item['value_1'] %>
<% end %>
Try the following in config > initializers > wrap_parameters.rb
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActionController::Base.wrap_parameters format: [:json]
# Disable root element in JSON by default.
if defined?(ActiveRecord)
ActiveRecord::Base.include_root_in_json = false
end
By setting the "include_root_in_json" to false, it should get you the desired output