Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return a nested JSon object in a key value store object

I have a [simplified] JSON file like

{
   "id":87,
   "meta_data":[
      {
         "id":1232,
         "key":"user_created_by",
         "value":"example@gmail.com"
      },
      {
         "id":1233,
         "key":"user_created_at",
         "value":"1659890457477"
      },
      {
         "id":1234,
         "key":"good_person_or_silly_sausage",
         "value":"No"
      }      
   ]
}

I want to do something like

Logger.log(orderDetailsObject.meta_data.value[key="good_person_or_silly_sausage"])

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The expected value would be

No

However, this isn’t what’s occurring. At present I’m being told that the object is null.

I think it’s because the meta_data object is an object in an object or two dimensional array or something? I don’t have a good understand of what I’m doing here,

I feel like
https://docs.jsonata.org/simple
and
https://docs.jsonata.org/predicate
Should be guiding me towards the answer – but I’m not getting something I think.

I’m a newbie to SO and JSON & Javascript – so please be gentle. <3

EDIT: None-simplified actual json file below:

{
   "id":87,
   "parent_id":0,
   "status":"pending",
   "currency":"USD",
   "version":"6.5.1",
   "prices_include_tax":false,
   "date_created":"2022-08-07T16:40:57",
   "date_modified":"2022-08-11T10:13:07",
   "discount_total":"0.00",
   "discount_tax":"0.00",
   "shipping_total":"0.00",
   "shipping_tax":"0.00",
   "cart_tax":"0.00",
   "total":"1100.00",
   "total_tax":"0.00",
   "customer_id":17,
   "order_key":"wc_order_omQo7nW5yCS9X",
   "billing":{
      "first_name":"",
      "last_name":"",
      "company":"",
      "address_1":"",
      "address_2":"",
      "city":"",
      "state":"",
      "postcode":"",
      "country":"",
      "email":"freddie@example.com",
      "phone":""
   },
   "shipping":{
      "first_name":"",
      "last_name":"",
      "company":"",
      "address_1":"",
      "address_2":"",
      "city":"",
      "state":"",
      "postcode":"",
      "country":"",
      "phone":""
   },
   "payment_method":"",
   "payment_method_title":"",
   "transaction_id":"",
   "customer_ip_address":"",
   "customer_user_agent":"",
   "created_via":"rest-api",
   "customer_note":"",
   "date_completed":null,
   "date_paid":"2022-08-07T16:40:58",
   "cart_hash":"",
   "number":"87",
   "meta_data":[
      {
         "id":1232,
         "key":"user_created_by",
         "value":"example@gmail.com"
      },
      {
         "id":1233,
         "key":"user_created_at",
         "value":"1659890457477"
      },
      {
         "id":1234,
         "key":"good_person_or_silly_sausage",
         "value":"No"
      },
      {
         "id":1235,
         "key":"order_request_high_chair",
         "value":"yes"
      },
      {
         "id":1240,
         "key":"order_notes",
         "value":"They're on their honeymoon"
      },
      {
         "id":1241,
         "key":"pricing_notes",
         "value":"They're on their honeymoon"
      },
      {
         "id":1242,
         "key":"order_booked_by",
         "value":"me on me"
      },
      {
         "id":1243,
         "key":"order_referral_source",
         "value":"Newspaper"
      },
      {
         "id":1244,
         "key":"commission_percent",
         "value":"13"
      },
      {
         "id":1245,
         "key":"discount_percent",
         "value":"5"
      },
      {
         "id":1246,
         "key":"deposit_paid_date",
         "value":"today"
      },
      {
         "id":1247,
         "key":"damage_bond_date_paid",
         "value":"today"
      },
      {
         "id":1248,
         "key":"damage_bond",
         "value":"100"
      },
      {
         "id":1249,
         "key":"start_date",
         "value":"01\/01\/01"
      },
      {
         "id":1250,
         "key":"deposit_payment_amount",
         "value":"500"
      },
      {
         "id":1251,
         "key":"order_request_welcome_pack_wine_type",
         "value":"Red"
      },
      {
         "id":1252,
         "key":"end_date",
         "value":"02\/02\/02"
      },
      {
         "id":1259,
         "key":"_new_order_email_sent",
         "value":"true"
      },
      {
         "id":1260,
         "key":"_automatewoo_order_created",
         "value":"1"
      }
   ],
   "line_items":[
      {
         "id":45,
         "name":"Villa Booking",
         "product_id":0,
         "variation_id":0,
         "quantity":1,
         "tax_class":"",
         "subtotal":"1000.00",
         "subtotal_tax":"0.00",
         "total":"1000.00",
         "total_tax":"0.00",
         "taxes":[
            
         ],
         "meta_data":[
            {
               "id":411,
               "key":"Dates",
               "value":"01\/01\/01 until 02\/02\/02",
               "display_key":"Dates",
               "display_value":"01\/01\/01 until 02\/02\/02"
            }
         ],
         "sku":null,
         "price":1000,
         "parent_name":null
      },
      {
         "id":46,
         "name":"Standard Welcome Pack",
         "product_id":73,
         "variation_id":0,
         "quantity":2,
         "tax_class":"",
         "subtotal":"0.00",
         "subtotal_tax":"0.00",
         "total":"0.00",
         "total_tax":"0.00",
         "taxes":[
            
         ],
         "meta_data":[
            {
               "id":421,
               "key":"Wine Type",
               "value":"Red",
               "display_key":"Wine Type",
               "display_value":"Red"
            }
         ],
         "sku":"",
         "price":0,
         "parent_name":null
      }
   ],
   "tax_lines":[
      
   ],
   "shipping_lines":[
      
   ],
   "fee_lines":[
      {
         "id":47,
         "name":"Damage Bond",
         "tax_class":"",
         "tax_status":"taxable",
         "amount":"100",
         "total":"100.00",
         "total_tax":"0.00",
         "taxes":[
            
         ],
         "meta_data":[
            {
               "id":428,
               "key":"damage_bond_paid_to",
               "value":"example@gmail.com",
               "display_key":"damage_bond_paid_to",
               "display_value":"example@gmail.com"
            },
            {
               "id":429,
               "key":"damage_bond_paid_on",
               "value":"today",
               "display_key":"damage_bond_paid_on",
               "display_value":"today"
            }
         ]
      },
      {
         "id":48,
         "name":"Booking Fee",
         "tax_class":"",
         "tax_status":"taxable",
         "amount":"",
         "total":"0.00",
         "total_tax":"0.00",
         "taxes":[
            
         ],
         "meta_data":[
            {
               "id":436,
               "key":"dbooing_fee_paid_to",
               "value":"example@gmail.com",
               "display_key":"dbooing_fee_paid_to",
               "display_value":"example@gmail.com"
            },
            {
               "id":437,
               "key":"booking_fee_paid_on",
               "value":"1659890457477",
               "display_key":"booking_fee_paid_on",
               "display_value":"1659890457477"
            }
         ]
      }
   ],
   "coupon_lines":[
      
   ],
   "refunds":[
      
   ],
   "payment_url":"https:\/\/dev1.example.com\/checkout\/order-pay\/87\/?pay_for_order=true&key=wc_order_omQo7nW5yCS9X",
   "date_created_gmt":"2022-08-07T16:40:57",
   "date_modified_gmt":"2022-08-11T10:13:07",
   "date_completed_gmt":null,
   "date_paid_gmt":"2022-08-07T16:40:58",
   "currency_symbol":"$",
   "_links":{
      "self":[
         {
            "href":"https:\/\/dev1.example.com\/wp-json\/wc\/v3\/orders\/87"
         }
      ],
      "collection":[
         {
            "href":"https:\/\/dev1.example.com\/wp-json\/wc\/v3\/orders"
         }
      ],
      "customer":[
         {
            "href":"https:\/\/dev1.example.com\/wp-json\/wc\/v3\/customers\/17"
         }
      ]
   }
}

>Solution :

You can use the find method on the array:

const orderDetailsObject = {"id":87,"meta_data":[{"id":1232,"key":"user_created_by","value":"example@gmail.com"},{"id":1233,"key":"user_created_at","value":"1659890457477"},{"id":1234,"key":"good_person_or_silly_sausage","value":"No"}]};

console.log(orderDetailsObject.meta_data.find(
    ({key}) => key == "good_person_or_silly_sausage"
)?.value);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading