Help with filtering and flattening some JSON

Hi there

I’ve tried and cannot figure out how to solve this problem

I have some JSON which comes from a Woocommerce hook with some Stripe meta data
Example below:

{
  "id": 111518,
  "parent_id": 0,
  "status": "on-hold",
  "currency": "IDR",
  "version": "6.9.0",
  "prices_include_tax": false,
  "date_created": "2022-09-15T08:40:17",
  "date_modified": "2022-09-15T08:40:23",
  "discount_total": "0",
  "discount_tax": "0",
  "shipping_total": "0",
  "shipping_tax": "0",
  "cart_tax": "0",
  "total": "5291000",
  "total_tax": "0",
  "customer_id": 1,
  "order_key": "wc_order_REbmpmNI2Vyu1",
  "billing": {
    "first_name": "We ",
    "last_name": "Covd",
    "company": "",
    "address_1": "1111",
    "address_2": "",
    "city": "bali",
    "state": "BA",
    "postcode": "22222",
    "country": "ID",
    "email": "[email protected]",
    "phone": "+628216264249"
  },
  "shipping": {
    "first_name": "",
    "last_name": "",
    "company": "",
    "address_1": "",
    "address_2": "",
    "city": "",
    "state": "",
    "postcode": "",
    "country": "",
    "phone": ""
  },
  "payment_method": "stripe",
  "payment_method_title": "Credit Card (Stripe)",
  "transaction_id": "ch_3Li67sCjMA057yhb1I32XhkG",
  "customer_ip_address": "53.220.149.91",
  "customer_user_agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
  "created_via": "checkout",
  "customer_note": "",
  "date_completed": null,
  "date_paid": null,
  "cart_hash": "74d73bbab72b03386ba23e7b70fab8dc",
  "number": "111518",
  "meta_data": [
    {
      "id": 697073,
      "key": "affwp_affiliate_submission_forms",
      "value": []
    },
    {
      "id": 697102,
      "key": "is_vat_exempt",
      "value": "no"
    },
    {
      "id": 697103,
      "key": "wpml_language",
      "value": "en"
    },
    {
      "id": 697104,
      "key": "_wc_deposits_version",
      "value": "1.6.0"
    },
    {
      "id": 697105,
      "key": "_stripe_customer_id",
      "value": "cus_MQy3ZiAeNQZsqg"
    },
    {
      "id": 697106,
      "key": "_stripe_source_id",
      "value": "src_1Li67lCjMA057yhbeqJjGUDQ"
    },
    {
      "id": 697107,
      "key": "_stripe_intent_id",
      "value": "pi_3Li67sCjMA057yhb1IC7uQhx"
    },
    {
      "id": 697108,
      "key": "_stripe_charge_captured",
      "value": "no"
    },
    {
      "id": 697117,
      "key": "_new_order_email_sent",
      "value": "true"
    },
    {
      "id": 697122,
      "key": "_ga_tracked",
      "value": "1"
    }
  ],
  "line_items": [
    {
      "id": 11,
      "name": "MONTHLY RENTAL ",
      "product_id": 111050,
      "variation_id": 0,
      "quantity": 1,
      "tax_class": "",
      "subtotal": "5291000",
      "subtotal_tax": "0",
      "total": "5291000",
      "total_tax": "0",
      "taxes": [],
      "meta_data": [
        {
          "id": 118,
          "key": "_is_deposit",
          "value": "yes",
          "display_key": "_is_deposit",
          "display_value": "yes"
        },
        {
          "id": 119,
          "key": "_deposit_full_amount",
          "value": "40700000.1",
          "display_key": "Full Amount",
          "display_value": "40700000"
        },
        {
          "id": 120,
          "key": "_deposit_full_amount_ex_tax",
          "value": "40700000.1",
          "display_key": "Full Amount (excl. tax)",
          "display_value": "40700000"
        },
        {
          "id": 121,
          "key": "_deposit_deposit_amount_ex_tax",
          "value": "5291000.013",
          "display_key": "Deposit Amount (excl. tax)",
          "display_value": "5291000"
        }
      ],
      "sku": "14890-monthly-rental",
      "price": 5291000.013,
      "image": {
        "id": "110011",
        "src": "logo-stacked.png"
      },
      "parent_name": null
    }
  ],
  "tax_lines": [],
  "shipping_lines": [],
  "fee_lines": [],
  "coupon_lines": [],
  "refunds": [],
  "payment_url": "https://example.com/checkout/order-pay/111518/?pay_for_order=true&key=wc_order_REbmpm8I2Vyu1",
  "is_editable": true,
  "needs_payment": false,
  "needs_processing": true,
  "date_created_gmt": "2022-09-15T00:40:17",
  "date_modified_gmt": "2022-09-15T00:40:23",
  "date_completed_gmt": null,
  "date_paid_gmt": null,
  "currency_symbol": "Rp",

}

Basically I need the meta_data in a more useable format, the id key is useless so would like toremove that

  "meta_data": [
     {
      "id": 697108,
      "key": "_stripe_charge_captured",
      "value": "no"
    },
    {
      "id": 697102,
      "key": "is_vat_exempt",
      "value": "no"
    },

to soemthing that looks like this:


{
"_stripe_charge_captured" : "no",
"is_vat_exempt" : "no"
}

Ideally It would also work for the line_items.meta_data as well keeping “display_key” and display_value":


    {
          "id": 119,
          "key": "_deposit_full_amount",
          "value": "40700000.1",
          "display_key": "Full Amount",
          "display_value": "40700000"
        }

to
{"Full Amount" : "40700000"}

Any help would be greatly appreciated

Hi @wgicio

2 Likes

Thanks for your help, I couldnt get it working as I orginally though in a normal function node however figured out I could use that code to acess the objet by key / display key.
I ended up with this

let metaData = {};
let meta1 = $node["createRecord"].json["data"]["apiData"]["ogdata"]["webhookData"]["body"]["meta_data"];
for(var arrayItem of meta1){
  meta1[arrayItem.key] = arrayItem.value;
}

let meta2 = $node["createRecord"].json["data"]["apiData"]["ogdata"]["webhookData"]["body"]["line_items"][0]["meta_data"];
for(var arrayItem2 of meta2){
  meta2[arrayItem2.key] = arrayItem2.display_value;
}

metaData.wpmlLanguage =  (meta1['wpml_language'] !='') ? meta1['wpml_language'] : 'en';
metaData.affwp = meta1['affwp_affiliate_submission_forms'];
metaData.stripeIntentID =  meta1['_stripe_customer_id'] ?? '';
metaData.stripeChargeCaptured =  meta1['_stripe_charge_captured'] ?? '';
metaData.stripeCurrency = meta1['_stripe_currency'] ?? '';

metaData.isDeposit =  meta2['_is_deposit'] ;
metaData.despositFullAmount =  meta2['_deposit_full_amount'] ?? '';
metaData.despositFullAmountExTax =  meta2['_deposit_full_amount_ex_tax'] ?? '';
metaData.despositAmountExTax =  meta2['_deposit_deposit_amount_ex_tax'] ?? '';

items[0].json.metaData = metaData;

Not the best solution but it works!
thanks for your assistance

Glad you got it figured out.
Not sure why you couldn’t get my example to work though.

You can copy the workflow and paste it into your workflow to use the nodes. Of course, you might need to rename something, but that should be that big of a deal.

I think it was because it was a “function item” and I wanted to add the code into an existing “function” node - not sure really. Also it was easier for me to have it in its own JSON node and not flatten down into the existing json structure

1 Like