reformat json in desired format - javascript

{
"EUR": {
"Description": "",
"Bid": "1.1222",
"Region": "",
"Bid1": "1.1283",
"CurrencyCode": "EUR",
"FeedSource": "1",
"Ask": "1.1226",
"ProviderName": "TEST 1",
"Low": "1.1195",
"LastModified": "2014-05-20T08:11:13",
"CreatedBy": "10000",
"OpenBid": "1.12",
"QuotedCurrencyCode": "USD"
},
"PHP": {
"Description": "",
"Bid": "46.75",
"Region": "",
"Bid1": "4.59",
"CurrencyCode": "PHP",
"FeedSource": "1",
"Ask": "46.755",
"ProviderName": "Test2",
"Low": "4.715",
"LastModified": "2016-05-20T07:54:32",
"CreatedBy": "10000",
"QuotedCurrencyCode": "USD"
}
}
Need to reformat this json in below format,
{ CurrencyCode: "EUR", LastModified: "10/02/2012", ProviderName: "Test1", Bid: "1.2", Bid1: "1.19", Bid2: "1.2", Ask: "2CloseBid: "32", CloseAsk: "35" },
{ CurrencyCode: "PHP", LastModified: "10/02/2012", ProviderName: "Test2", Other fields... }]
I want to do this conversion in javascript.
Tried with stringify and parse but it doesn't give desired output.
Please suggest

You can use for-in loop & iterate over each key
let input = {
"EUR": {
"Description": "",
"Bid": "1.1222",
"Region": "",
"Bid1": "1.1283",
"CurrencyCode": "EUR",
"FeedSource": "1",
"Ask": "1.1226",
"ProviderName": "TEST 1",
"Low": "1.1195",
"LastModified": "2014-05-20T08:11:13",
"CreatedBy": "10000",
"OpenBid": "1.12",
"QuotedCurrencyCode": "USD"
},
"PHP": {
"Description": "",
"Bid": "46.75",
"Region": "",
"Bid1": "4.59",
"CurrencyCode": "PHP",
"FeedSource": "1",
"Ask": "46.755",
"ProviderName": "Test2",
"Low": "4.715",
"LastModified": "2016-05-20T07:54:32",
"CreatedBy": "10000",
"QuotedCurrencyCode": "USD"
}
}
var output = [];
for(let key in input){
output.push(input[key])
}
console.log(output);

Related

How to update a field inside the array of object of another array of objects in mongo db? or How to update a field using the array index in mongo db?

This is the structure of my collection of mongodb database. I need to update the status inside each order products. What I need to do?
{
"_id": {
"$oid": "633ab3c11e6e97b6332f56a1"
},
"orders": [
{
"date": "6/10/2022",
"productDetails": {
"0": {
"name": "Top",
"price": "1235",
"quantity": 1,
"status": "placed"
},
"1": {
"name": "Shirt",
"price": "1235",
"quantity": 1,
"status": "placed"
}
},
"billingAddress": {
"address": "My Address",
"city": "City",
"state": "State",
"country": "Country",
"pincode": "123456",
"contact": "1234567890"
},
"paymentMode": "cod"
},
{
"date": "6/10/2022",
"productDetails": {
"0": {
"name": "Top",
"price": "1235",
"quantity": 3,
"status": "placed"
},
"1": {
"name": "Shirt",
"price": "1235",
"quantity": 1,
"status": "placed"
}
},
"billingAddress": {
"address": "My Address",
"city": "City",
"state": "State",
"country": "Country",
"pincode": "123456",
"contact": "1234567890"
},
"paymentMode": "cod"
},
{
"date": "6/10/2022",
"productDetails": {
"0": {
"name": "Shirt",
"price": "234",
"quantity": 1,
"status": "placed"
},
"1": {
"name": "Top",
"price": "123",
"quantity": 1,
"status": "placed"
}
},
"billingAddress": {
"address": "My Address",
"city": "City",
"state": "State",
"country": "Country",
"pincode": "123456",
"contact": "1234567890"
},
"paymentMode": "cod"
}
]
}
I want to update the order product status to shipped, canceled, etc. I have to set status in desired position like the following.
I waant to update the mongodb databse and have to get the result in like the following result.
{
"_id": {
"$oid": "633ab3c11e6e97b6332f56a1"
},
"orders": [
{
"date": "6/10/2022",
"productDetails": {
"0": {
"name": "Top",
"price": "1235",
"quantity": 1,
"status": "caneled"
},
"1": {
"name": "Shirt",
"price": "1235",
"quantity": 1,
"status": "shipped"
}
},
"billingAddress": {
"address": "My Address",
"city": "City",
"state": "State",
"country": "Country",
"pincode": "123456",
"contact": "1234567890"
},
"paymentMode": "cod"
},
{
"date": "6/10/2022",
"productDetails": {
"0": {
"name": "Top",
"price": "1235",
"quantity": 3,
"status": "canceled"
},
"1": {
"name": "Shirt",
"price": "1235",
"quantity": 1,
"status": "shipped"
}
},
"billingAddress": {
"address": "My Address",
"city": "City",
"state": "State",
"country": "Country",
"pincode": "123456",
"contact": "1234567890"
},
"paymentMode": "cod"
},
{
"date": "6/10/2022",
"productDetails": {
"0": {
"name": "Shirt",
"price": "234",
"quantity": 1,
"status": "placed"
},
"1": {
"name": "Top",
"price": "123",
"quantity": 1,
"status": "shipped"
}
},
"billingAddress": {
"address": "My Address",
"city": "City",
"state": "State",
"country": "Country",
"pincode": "123456",
"contact": "1234567890"
},
"paymentMode": "cod"
}
]
}
According to my research, the correct method is as follows:
db.orders.updateOne({_id: ObjectId("633a6a73dd9f8cce0029e53d")},{$set:{"orders.0.productDetails.0.status": "shipped"}})
And the result is :-
{
"_id": {
"$oid": "633ab3c11e6e97b6332f56a1"
},
"orders": [
{
"_id": {
"$oid": "633e8f2d3e3f12f07438cc64"
},
"date": "6/10/2022",
"productDetails": {
"0": {
"name": "Top",
"price": "1235",
"quantity": 1,
"status": "shipped"
},
"1": {
"name": "Shirt",
"price": "1235",
"quantity": 1,
"status": "placed"
}
},
"billingAddress": {
"address": "My Address",
"city": "City",
"state": "State",
"country": "Country",
"pincode": "123456",
"contact": "1234567890"
},
"paymentMode": "cod"
},
{
"_id": {
"$oid": "633e96533e3f12f07438cc65"
},
"date": "6/10/2022",
"productDetails": {
"0": {
"name": "Top",
"price": "1235",
"quantity": 1,
"status": "placed"
},
"1": {
"name": "Shirt",
"price": "1235",
"quantity": 1,
"status": "placed"
},
"2": {
"name": "Jeans",
"price": "1234",
"quantity": 1,
"status": "placed"
}
},
"billingAddress": {
"address": "My Address",
"city": "City",
"state": "State",
"country": "Country",
"pincode": "123456",
"contact": "1234567890"
},
"paymentMode": "cod"
}
]
}

Combine nested data from 2 api

I have 2 api:
Having bank details
Other having user account detail.
User can have more than 1 account with 1 bank. I have to fetch bank name and logo from bank api, and show all the user accounts with name, amount banks name and logo.
Two api data looks like below :
Bank API:
[
{
"id": 2,
"name": "KlikBCA",
"bank_code": "KlikBCA",
"country_code": "ID",
"country_name": "India",
"logo": "url",
"createdAt": null,
"updatedAt": "2021-07-26T14:58:03.000+00:00",
"channels": null
},
{
"id": 7,
"name": "BNI Internet Banking",
"bank_code": "BNI Internet Banking",
"country_code": "ID",
"country_name": "Indis",
"primary_color": "url",
"logo": null,
"createdAt": null,
"updatedAt": "2021-07-26T14:58:03.000+00:00",
"channels": null
}
]
User API:
[
{
"id": "sdf",
"bankId": "7",
"data": [
{
"accountId": “1234”,
"accountHolder": "John Doe",
"accountNumber": "587-2673-989",
"balances": {
"available": 500000,
}
},
{
"accountId": “2345”,
"accountHolder": "John Doe",
"accountNumber": "987-0675-789",
"balances": {
"available": 7500000
}
}
]
},
{
"id": "f230",
"bankId": "3",
"data": [
{
"accountId": "9876",
"accountHolder": "Charls",
"accountNumber": "765-6543-345",
"balances": {
"available": 200000
}
}
]
}
]
i need data to be like this..so that I can use it in my next component, which will take each account with all the bank details.
[
{
"id": "sdf",
"bankId": "7",
"data":
{
"accountId": “1234”,
"accountHolder": "John Doe",
"accountNumber": "587-2673-989",
"balances": {
"available": 500000,
}
},
"bank":{
"id": 7,
"name": "BNI Internet Banking",
"bank_code": "BNI Internet Banking",
"country_code": "ID",
"country_name": "Indis",
"primary_color": "url",
"logo": null,
"createdAt": null,
"updatedAt": "2021-07-26T14:58:03.000+00:00",
"channels": null
}
},
{"id": "sdf",
"bankId": "7",
"data": {
"accountId": “2345”,
"accountHolder": "John Doe",
"accountNumber": "987-0675-789",
"balances": {
"available": 7500000
}
},
"bank":{
"id": 7,
"name": "BNI Internet Banking",
"bank_code": "BNI Internet Banking",
"country_code": "ID",
"country_name": "Indis",
"primary_color": "url",
"logo": null,
"createdAt": null,
"updatedAt": "2021-07-26T14:58:03.000+00:00",
"channels": null
}
},
{
"id": "f230",
"bankId": "3",
"data": {
"accountId": "9876",
"accountHolder": "Charls",
"accountNumber": "765-6543-345",
"balances": {
"available": 200000
},
"bank":{
"name": "Mandiri",
"bank_code": "Mandiri",
"country_code": "ID",
"country_name": "India",
"primary_color": "url",
"logo": null,
"createdAt": null,
"updatedAt": "2021-07-26T14:58:03.000+00:00",
"channels": null
}
}
}
]
You can use loop to merge two api json into one,
this is my code.
hope I can help u.
const banks = [{
"id": 2,
"name": "KlikBCA",
"bank_code": "KlikBCA",
"country_code": "ID",
"country_name": "India",
"logo": "url",
"createdAt": null,
"updatedAt": "2021-07-26T14:58:03.000+00:00",
"channels": null
}, {
"id": 3,
"name": "Mandiri",
"bank_code": "Mandiri",
"country_code": "ID",
"country_name": "India",
"primary_color": "url",
"logo": null,
"createdAt": null,
"updatedAt": "2021-07-26T14:58:03.000+00:00",
"channels": null
}, {
"id": 7,
"name": "BNI Internet Banking",
"bank_code": "BNI Internet Banking",
"country_code": "ID",
"country_name": "Indis",
"primary_color": "url",
"logo": null,
"createdAt": null,
"updatedAt": "2021-07-26T14:58:03.000+00:00",
"channels": null
}]
const users = [{
"id": "sdf",
"bankId": "7",
"data": [{
"accountId": "1234",
"accountHolder": "John Doe",
"accountNumber": "587-2673-989",
"balances": {
"available": 500000,
}
}, {
"accountId": "2345",
"accountHolder": "John Doe",
"accountNumber": "987-0675-789",
"balances": {
"available": 7500000
}
}]
}, {
"id": "f230",
"bankId": "3",
"data": [{
"accountId": "9876",
"accountHolder": "Charls",
"accountNumber": "765-6543-345",
"balances": {
"available": 200000
}
}]
}]
let resultApi = users.map(u => {
const t = banks.find(b => b.id == u.bankId) || {}
return {
...u,
...t
}
})
console.log(resultApi);
first of all, a feedback, you should write json inside code blocks for readability.
your question seems incomplete as what you want to achieve? you want to show these data in UI (html) or create a new json? and how far have you tried?
try to share a codesandbox/codepen link.
remember, you need to loop through both the arrays and first store bank data inside user's account by matching user.bankId === bank.id and then with the new data set you can show it wherever you want. give it a try first and then let us know in comments if you're unable to achieve.

Where can I find a full list of PayPal Checkout Button properties?

PayPal Checkout
offers a JavaScript function to display buttons example:
<script>
paypal.Buttons({
createOrder: function(data, actions) {
// Set up the transaction
return actions.order.create({
purchase_units: [{
amount: {
value: '0.01'
}
}]
});
}
}).render('#paypal-button-container');
</script>
As you see, the Buttons function has an object as a parameter. Where can I see all the available properties of this object?
Check https://developer.paypal.com/docs/api/orders/v2/#orders_create
Request body section contains all the data that can be passed.
You can find it on the source code: https://github.com/paypal/paypal-checkout-components/blob/master/docs/implement-checkout.md
Here's a full list of everything you need
"purchase_units": [
{
"reference_id": "store_mobile_world_order_1234",
"description": "Mobile World Store order-1234",
"amount": {
"currency": "USD",
"details": {
"subtotal": "1.09",
"shipping": "0.02",
"tax": "0.33"
},
"total": "1.44"
},
"payee": {
"email": "seller#example.com"
},
"items": [
{
"name": "NeoPhone",
"sku": "sku03",
"price": "0.54",
"currency": "USD",
"quantity": "1"
},{
"name": "Fitness Watch",
"sku": "sku04",
"price": "0.55",
"currency": "USD",
"quantity": "1"
}
],
"shipping_address": {
"line1": "2211 N First Street",
"line2": "Building 17",
"city": "San Jose",
"country_code": "US",
"postal_code": "95131",
"state": "CA",
"phone": "(123) 456-7890"
},
"shipping_method": "United Postal Service",
"partner_fee_details": {
"receiver": {
"email": "partner#example.com"
},
"amount": {
"value": "0.01",
"currency": "USD"
}
},
"payment_linked_group": 1,
"custom": "custom_value_2388",
"invoice_number": "invoice_number_2388","payment_descriptor": "Payment Mobile World"
}
],
"redirect_urls": {
"return_url": "https://example.com/return",
"cancel_url": "https://example.com/cancel"
}
}'

item_option_selection - Where to put in the json request?

I see in the documentation that there's a way to set item options: https://developer.paypal.com/docs/api/payments/#definition-item_option_selection
Where do i exactly put this in paypal example? I don't find examples or any instruction on paypal developers documentation
{
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"transactions": [
{
"amount": {
"total": "30.11",
"currency": "USD",
"details": {
"subtotal": "30.00",
"tax": "0.07",
"shipping": "0.03",
"handling_fee": "1.00",
"shipping_discount": "-1.00",
"insurance": "0.01"
}
},
"description": "The payment transaction description.",
"custom": "EBAY_EMS_90048630024435",
"invoice_number": "48787589673",
"payment_options": {
"allowed_payment_method": "INSTANT_FUNDING_SOURCE"
},
"soft_descriptor": "ECHI5786786",
"item_list": {
"items": [
{
"name": "hat",
"description": "Brown hat.",
"quantity": "5",
"price": "3",
"tax": "0.01",
"sku": "1",
"currency": "USD"
},
{
"name": "handbag",
"description": "Black handbag.",
"quantity": "1",
"price": "15",
"tax": "0.02",
"sku": "product34",
"currency": "USD"
}
],
"shipping_address": {
"recipient_name": "Brian Robinson",
"line1": "4th Floor",
"line2": "Unit #34",
"city": "San Jose",
"country_code": "US",
"postal_code": "95131",
"phone": "011862212345678",
"state": "CA"
}
}
}
],
"note_to_payer": "Contact us for any questions on your order.",
"redirect_urls": {
"return_url": "https://www.paypal.com/return",
"cancel_url": "https://www.paypal.com/cancel"
}
}'
The object is not part of the Paypal REST api anymore. It will be removed soon
Reference: https://github.com/paypal/PayPal-REST-API-issues/issues/137

Use Javascript object variable for an if statement

I'm using javascript to display information of a shop basket. I know how to pull the data and create elements from it, but I need to use one of the variables for an if statement, and am unsure how to.
If this is true: "isPunchOut": false, then I want to target that with jQuery, and do something like $(".button").remove();
How do I do this?
var retailerData = {
"del": {
"zip": "",
"city": ""
},
"user": {
"country": "",
"phone": "",
"nbrOrders": 0,
"isPunchOut": false,
"name": "",
"salesPerson": "",
"customerNo": "",
"email": ""
},
"order": {
"shippingSum": 0.0,
"shippingFormatSum": "\u20AC0",
"orderno": "0",
"orderFormatSum": "\u20AC130",
"voucher": "",
"orderFormatVat": "\u20AC27,30",
"currencySymbol": "\u20AC",
"currency": "EUR",
"orderVat": 27.3,
"orderSum": 130.0,
"items": [{
"imageURI": "\/imgr\/8c82380c-65f5-43aa-83ad-fae1215b5b39\/70\/70",
"qtyAvail": 7,
"price": 130.0,
"qty": 1,
"artno": "D630-T7100-GE-REF",
"vat": 27.3,
"formatVat": "\u20AC27,30",
"id": "52307",
"label": "D630 C2D-T7100/2GB/80GB/DVD/14"/NO COA WLAN",
"category": "Computers - Notebooks",
"formatPrice": "\u20AC130",
"manufacturer": "Dell"
}]
}
}
You can take a look at below JS code for reference:
var isPunchOut = retailerData["user"]["isPunchOut"];
if(isPunchOut === false)
$(".button").remove();
I would say you should try some code first and put where you get stuck .We not here to write code for your problems.
var retailerData = {
"del": {
"zip": "",
"city": ""
},
"user": {
"country": "",
"phone": "",
"nbrOrders": 0,
"isPunchOut": false,
"name": "",
"salesPerson": "",
"customerNo": "",
"email": ""
},
"order": {
"shippingSum": 0.0,
"shippingFormatSum": "\u20AC0",
"orderno": "0",
"orderFormatSum": "\u20AC130",
"voucher": "",
"orderFormatVat": "\u20AC27,30",
"currencySymbol": "\u20AC",
"currency": "EUR",
"orderVat": 27.3,
"orderSum": 130.0,
"items": [{
"imageURI": "\/imgr\/8c82380c-65f5-43aa-83ad-fae1215b5b39\/70\/70",
"qtyAvail": 7,
"price": 130.0,
"qty": 1,
"artno": "D630-T7100-GE-REF",
"vat": 27.3,
"formatVat": "\u20AC27,30",
"id": "52307",
"label": "D630 C2D-T7100/2GB/80GB/DVD/14"/NO COA WLAN",
"category": "Computers - Notebooks",
"formatPrice": "\u20AC130",
"manufacturer": "Dell"
}]
}
}
if(retailerData.user.isPunchOut){
//your jquery operation
}
Checkjsfiddle
What about?
if (!retailerData.user.isPunchOut) {
$(".button").remove();
}

Categories