Related
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"
}
]
}
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.
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"
}
}'
{
"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);
So I'm trying to put together some JSON and parse it out into jquery but I am doing something wrong with my JSON syntax. I tried running it through a validator but it doesn't really tell me what I'm doing wrong. Can somebody point out the error of my ways?
var searchresults = [
{
"providerlisting": [
{
"nametitle": "Cory M Spears, MD, FACP",
"caretype": "Internal Medicine",
"preferredProvider": true,
"address1": "289 N. Highland Ave.",
"address2": "",
"cityStateZip": "Atlanta, GA 30306",
"coverage": "/images/example.png",
"status": "Out of Network",
"psn": "",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere",
},
{
"nametitle": "Jimmy Dean, MD, FACP",
"caretype": "External Medicine",
"preferredProvider": false,
"address1": "3 Piedmont Rd.",
"address2": "",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "In Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere",
},
{
"nametitle": "John Doe, DD, PM",
"caretype": "Internal Medicine",
"preferredProvider": true,
"address1": "500 Ponce De Leon Ave",
"address2": "Suite 5",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "Out of Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere",
}]
},
{
"categories": [{
"categoryMenu": [
{
"providertype": [
{
"title": "Doctor",
"link": "#doctor",
"amount": "400"
},
{
"title": "Hospital",
"link": "#hospital",
"amount": "40"
},
{
"title": "Urgent Care",
"link": "#urgentCare",
"amount": "37"
}
]
},
{
"specialty": [
{
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "2"
},
{
"title": "Audiology",
"link": "#audiology",
"amount": "3"
},
{
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "6"
},
{
"title": "Ambulatory Surgical Center",
"link": "#ambulatorySurgicalCenter",
"amount": "4"
}
]
},
{
"gender": [
{
"title": "Male",
"link": "#male",
"amount": "67"
},
{
"title": "Female",
"link": "#female",
"amount": "3"
}
]
}
}]
}];
Remove the , at the end of each
"rating": "urlhere"
there was a ] missing on the third last line, below is a valid json object
var searchresults = [{
"providerlisting": [{
"nametitle": "Cory M Spears, MD, FACP",
"caretype": "Internal Medicine",
"preferredProvider": true,
"address1": "289 N. Highland Ave.",
"address2": "",
"cityStateZip": "Atlanta, GA 30306",
"coverage": "/images/example.png",
"status": "Out of Network",
"psn": "",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere"
}, {
"nametitle": "Jimmy Dean, MD, FACP",
"caretype": "External Medicine",
"preferredProvider": false,
"address1": "3 Piedmont Rd.",
"address2": "",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "In Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere"
}, {
"nametitle": "John Doe, DD, PM",
"caretype": "Internal Medicine",
"preferredProvider": true,
"address1": "500 Ponce De Leon Ave",
"address2": "Suite 5",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "Out of Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere"
}]
}, {
"categories": [{
"categoryMenu": [{
"providertype": [{
"title": "Doctor",
"link": "#doctor",
"amount": "400"
}, {
"title": "Hospital",
"link": "#hospital",
"amount": "40"
}, {
"title": "Urgent Care",
"link": "#urgentCare",
"amount": "37"
}]
}, {
"specialty": [{
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "2"
}, {
"title": "Audiology",
"link": "#audiology",
"amount": "3"
}, {
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "6"
}, {
"title": "Ambulatory Surgical Center",
"link": "#ambulatorySurgicalCenter",
"amount": "4"
}]
}, {
"gender": [{
"title": "Male",
"link": "#male",
"amount": "67"
}, {
"title": "Female",
"link": "#female",
"amount": "3"
}]
}]
}]
}];
There are only , commas after a key/value pairs in objects if there is another one following it. (Same goes for Arrays as well)
For example:
var a = {
key : 'value',
keyB : 'value' // <-- there is no trailing comma before an object ends
};
Do you know http://jsonlint.com/?
You have to realise, an object in javascript is nearly the same like an associative array.
I think you should read a bit more about Objects and Arrays in Javascript.
Try this:
var searchresults = {
"providerlisting": [
{
"nametitle": "Cory M Spears, MD, FACP",
"caretype": "Internaenter code herel Medicine",
"preferredProvider": true,
"address1": "289 N. Highland Ave.",
"address2": "",
"cityStateZip": "Atlanta, GA 30306",
"coverage": "/images/example.png",
"status": "Out of Network",
"psn": "",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere"
},
{
"nametitle": "Jimmy Dean, MD, FACP",
"caretype": "External Medicine",
"preferredProvider": false,
"address1": "3 Piedmont Rd.",
"address2": "",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "In Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere"
},
{
"nametitle": "John Doe, DD, PM",
"caretype": "Internal Medicine",
"preferredProvider": true,
"address1": "500 Ponce De Leon Ave",
"address2": "Suite 5",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "Out of Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere"
}
],
"categories": {
"categoryMenu": {
"providertype": [
{
"title": "Doctor",
"link": "#doctor",
"amount": "400"
},
{
"title": "Hospital",
"link": "#hospital",
"amount": "40"
},
{
"title": "Urgent Care",
"link": "#urgentCare",
"amount": "37"
}
],
"specialty": [
{
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "2"
},
{
"title": "Audiology",
"link": "#audiology",
"amount": "3"
},
{
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "6"
},
{
"title": "Ambulatory Surgical Center",
"link": "#ambulatorySurgicalCenter",
"amount": "4"
}
],
"gender": [
{
"title": "Male",
"link": "#male",
"amount": "67"
},
{
"title": "Female",
"link": "#female",
"amount": "3"
}
]
}
}
}
Please find the corrected Json string
var searchresults = [
{
"providerlisting": [
{
"nametitle": "Cory M Spears, MD, FACP",
"caretype": "Internal Medicine",
"preferredProvider": true,
"address1": "289 N. Highland Ave.",
"address2": "",
"cityStateZip": "Atlanta, GA 30306",
"coverage": "/images/example.png",
"status": "Out of Network",
"psn": "",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere",
},
{
"nametitle": "Jimmy Dean, MD, FACP",
"caretype": "External Medicine",
"preferredProvider": false,
"address1": "3 Piedmont Rd.",
"address2": "",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "In Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere",
},
{
"nametitle": "John Doe, DD, PM",
"caretype": "Internal Medicine",
"preferredProvider": true,
"address1": "500 Ponce De Leon Ave",
"address2": "Suite 5",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "Out of Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere",
}]
},
{
"categories": [{
"categoryMenu": [
{
"providertype": [
{
"title": "Doctor",
"link": "#doctor",
"amount": "400"
},
{
"title": "Hospital",
"link": "#hospital",
"amount": "40"
},
{
"title": "Urgent Care",
"link": "#urgentCare",
"amount": "37"
}
]
},
{
"specialty": [
{
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "2"
},
{
"title": "Audiology",
"link": "#audiology",
"amount": "3"
},
{
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "6"
},
{
"title": "Ambulatory Surgical Center",
"link": "#ambulatorySurgicalCenter",
"amount": "4"
}
]
},
{
"gender": [
{
"title": "Male",
"link": "#male",
"amount": "67"
},
{
"title": "Female",
"link": "#female",
"amount": "3"
}
]
}
]
}]
}];
alert(JSON.stringify(searchresults))