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);
Related
I am needing to unnest a multidimensional array with multiple items.
My data looks like this (two rows):
Each order in this table has a fulfillments column which is a multidimensional array.
fulfilments has multiple line_items. Some orders only have 1 line_items, some orders have multiple line_items.
My goal is to get each product name and the quantity from each line_items and do a running sum/tally.
Here's a JSON export of the fulfillments column data:
"{
"fulfillments": [{
"value": {
"id": "6575674745",
"tracking_number": null,
"line_items": [{
"value": {
"properties": [{
"value": {
"name": "shipping_interval_unit_type",
"value": "Months"
}
}, {
"value": {
"name": "shipping_interval_frequency",
"value": "1"
}
}, {
"value": {
"name": "ch_item_id",
"value": "77321530"
}
}],
"product_id": "4790985097351",
"total_discount_set": {
"shop_money": {
"amount": "0.00",
"currency_code": "USD"
},
"presentment_money": {
"amount": "0.00",
"currency_code": "USD"
}
},
"sku": “10101”,
"name": “Product1”,
"variant_id": "33433213108359",
"quantity": "1",
"taxable": "true",
"total_discount": "0",
"title": "Product1 Title",
"price_set": {
"shop_money": {
"amount": "12.00",
"currency_code": "USD"
},
"presentment_money": {
"amount": "12.00",
"currency_code": "USD"
}
},
"product_exists": "true"
}
}],
"service": "manual",
"tracking_urls": []
}
}, {
"value": {
"tracking_url": null,
"id": "3555531128967",
"tracking_company": null,
"tracking_number": null,
"name": "#3129472402.2",
"tracking_numbers": [],
"line_items": [{
"value": {
"properties": [{
"value": {
"name": "shipping_interval_frequency",
"value": "1"
}
}, {
"value": {
"name": "shipping_interval_unit_type",
"value": "Months"
}
}, {
"value": {
"name": "ch_item_id",
"value": "77321529"
}
}],
"product_id": "5216409780359",
"total_discount_set": {
"shop_money": {
"amount": "0.00",
"currency_code": "USD"
},
"presentment_money": {
"amount": "0.00",
"currency_code": "USD"
}
},
"sku": "9005",
"name": “Product2”,
"quantity": "1",
"title": “Product2 Title”,
"price_set": {
"shop_money": {
"amount": "0.00",
"currency_code": "USD"
},
"presentment_money": {
"amount": "0.00",
"currency_code": "USD"
}
},
"product_exists": "true"
}
}],
"service": "manual",
"tracking_urls": []
}
}]
}"
SELECT
fulfillments[ safe_OFFSET (0)].value.line_items[ safe_OFFSET (0)].value.name AS name,
fulfillments[ safe_OFFSET (0)].value.line_items[ safe_OFFSET (0)].value.quantity AS quantity
FROM
`mydatabase`
isn't ideal in the event the order has more than one array in line_items
How can I unnest a multidimensional array when I don't know how large each array is?
SELECT
*
FROM
`mydatabase`,
UNNEST(fulfillments) as a
Only gives me another array to flatten
Given your scenario, you may try to use this query to unnest line_items with product name and the quantity:
SELECT
line_items.value.name,
line_items.value.quantity,
line_items.value.product_id
from `mydatabase`,
unnest(fulfillments) fulfillments,
unnest(fulfillments.value.line_items) line_items
Output:
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');
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"
}
I need to compare 2 JSON arrays using Mule 4 dataweave 2.0 to get matching and un-matching outputs.
The sample input JSON payload is given below:
[
{
"CODE": "A11",
"NAME": "Alpha",
"ID": "C10000"
},
{
"CODE": "B12",
"NAME": "Bravo",
"ID": "B20000"
},
{
"CODE": "C11",
"NAME": "Charlie",
"ID": "C30000"
},
{
"CODE": "D12",
"NAME": "Delta",
"ID": "D40000"
},
{
"CODE": "E12",
"NAME": "Echo",
"ID": "E50000"
}
]
This has to be compared to the below on ID/IDENTITY field.
[
{
"IDENTITY": "D40000",
"NM": "Delta"
},
{
"IDENTITY": "C30000",
"NM": "Charlie"
}
]
My expected output is 2 variable arrays containing matching and un-matching objects:
varMatch:
[
{
"CODE": "C11",
"NAME": "Charlie",
"ID": "C30000"
},
{
"CODE": "D12",
"NAME": "Delta",
"ID": "D40000"
}
]
varUnmatch:
[
{
"CODE": "A11",
"NAME": "Alpha",
"ID": "C10000"
},
{
"CODE": "B12",
"NAME": "Bravo",
"ID": "B20000"
},
{
"CODE": "E12",
"NAME": "Echo",
"ID": "E50000"
}
]
If that ID map is coming from somewhere else and you can't change its structure, I'd probably remap it and then use it like so:
%dw 2.0
output application/json
var identMap = [
{
"IDENTITY": "D40000",
"NM": "Delta"
},
{
"IDENTITY": "C30000",
"NM": "Charlie"
}
]
var remapped = identMap reduce ((item,accum={}) -> accum ++ (item.IDENTITY): 1)
---
payload groupBy (if (remapped[$.ID]?) "varMatched" else "varUnmatched")
which produces
{
"varUnmatched": [
{
"CODE": "A11",
"NAME": "Alpha",
"ID": "C10000"
},
{
"CODE": "B12",
"NAME": "Bravo",
"ID": "B20000"
},
{
"CODE": "E12",
"NAME": "Echo",
"ID": "E50000"
}
],
"varMatched": [
{
"CODE": "C11",
"NAME": "Charlie",
"ID": "C30000"
},
{
"CODE": "D12",
"NAME": "Delta",
"ID": "D40000"
}
]
}
Hope this helps
%dw 2.0
var input1=[{
"CODE": "A11",
"NAME": "Alpha",
"ID": "C10000"
},
{
"CODE": "B12",
"NAME": "Bravo",
"ID": "B20000"
},
{
"CODE": "C11",
"NAME": "Charlie",
"ID": "C30000"
},
{
"CODE": "D12",
"NAME": "Delta",
"ID": "D40000"
},
{
"CODE": "E12",
"NAME": "Echo",
"ID": "E50000"
}
]
var input2=[
{
"IDENTITY": "D40000",
"NM": "Delta"
},
{
"IDENTITY": "C30000",
"NM": "Charlie"
}
]
var varMatch = input1 map $ filter (input2.IDENTITY contains $.ID)
var varUnmatch = input1 -- varMatch
output application/json
---
{
varMatch: varMatch,
varUnmatch: varUnmatch
}
Sample Output
{
"varMatch": [
{
"CODE": "C11",
"NAME": "Charlie",
"ID": "C30000"
},
{
"CODE": "D12",
"NAME": "Delta",
"ID": "D40000"
}
],
"varUnmatch": [
{
"CODE": "A11",
"NAME": "Alpha",
"ID": "C10000"
},
{
"CODE": "B12",
"NAME": "Bravo",
"ID": "B20000"
},
{
"CODE": "E12",
"NAME": "Echo",
"ID": "E50000"
}
]
}
Here's another solution, albeit the previous two are solving your problem:
%dw 2.0
output application/dw
var data = [
{
"CODE": "A11",
"NAME": "Alpha",
"ID": "C10000"
},
{
"CODE": "B12",
"NAME": "Bravo",
"ID": "B20000"
},
{
"CODE": "C11",
"NAME": "Charlie",
"ID": "C30000"
},
{
"CODE": "D12",
"NAME": "Delta",
"ID": "D40000"
},
{
"CODE": "E12",
"NAME": "Echo",
"ID": "E50000"
}
]
var searchData = [
{
"IDENTITY": "D40000",
"NM": "Delta"
},
{
"IDENTITY": "C30000",
"NM": "Charlie"
}
]
---
data dw::core::Arrays::partition (e) -> searchData.*IDENTITY contains e.ID
Pick the one that perfoms the best and use it.
I need make payments with square, I used square_in_app_payments plugin but it only give me a nonce string that seems I have to send it to checkout api with items information inside JSON in http request, but I don't understand how to make it.
I only tried from postman make some samples fo square.
POST /v2/locations/{{location_id}}/checkouts HTTP/1.1
Host: connect.squareup.com
Authorization: Bearer {{access_token}}
Content-Type: application/json
cache-control: no-cache
Postman-Token: 5d2c05ae-11d6-42bd-bd1e-47fa44621a98
{
"idempotency_key": "60f919ba-2042-4402-bc66-e6455ec32349",
"order": {
"reference_id": "reference_id",
"line_items": [
{
"name": "Printed T Shirt",
"quantity": "2",
"base_price_money": {
"amount": 1500,
"currency": "USD"
},
"discounts": [
{
"name": "7% off previous season item",
"percentage": "7"
},
{
"name": "$3 off Customer Discount",
"amount_money": {
"amount": 300,
"currency": "USD"
}
}
]
},
{
"name": "Slim Jeans",
"quantity": "1",
"base_price_money": {
"amount": 2500,
"currency": "USD"
}
},
{
"name": "Woven Sweater",
"quantity": "3",
"base_price_money": {
"amount": 3500,
"currency": "USD"
},
"discounts": [
{
"name": "$11 off Customer Discount",
"amount_money": {
"amount": 1100,
"currency": "USD"
}
}
],
"taxes": [
{
"name": "Fair Trade Tax",
"percentage": "5"
}
]
}
],
"discounts": [
{
"name": "Father's day 12% OFF",
"percentage": "12"
},
{
"name": "Global Sales $55 OFF",
"amount_money": {
"amount": 5500,
"currency": "USD"
}
}
],
"taxes": [
{
"name": "Sales Tax",
"percentage": "8.5"
}
]
},
"ask_for_shipping_address": true,
"merchant_support_email": "merchant+support#website.com",
"pre_populate_buyer_email": "example#email.com",
"pre_populate_shipping_address": {
"address_line_1": "1455 Market St.",
"address_line_2": "Suite 600",
"locality": "San Francisco",
"administrative_district_level_1": "CA",
"postal_code": "94103",
"country": "US",
"first_name": "Jane",
"last_name": "Doe"
}
}
I need to know how to take the nonce string from card form of square_in_app_payments plugin and send it in http request for complete the payment.