Mapping data from nested JSON files - json

I'm trying to use a map function to pass all of the country names for a given continent into React components. I think I might need to convert this to an array, but I don't know how I would access the name key for each object given the objects all have different names.
"AD": {
"name": "Andorra",
"native": "Andorra",
"phone": "376",
"continent": "EU",
"capital": "Andorra la Vella",
"currency": "EUR",
"languages": [
"ca"
]
},
"AE": {
"name": "United Arab Emirates",
"native": "دولة الإمارات العربية المتحدة",
"phone": "971",
"continent": "AS",
"capital": "Abu Dhabi",
"currency": "AED",
"languages": [
"ar"
]
},
"AF": {
"name": "Afghanistan",
"native": "افغانستان",
"phone": "93",
"continent": "AS",
"capital": "Kabul",
"currency": "AFN",
"languages": [
"ps",
"uz",
"tk"
]
},
"AG": {
"name": "Antigua and Barbuda",
"native": "Antigua and Barbuda",
"phone": "1268",
"continent": "NA",
"capital": "Saint John's",
"currency": "XCD",
"languages": [
"en"
]
},
"AI": {
"name": "Anguilla",
"native": "Anguilla",
"phone": "1264",
"continent": "NA",
"capital": "The Valley",
"currency": "XCD",
"languages": [
"en"
]
}

You can try the following
var countryNames = [];
for(let key in YourJsonData){
countryNames.push(YourJsonData[key][name]);
}
then loop over that array and render the elements in your component

Related

Custom JSON format webhook WooCommerce Wordpress

I want my Woocommerce store connected to a shipping parcel company. There is no existing WooCommerce plugin for it. I want to do it with webhooks, I had already successfully made a POST request to a URL after the order.created action. The JSON output contains all the shipping information. But I want to change the format of the JSON output.
The current JSON output is something like this.
{
"id": 605,
"parent_id": 0,
"status": "pending",
"order_key": "wc_order_5893614a8fb78",
"number": 605,
"currency": "USD",
"version": "2.6.13",
"prices_include_tax": false,
"date_created": "2017-02-02T16:41:46",
"date_modified": "2017-02-02T16:41:46",
"customer_id": 2,
"discount_total": "0.00",
"discount_tax": "0.00",
"shipping_total": "13.60",
"shipping_tax": "1.22",
"cart_tax": "1.44",
"total": "32.24",
"total_tax": "2.66",
"billing": {
"first_name": "Damandeep",
"last_name": "Singh",
"company": "",
"address_1": "1257 ",
"address_2": "Coach House Court",
"city": "Fullerton",
"state": "CA",
"postcode": "92831",
"country": "US",
"email": "daman#singhdd.com",
"phone": "(122) 274-5555"
},
"shipping": {
"first_name": "Damandeep",
"last_name": "Singh",
"company": "",
"address_1": "1257",
"address_2": "Coach House Court",
"city": "Fullerton",
"state": "CA",
"postcode": "92831",
"country": "US"
},
"payment_method": "paypal_express",
"payment_method_title": "Paypal Express",
"transaction_id": "",
"customer_ip_address": "103.41.36.35",
"customer_user_agent": "PostmanRuntime/3.0.9",
"created_via": "rest-api",
"customer_note": "",
"date_completed": "2017-02-02T08:41:46",
"date_paid": "",
"cart_hash": "",
"line_items": [
{
"id": 79,
"name": "Kaju Katli",
"sku": "SW-282",
"product_id": 491,
"variation_id": 494,
"quantity": 1,
"tax_class": "",
"price": "15.98",
"subtotal": "15.98",
"subtotal_tax": "1.44",
"total": "15.98",
"total_tax": "1.44",
"taxes": [
{
"id": 1,
"total": 1.4382,
"subtotal": 1.4382
}
],
"meta": [
{
"key": "packing-size",
"label": "Packing Size",
"value": "2lb Box"
}
]
}
],
"tax_lines": [
{
"id": 81,
"rate_code": "SALES TAX-1",
"rate_id": "1",
"label": "Sales Tax",
"compound": false,
"tax_total": "1.44",
"shipping_tax_total": "1.22"
}
],
"shipping_lines": [
{
"id": 80,
"method_title": "USPS Medium Flat Rate Box",
"method_id": "usps_medium_box",
"total": "13.60",
"total_tax": "0.00",
"taxes": []
}
],
"fee_lines": [],
"coupon_lines": [],
"refunds": [],
"_links": {
"self": [
{
"href": "https://ambala.webdemos.cf/wp-json/wc/v1/orders/605"
}
],
"collection": [
{
"href": "https://ambala.webdemos.cf/wp-json/wc/v1/orders"
}
],
"customer": [
{
"href": "https://ambala.webdemos.cf/wp-json/wc/v1/customers/2"
}
]
}
}
The JSON output should be something like this.
{
"name": "Gijs Boersma",
"street": "Lange laan",
"house_number": "29",
"house_number_extension": "a",
"zipcode": "9281EM",
"city": "Zevenaar",
"telephone": "0602938172",
"email": "noreply#example.com",
"reference": "Bestelling 112",
"pick_up_point": {
"uuid": "560db083-d941-425b-b3b6-b813718297e1"
},
"product": "sameday_parcel_medium",
"product_options": [
{
"option": "allow_neighbours",
"value": false
},
{
"option": "require_signature",
"value": false
},
{
"option": "age_check_18",
"value": false
},
{
"option": "perishable",
"value": true,
"max_attempts": 2
}
]
}
you can do this by the orders filter.
add_filter( 'woocommerce_rest_prepare_shop_order_object', 'change_shop_order_response', 10, 3 );
function change_shop_order_response( $response, $item, $request ) {
//Do your stuff here with the $response object.
return $response
}

Couchbase N1ql query for updating new objet to JSON array

I have the following sample JSON
"address":{
"details":
[
{
"id" :<system generated UUID>,
"city" :{"type" : "string"},
"country" :{"type" : "string"},
"name" :{"type" : "string"},
"url" :{"type" : "string"},
"contacts" :
[{
"email":{"type" : "string"},
"phone":{"type" : "number"}
}]
}
]
}
I want the data to be in the following ways
Example 1:
"address":
{
"details": [
{
"id": 1234,
"city": "Medway",
"country": "United Kingdom",
"name": "Medway Youth Hostel",
"url": "http://www.yha.org.uk"
}]
}
Example 2:
"address":
{
"details": [
{
"id": 1234,
"city": "Medway",
"country": "United Kingdom",
"name": "Medway Youth Hostel",
"url": "http://www.yha.org.uk",
"contact": [
{
"email": "hari34#gmail.com",
"phone": 6789435634
}
]
}]
}
Example 3 :
"address":
{
"details": [
{
"id": 1234,
"city": "Medway",
"country": "United Kingdom",
"name": "Medway Youth Hostel",
"url": "http://www.yha.org.uk",
"contact": [
{
"email": "hari34#gmail.com",
"phone": 6789435634
},
{
"email": "sri123#gmail.com",
"phone": 9876655555
}
]
}]
}
Example 4:
"address":
{
"details": [
{
"id": 1234,
"city": "Medway",
"country": "United Kingdom",
"name": "Medway Youth Hostel",
"url": "http://www.yha.org.uk",
"contact": [
{
"email": "hari34#gmail.com",
"phone": 6789435634
}
]
},
{
"id": 3456,
"city": "Texas",
"country": "USA",
"name": "Texas Youth Hostel",
"url": "http://www.yha.org.uk",
"contact": [
{
"email": "ram123#gmail.com",
"phone": 876543219
}
]
}
]
}
I am very new to n1ql query, would anyone please help me to find the solution for inserting/Update an array to the object with in object. Example 1 is inserting an data to JSON, Example 2 is updating the data to Example 1 and follows...
1. Please help to get n1ql queries that satisfies above examples.
2. How to generate an UUID to id field in the document
1) Appends array never checks if element already exist in the array
INSERT INTO default VALUES ("k01", { "details": [ { "id": 1234, "city": "Medway", "country": "United Kingdom", "name": "Medway Youth Hostel", "url": "http://www.yha.org.uk" }] });
UPDATE default AS d SET e.contact = [ { "email": "hari34#gmail.com", "phone": 6789435634 } ]
FOR e IN d.details WHEN e.id = 1234 END
WHERE META().id = "k01";
UPDATE default AS d SET e.contact = ARRAY_APPEND(e.contact,{ "email": "sri123#gmail.com", "phone": 9876655555 })
FOR e IN d.details WHEN e.id = 1234 END
WHERE META().id = "k01";
UPDATE default AS d SET d.details = ARRAY_APPEND(d.details,{ "id": 3456, "city": "Texas", "country": "USA", "name": "Texas Youth Hostel", "url": "http://www.yha.org.uk", "contact": [ { "email": "ram123#gmail.com", "phone": 876543219 } ] })
WHERE META().id = "k01";
SELECT * from default USE KEYS["k01"];
2) USE UUID() function like below
INSERT INTO default VALUES ("k01", { "details": [ { "id": UUID(), "city": "Medway", "country": "United Kingdom", "name": "Medway Youth Hostel", "url": "http://www.yha.org.uk" }] });

How to verify that i get only one key from JSON response?

I get a response in JSON and need to make sure that i get only one 'signal' in signal_events. I'm using Ruby with rspec.
"signal_events": [
{
"id": "587e9ae969702d10bd5a0000",
"created_at": 1484692201,
"geo": {
"type": "Point",
"coordinates": [
-153.45703125,
59.67773438
]
},
"expires_at": 1484778601,
"geohashes": [
"bddg",
"bdeh"
],
"signal": {
"id": "587e9ae969702d0911060000",
"created_at": 1484692201.24,
"expires_at": 1484778601.24,
"signal_at": 1484691607,
"source": "usgs",
"updated_at": 1484692144,
"magnitude": 2,
"radius": 6.36107901750268,
"event_name": "earthquake",
"tsunami": "no"
},
"signal_type": "earthquake",
"centroid": {
"type": "Point",
"coordinates": [
-153.45703125,
59.67773438
]
},
"location": {
"country": "United States",
"country_code": "US",
"city": "Kenai Peninsula Borough",
"region": "Kenai Peninsula Borough",
"region_code": "AK"
}
},
Hash can not contain duplicated keys, so you will always have one value for any given key.

papal plus malformed_request JSON with shipping address

To show the Paypal plus iFrame (REST API) i make a Request with JSON
{
"intent": "sale",
"experience_profile_id": "XP-XXXX-XXXX-XXX-XXX",
"redirect_urls": {
"return_url": "https://www.XXXXXXX.de/bestellen.php",
"cancel_url": "https://www.XXXXXXX.de/zahlungabbruch.php"
},
"payer": {
"payment_method": "paypal"
},
"transactions": [{
"amount": {
"total": "53.45",
"currency": "EUR",
"details": {
"subtotal": "49.5",
"shipping": "3.95"
}
},
"description": "Tollewolle",
"invoice_number": "",
"item_list": {
"items": [
{
"quantity": "4",
"name": "Fine Kid - 50",
"price": "8.25",
"currency": "EUR",
"sku": "8-50"
},
{
"quantity": "2",
"name": "Fine Kid - 208",
"price": "8.25",
"currency": "EUR",
"sku": "8-208"
}
]
},
"shipping_address": {
"line1": "Rechnungs Str. 41",
"city": "Flensburg",
"postal_code": "24939",
"country_code": "DE"
}
}]
}
Without the shipping_address it works fine.
With the address i get an Error 'MALFORMED_REQUEST'
I believe Shipping address is a child ob lineitems so you need to move it up a level. E.g.
"transactions": [{"amount": {"total": "53.45", "currency":
"EUR","details":{"subtotal": "49.5", "shipping":
"3.95"}},"description": "Tollewolle", "invoice_number": "",
"item_list": {"items": [{"quantity": "4", "name": "Fine Kid - 50",
"price": "8.25", "currency": "EUR", "sku": "8-50"},{"quantity": "2",
"name": "Fine Kid - 208","price": "8.25", "currency": "EUR", "sku":
"8-208"}],"shipping_address": {"line1": "Rechnungs Str. 41","city":
"Flensburg", "postal_code": "24939", "country_code": "DE"}}}]

Fetching per segment price from Google QPX Express API

Using this url: https://qpx-express-demo.itasoftware.com/ I am making a request to get flight availability from BOM to NYC on a future date.
The JSON response that i receive :
Formatted Response:
Solution# 1 Sale Price: INR31803
Slice 0
KU 302 BOM 2016-01-29T05:50+05:30 KWI 2016-01-29T07:50+03:00
KU 117 KWI 2016-01-29T09:55+03:00 JFK 2016-01-29T16:20-05:00
Unformatted Response:
{
"kind": "qpxexpress#tripOption",
"saleTotal": "INR31803",
"id": "5WAPnfYj8mARdie1jBKphP002",
"slice": [
{
"kind": "qpxexpress#sliceInfo",
"duration": 1260,
"segment": [
{
"kind": "qpxexpress#segmentInfo",
"duration": 270,
"flight": {
"carrier": "KU",
"number": "302"
},
"id": "G9ieitLhcbGEpU6u",
"cabin": "COACH",
"bookingCode": "H",
"bookingCodeCount": 9,
"marriedSegmentGroup": "0",
"leg": [
{
"kind": "qpxexpress#legInfo",
"id": "LRlNO3Gw0UX8y69J",
"aircraft": "777",
"arrivalTime": "2016-01-29T07:50+03:00",
"departureTime": "2016-01-29T05:50+05:30",
"origin": "BOM",
"destination": "KWI",
"originTerminal": "2",
"destinationTerminal": "M",
"duration": 270,
"mileage": 1713,
"meal": "Breakfast",
"secure": true
}
],
"connectionDuration": 125
},
{
"kind": "qpxexpress#segmentInfo",
"duration": 865,
"flight": {
"carrier": "KU",
"number": "117"
},
"id": "GBan4WxY2pcKVHbu",
"cabin": "COACH",
"bookingCode": "H",
"bookingCodeCount": 9,
"marriedSegmentGroup": "1",
"leg": [
{
"kind": "qpxexpress#legInfo",
"id": "L9pmI3XsVZSBhZah",
"aircraft": "777",
"arrivalTime": "2016-01-29T16:20-05:00",
"departureTime": "2016-01-29T09:55+03:00",
"origin": "KWI",
"destination": "JFK",
"originTerminal": "M",
"destinationTerminal": "4",
"duration": 865,
"mileage": 6336,
"meal": "Meal",
"secure": true
}
]
}
]
}
],
"pricing": [
{
"kind": "qpxexpress#pricingInfo",
"fare": [
{
"kind": "qpxexpress#fareInfo",
"id": "A80Fm/pw8E5LRH33c8hjZw/s+BTPU4IxJWOlGXyM1SLM",
"carrier": "KU",
"origin": "BOM",
"destination": "NYC",
"basisCode": "HLOWIN1"
}
],
"segmentPricing": [
{
"kind": "qpxexpress#segmentPricing",
"fareId": "A80Fm/pw8E5LRH33c8hjZw/s+BTPU4IxJWOlGXyM1SLM",
"segmentId": "G9ieitLhcbGEpU6u"
},
{
"kind": "qpxexpress#segmentPricing",
"fareId": "A80Fm/pw8E5LRH33c8hjZw/s+BTPU4IxJWOlGXyM1SLM",
"segmentId": "GBan4WxY2pcKVHbu"
}
],
"baseFareTotal": "INR26460",
"saleFareTotal": "INR26460",
"saleTaxTotal": "INR5343",
"saleTotal": "INR31803",
"passengers": {
"kind": "qpxexpress#passengerCounts",
"adultCount": 1
},
"tax": [
{
"kind": "qpxexpress#taxInfo",
"id": "JN_004",
"chargeType": "GOVERNMENT",
"code": "JN",
"country": "IN",
"salePrice": "INR1482"
},
{
"kind": "qpxexpress#taxInfo",
"id": "F2_001",
"chargeType": "GOVERNMENT",
"code": "F2",
"country": "IN",
"salePrice": "INR53"
},
{
"kind": "qpxexpress#taxInfo",
"id": "IN",
"chargeType": "GOVERNMENT",
"code": "IN",
"country": "IN",
"salePrice": "INR627"
},
{
"kind": "qpxexpress#taxInfo",
"id": "YM_001",
"chargeType": "GOVERNMENT",
"code": "YM",
"country": "IN",
"salePrice": "INR687"
},
{
"kind": "qpxexpress#taxInfo",
"id": "WO_001",
"chargeType": "GOVERNMENT",
"code": "WO",
"country": "IN",
"salePrice": "INR149"
},
{
"kind": "qpxexpress#taxInfo",
"id": "US_002",
"chargeType": "GOVERNMENT",
"code": "US",
"country": "US",
"salePrice": "INR1190"
},
{
"kind": "qpxexpress#taxInfo",
"id": "YX_001",
"chargeType": "GOVERNMENT",
"code": "YX",
"country": "KW",
"salePrice": "INR54"
},
{
"kind": "qpxexpress#taxInfo",
"id": "YC",
"chargeType": "GOVERNMENT",
"code": "YC",
"country": "US",
"salePrice": "INR368"
},
{
"kind": "qpxexpress#taxInfo",
"id": "XY",
"chargeType": "GOVERNMENT",
"code": "XY",
"country": "US",
"salePrice": "INR468"
},
{
"kind": "qpxexpress#taxInfo",
"id": "XA",
"chargeType": "GOVERNMENT",
"code": "XA",
"country": "US",
"salePrice": "INR265"
}
],
"fareCalculation": "BOM KU X/KWI KU NYC 397.64HLOWIN1 NUC 397.64 END ROE 66.541160 FARE INR 26460 XT 53F2 627IN 1482JN 149WO 687YM 54YX 368YC 468XY 265XA 1190US",
"latestTicketingTime": "2016-01-28T19:19-05:00",
"ptc": "ADT",
"refundable": true
}
From the response it is inferred that there are two segments, one with KU 302 and other with KU 117. How can i get per segment (per flight) info from the response?
I'm using the APIs in C#, if you install the Json.NET NuGet package, you can then get the response and deserialize it with:
JsonConvert.DeserializeObject<Response>(jsonString);
To build the Response object you can use an online Json to C# class builder (Google it and you'll find it immediately), or you can use dynamic objects.
Once you have the deserialized responde, you can just use LINQ to filter the informations:
from r in response where <filters> select r.segment // Or specific data.
All this assuming you want to use C#.