i have an json response like this :
{
"result": [
{
"id": 2984,
"amount": 5000000,
"account": "money",
"trade_no": "2121683414670617655",
"type": 0,
"for_id": 0,
"created_at": "2021-02-16 20:50:14",
"payment_method": 0,
"status": 1
},
{
"id": 2999,
"amount": -450000,
"account": "money",
"trade_no": "212173166272246C118",
"type": 5,
"for_id": "2021021760479",
"created_at": "2021-01-17 10:14:22",
"payment_method": "0",
"status": 1
},
],
"code": 200,
"description": "OK"
}
and then i want grouping the api by month and year look like this :
please help me to solve this
You can use groupBy method from lodash.
var grouped = _.groupBy(result, function(item) {
return item.created_at.substring(0,7);
});
This will give you data in below format:
{
"2021-02": [
{
"id": 2984,
"amount": 5000000,
"account": "money",
"trade_no": "2121683414670617655",
"type": 0,
"for_id": 0,
"created_at": "2021-02-16 20:50:14",
"payment_method": 0,
"status": 1
}
],
"2021-01": [
{
"id": 2999,
"amount": -450000,
"account": "money",
"trade_no": "212173166272246C118",
"type": 5,
"for_id": "2021021760479",
"created_at": "2021-01-17 10:14:22",
"payment_method": "0",
"status": 1
}
]
}
When rendering the data, you can format your Year-Month key to string using moment like this,
moment('2021-01', 'YYYY-MM').format('MMMM YYYY');
Related
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
I need to transform the below JSON
Input :-
{
"type": "donut",
"weight-unit": "lb",
"price-unit": "$/lb",
"price": 10.75,
"batters":
{
"batter":
[
{ "id": "10011", "type": "Original","weight": 500},
{ "id": "10021", "type": "Chocolate","weight": 200, "price": 11.75 },
{ "id": "10031", "type": "Blueberry", "weight": 250, "price": 11.75 },
{ "id": "10041", "type": "Devil's Food", "weight": 150}
]
},
"topping":
[
{ "id": "50011", "type": "None", "price": 0 },
{ "id": "50021", "type": "Glazed", "price": 45.23},
{ "id": "50051", "type": "Sugar", "price": 34.1},
{ "id": "50071", "type": "Powdered Sugar", "price": 21.11},
{ "id": "50061", "type": "Chocolate with Sprinkles", "price": 34.43 },
{ "id": "50031", "type": "Chocolate", "price": 87.40},
{ "id": "50041", "type": "Maple", "price": 64.11}
]
}
The output that I want is
Output :-
{
"type": "donut",
"ChocolateFlavoredGlazedDonut" : {
"weight": 200,
"unit": "kg",
"price": 56.98,
"unit": "$/kg",
},
"ChocolateFlavoredSprinklesDonut" : {
"weight": 200,
"unit": "kg",
"price": 46.18,
"unit": "$/kg",
},
"BlueberryFlavoredSugarDonut" : {
"weight": 250,
"unit": "kg",
"price": 45.85,
"unit": "$/kg",
},
"OriginalGlazedDonut" : {
"weight": 500,
"unit": "kg",
"price": 45.23,
"unit": "$/kg",
},
"OriginalMapleDonut" : {
"weight": 500,
"unit": "kg",
"price": 64.11,
"unit": "$/kg",
},
"OriginalSugarDonut" : {
"weight": 500,
"unit": "kg",
"price": 34.1,
"unit": "$/kg",
},
}
Explanantion:-
"BatterName + ToppingName" : {
"weight": 500(batter weight),
"unit": "kg"(hard coded),
"price": 34.1(batter price + topping price),
"unit": "$/kg"(hard coded,
}
For example if Batter Name is "Chocolate", then there will be 6 toppings for Chocolate batter and so on for each batter. So total batter number is 4 and topping is 8 , I want 32 items in the final output
You basically need a cross join on toppings and batters. You can use join from dw::core::Arrays to do that. It accepts the 2 arrays as input along with two joining criteria (which are inline functions). For that you can just pass a function that always returns true (or any other static value but it should be same in both criteria functions) so the function will merge every item with every item, and you will get all combos possible.
I noticed that the names of the snack after combining is not very straight forward, so I crated a separated function for that.
%dw 2.0
import join from dw::core::Arrays
import capitalize from dw::core::Strings
output application/json
fun getComboName(batterName, toppingName, snackType) =
capitalize(batterName)
++ (if(lower(batterName) != "original")("Flavoured") else "")
++ (if(lower(toppingName) != "none") capitalize((toppingName splitBy " ")[-1]) else "")
++ capitalize(snackType)
---
join(
payload.batters.batter,
payload.topping,
(a) -> true,
(a) -> true
)
reduce ((combo, acc={"type": payload."type"}) -> {
(acc),
(getComboName(combo.l."type", combo.r."type", payload."type")): {
weight: combo.l.weight,
unit: "kg",
price: (combo.l.price default 0) + (combo.r.price default 0),
unit: "\$/kg"
}
})
I'm trying to use the following Square API endpoint to update an item, but I'm getting some errors. Here is the item that I'm trying to update.
{
"type": "ITEM",
"id": "7HXK6NVBDKSOK64CCAYZJ2KW",
"updated_at": "2020-08-08T21:43:41.849Z",
"version": 1596923021849,
"is_deleted": false,
"present_at_all_locations": false,
"present_at_location_ids": [
"1JZ5JTPBW0EXY"
],
"item_data": {
"name": "Hummus Plate",
"description": "Olives, feta, tomatoes, tzatziki and warm pita.",
"visibility": "PRIVATE",
"category_id": "ARB5LTTDUSER6T66ZWYO65FL",
"variations": [
{
"type": "ITEM_VARIATION",
"id": "HBIHPLROOJUUNUSW3BZUDQ5J",
"updated_at": "2020-08-08T21:40:14.581Z",
"version": 1596922814581,
"is_deleted": false,
"present_at_all_locations": false,
"present_at_location_ids": [
"1JZ5JTPBW0EXY"
],
"item_variation_data": {
"item_id": "7HXK6NVBDKSOK64CCAYZJ2KW",
"name": "Regular",
"ordinal": 0,
"pricing_type": "FIXED_PRICING",
"price_money": {
"amount": 1000,
"currency": "USD"
}
}
}
],
"product_type": "REGULAR",
"skip_modifier_screen": true,
"ecom_available": false,
"ecom_visibility": "UNINDEXED"
}
}
Here the body content.
{
"batches": [{
"objects": [{
"type": "ITEM_VARIATION",
"id": "T6KKCATDIU2VV4BDBMVGML4W",
"item_variation_data": {
"item_id": "FNWWGFTBQWFAYK6JGBD3LDLD",
"pricing_type": "FIXED_PRICING",
"price_money": {
"amount": 150,
"currency": "USD"
}
}
}]
}],
"idempotency_key": "61994762-3a6s-4b75-sf81-fdfaf"
}
Here is the error I'm getting:
**{
"errors": [
{
"category": "INVALID_REQUEST_ERROR",
"code": "VERSION_MISMATCH",
"detail": "Object version does not match latest database version.",
"field": "version"
}
]
}**
I'm not sure what I'm doing wrong, but I appreciate any help I can get. Thank you in advance!
When updating a catalog object in Square, you need to provide the latest version number in the request.
{
"batches": [{
"objects": [{
"type": "ITEM_VARIATION",
"id": "T6KKCATDIU2VV4BDBMVGML4W",
"version": "12345",
"item_variation_data": {
"item_id": "FNWWGFTBQWFAYK6JGBD3LDLD",
"pricing_type": "FIXED_PRICING",
"price_money": {
"amount": 150,
"currency": "USD"
}
}
}]
}],
"idempotency_key": "61994762-3a6s-4b75-sf81-fdfaf"
}
Have a Objects:
{
"id": 1766013,
"created_date": "2019-01-28 12:54:23",
"amount": 10000,
"type": "payin",
"status": "done",
"method": "m_visamc"
},
{
"id": 1766012,
"created_date": "2019-03-28 12:54:08",
"amount": 300000,
"type": "payin",
"status": "error",
"method": "m_visamc"
},
{
"id": 1766011,
"created_date": "2019-02-28 12:53:31",
"amount": 6000,
"type": "payin",
"status": "done",
"method": "m_visamc"
}
How I can group items by month, by value in "created_date"?
Need get items just one month
Thank you
So if I understand you correctly you want to group your items by the month value which is stored in the property created_date. The following code will do that for you.
var items = [
{
"id": 1766013,
"created_date": "2019-01-28 12:54:23",
"amount": 10000,
"type": "payin",
"status": "done",
"method": "m_visamc"
},
{
"id": 1766012,
"created_date": "2019-03-28 12:54:08",
"amount": 300000,
"type": "payin",
"status": "error",
"method": "m_visamc"
},
{
"id": 1766011,
"created_date": "2019-02-28 12:53:31",
"amount": 6000,
"type": "payin",
"status": "done",
"method": "m_visamc"
},
{
"id": 1766012,
"created_date": "2019-02-26 12:53:36",
"amount": 6000,
"type": "payin",
"status": "done",
"method": "m_visamc"
}];
var groupByCreatedDateMonth = array =>
array.reduce((objectsByKeyValue, obj) => {
var dateString = obj.created_date;
var date = new Date(dateString);
var value = date.getMonth();
objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj);
return objectsByKeyValue;
}, {});
var result = groupByCreatedDateMonth(items);
console.log(result);
I am developing a VB.NET application to interact with our new Shopify store. I am in the process of automating product additions/updates/removal based on data in our Oracle database.
Basically the application creates a new Shopify part for a pattern, then creates product variants for each sku in that pattern. It does this by POSTing and PUTing requests using the HttpWebRequest class. This was working great through yesterday afternoon, now today for some reason it's failing on the product variant creation/update with a 404 - Not Found error code.
The product add request JSON is below. This works perfectly, it creates the part on Shopify which I can then see through the admin panel. This is POSTed to the URL "https://key:password#bath-and-window-direct.myshopify.com/admin/products.json" (where key and password are replaced with our credentials)
{
"product": {
"id": 0,
"body_html": "A classic combination of embroidery and cut work form the flowing border, creating your own seaside retreat. ",
"title": "Seabreeze Sand",
"vendor": "SKL",
"product_type": "",
"published_scope": "global",
"tags": "J71227",
"variants": null,
"options": null,
"images": [{
"id": 0,
"product_id": 0,
"position": 1,
"src": "http:\/\/i320.photobucket.com\/albums\/nn353\/fkhphoto\/J71227main.jpg",
"variant_ids": null
}],
"image": null
}
}
This is the JSON reply I'm receiving after this call, and I can also see the part added in the Shopify admin panel:
{
"product": {
"id": 7874734983,
"title": "Seabreeze Sand",
"body_html": "A classic combination of embroidery and cut work form the flowing border, creating your own seaside retreat. ",
"vendor": "SKL",
"product_type": "",
"created_at": "2016-07-14T10:43:56-04:00",
"handle": "seabreeze-sand",
"updated_at": "2016-07-14T10:43:56-04:00",
"published_at": "2016-07-14T10:43:56-04:00",
"template_suffix": null,
"published_scope": "global",
"tags": "J71227",
"variants": [{
"id": 24925005383,
"product_id": 7874734983,
"title": "Default Title",
"price": "0.00",
"sku": "",
"position": 1,
"grams": 0,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": null,
"option1": "Default Title",
"option2": null,
"option3": null,
"created_at": "2016-07-14T10:43:56-04:00",
"updated_at": "2016-07-14T10:43:56-04:00",
"taxable": true,
"barcode": null,
"image_id": null,
"inventory_quantity": 1,
"weight": 0.0,
"weight_unit": "lb",
"old_inventory_quantity": 1,
"requires_shipping": true
}],
"options": [{
"id": 9404426823,
"product_id": 7874734983,
"name": "Title",
"position": 1,
"values": ["Default Title"]
}],
"images": [{
"id": 16242879303,
"product_id": 7874734983,
"position": 1,
"created_at": "2016-07-14T10:43:56-04:00",
"updated_at": "2016-07-14T10:43:56-04:00",
"src": "https://cdn.shopify.com/s/files/1/1363/2407/products/J71227main.jpg?v=1468507436",
"variant_ids": []
}],
"image": {
"id": 16242879303,
"product_id": 7874734983,
"position": 1,
"created_at": "2016-07-14T10:43:56-04:00",
"updated_at": "2016-07-14T10:43:56-04:00",
"src": "https://cdn.shopify.com/s/files/1/1363/2407/products/J71227main.jpg?v=1468507436",
"variant_ids": []
}
}
}
Immediately after that, I'm POSTing the following JSON to add a product variant to this newly created part, to the URL "https://key:password#bath-and-window-direct.myshopify.com/admin/products/7874734983/variants.json". This is the call that returns a 404 - Not Found error through the HttpWebRequest class.
{
"variant": {
"id": 0,
"product_id": 7874734983,
"title": "Seabreeze Tier Curtain in Sand",
"price": "11.99",
"sku": "J7122700013V09",
"compare_at_price": "0.00",
"position": 0,
"grams": 0,
"option1": "57X13 ROD POCKET VAL",
"option2": null,
"option3": null,
"taxable": true,
"barcode": "036326422417",
"weight": 0,
"weight_unit": "lb",
"inventory_quantity": 550,
"old_inventory_quantity": 550,
"requires_shipping": true,
"image_id": null,
"metafields": [{
"id": 0,
"key": "point1",
"value": "57 x 13 Valance",
"value_type": "string",
"namespace": "J7122700013V09"
},
{
"id": 0,
"key": "point2",
"value": "To achieve the look in the photo, use one valance and one tier pair.",
"value_type": "string",
"namespace": "J7122700013V09"
},
{
"id": 0,
"key": "point3",
"value": "Feels like you are in your own beach cottage.",
"value_type": "string",
"namespace": "J7122700013V09"
},
{
"id": 0,
"key": "point4",
"value": "100% Polyester.",
"value_type": "string",
"namespace": "J7122700013V09"
},
{
"id": 0,
"key": "point5",
"value": " ",
"value_type": "string",
"namespace": "J7122700013V09"
}]
}
}
What am I doing wrong? The URLs, when copied into a web browser, work perfectly fine...I can see all the JSON of the current data. But when attempting to add the variant, I'm getting the 404.
Try leaving out the "id": 0 properties — both from the variant and from the metafields. Shopify will determine the IDs automatically.