Reputation: 1414
I have a blob of JSON which I get back from the server and I'm trying to get a specific obj and use it in a new JSON that I need to send it back.
JSON:
var oldJson = {"Message":"string","Balance":0,"OrderId":94,"SiteId":1234,"OrderLine":[{"Id":"e672f84e-4b97-4cd7-826a-d0e57247ffe3","Product":{"Id":21,"Codes":["1112"],"Sku":"CS1112"},"Description":"testing 123","QuantityRequired":1,"QuantityPicked":0,"SourceSite":{"Id":1,"PropertyCode":"","StoreCode":"1"},"Status":"Preparing","TotalOriginalValue":0,"TotalAdjustedValue":0,"AdjustmentReference":null,"Note":null,"PromotionLines":[],"Taxes":[],"CancellationReason":null,"QuantityIssued":0,"TotalPaidAmount":null,"CustomData":{}},{"Id":"c9f18678-11f0-4fbb-afe4-db926ae97d04","Product":{"Id":9,"Codes":["1113"],"Sku":"CS1113"},"Description":"testing 123","QuantityRequired":1,"QuantityPicked":0,"SourceSite":{"Id":1,"PropertyCode":"","StoreCode":"1"},"Status":"Preparing","TotalOriginalValue":0,"TotalAdjustedValue":0,"AdjustmentReference":null,"Note":null,"PromotionLines":[],"Taxes":[],"CancellationReason":null,"QuantityIssued":0,"TotalPaidAmount":null,"CustomData":{}}],"CustomData":{}}
I want to get the Product
object with the key name, I managed to get the obj but without its key name Product
I tried the code below, its giving me the Product key name but the data in it are the same product (which is wrong). Also how can I add extra object when you push the data in the array?
So it would look like this:
"Product": { "Id": 21, "Codes": ["1112"], "Sku": "1112" }, "TotalValue": 0, "Quantity": 1
var obj = {}
var el = [];
for (var i = 0; i < oldJson.OrderLine.length; i++) {
obj["Product"] = json.OrderLine[i].Product;
el.push(obj);
}
Trying to rebuild the same JSON but with just the Product
in the OrderLine
.
var rebuild = {
"Message": "string",
"Balance": 0,
"OrderId": 10,
"SiteId": 1234,
"OrderLine": el,
"CustomData": {}
}
Expected output:
{
"Message": "string",
"Balance": 0,
"OrderId": 10,
"SiteId": 1234,
"OrderLine": [
{
"Product": { "Id": 21, "Codes": ["1112"], "Sku": "1112" }, "TotalValue": 0, "Quantity": 1
},
{
"Product": { "Id": 9, "Codes": ["1113"], "Sku": "1113" }, "TotalValue": 0, "Quantity": 1
}
],
"CustomData": {}
}
Here's a JsFiddle
Upvotes: 0
Views: 216
Reputation: 32354
You can try the following each loop:
var json = {
"Message": "string",
"Balance": 0,
"OrderId": 94,
"SiteId": 1234,
"OrderLine": [{
"Id": "e672f84e-4b97-4cd7-826a-d0e57247ffe3",
"Product": {
"Id": 21,
"Codes": [
"1112"
],
"Sku": "CS1112"
},
"Description": "testing 123",
"QuantityRequired": 1,
"QuantityPicked": 0,
"SourceSite": {
"Id": 1,
"PropertyCode": "",
"StoreCode": "1"
},
"Status": "Preparing",
"TotalOriginalValue": 0,
"TotalAdjustedValue": 0,
"AdjustmentReference": null,
"Note": null,
"PromotionLines": [
],
"Taxes": [
],
"CancellationReason": null,
"QuantityIssued": 0,
"TotalPaidAmount": null,
"CustomData": {
}
},
{
"Id": "c9f18678-11f0-4fbb-afe4-db926ae97d04",
"Product": {
"Id": 9,
"Codes": [
"1113"
],
"Sku": "CS1113"
},
"Description": "testing 123",
"QuantityRequired": 1,
"QuantityPicked": 0,
"SourceSite": {
"Id": 1,
"PropertyCode": "",
"StoreCode": "1"
},
"Status": "Preparing",
"TotalOriginalValue": 0,
"TotalAdjustedValue": 0,
"AdjustmentReference": null,
"Note": null,
"PromotionLines": [
],
"Taxes": [
],
"CancellationReason": null,
"QuantityIssued": 0,
"TotalPaidAmount": null,
"CustomData": {
}
}
],
"CustomData": {
}
}
$.each(json.OrderLine, function(i, v) {
json.OrderLine[i] = {"product":v.Product,"TotalValue": v.TotalOriginalValue, "Quantity": v.QuantityRequired}
});
console.log(json)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
or using for loop:
var json = {
"Message": "string",
"Balance": 0,
"OrderId": 94,
"SiteId": 1234,
"OrderLine": [{
"Id": "e672f84e-4b97-4cd7-826a-d0e57247ffe3",
"Product": {
"Id": 21,
"Codes": [
"1112"
],
"Sku": "CS1112"
},
"Description": "testing 123",
"QuantityRequired": 1,
"QuantityPicked": 0,
"SourceSite": {
"Id": 1,
"PropertyCode": "",
"StoreCode": "1"
},
"Status": "Preparing",
"TotalOriginalValue": 0,
"TotalAdjustedValue": 0,
"AdjustmentReference": null,
"Note": null,
"PromotionLines": [
],
"Taxes": [
],
"CancellationReason": null,
"QuantityIssued": 0,
"TotalPaidAmount": null,
"CustomData": {
}
},
{
"Id": "c9f18678-11f0-4fbb-afe4-db926ae97d04",
"Product": {
"Id": 9,
"Codes": [
"1113"
],
"Sku": "CS1113"
},
"Description": "testing 123",
"QuantityRequired": 1,
"QuantityPicked": 0,
"SourceSite": {
"Id": 1,
"PropertyCode": "",
"StoreCode": "1"
},
"Status": "Preparing",
"TotalOriginalValue": 0,
"TotalAdjustedValue": 0,
"AdjustmentReference": null,
"Note": null,
"PromotionLines": [
],
"Taxes": [
],
"CancellationReason": null,
"QuantityIssued": 0,
"TotalPaidAmount": null,
"CustomData": {
}
}
],
"CustomData": {
}
}
for (var i = 0; i < json.OrderLine.length; i++) {
json.OrderLine[i] = {
"product": json.OrderLine[i].Product,
"TotalValue": json.OrderLine[i].TotalOriginalValue,
"Quantity": json.OrderLine[i].QuantityRequired
}
}
console.log(json)
Upvotes: 1
Reputation: 252
You can access object using key.. Try this
var product = $.map(oldJson, function (data, key) {
if (key == "OrderLine") return data;
});
Upvotes: 0
Reputation: 36703
All you need to do is to use .map
and fetch products
var product = json.OrderLine.map(function(el){
return {"Product": el.Product, "TotalValue":el.TotalValue, "Quantity":el.Quantity};
});
var rebuild = {
"Message": "string",
"Balance": 0,
"OrderId": 10,
"SiteId": 1234,
"OrderLine": product,
"CustomData": {}
}
Try this fiddle
Upvotes: 1