1

I want to figure to merge JSON. I have two JSON arrays and want to add objects from one array to other having same values

Products

"Products": [
                {
                    "Date": "2015-04-28T12:30:19.107",
                    "Code": "003UYTX1A",
                    "Title": "Divatex Dots Microfiber Queen Bed In the Bag",
                    "Description": "Go crazy with lots of dots with Divatex,                    
                    "Weight": 7.60,
                    "WeightUnit": "pounds",                    
                    "AverageRating": 4.1,                    
                    "EstimatedSalesPerDay": 761,
                    "ProfitScore": 50,
                    "UpdateStatus": 0,
                    "VersionCode": 23
                },
                {
                    "Date": "2015-04-28T12:29:46.66",
                    "Code": "05461AQDV",
                    "Title": "Wilton 1912-1294 100 Count Party Bags",
                    "Description": "Some Description,                    
                    "Weight": 8.10,
                    "WeightUnit": "pounds",                    
                    "AverageRating": 4.0,                    
                    "EstimatedSalesPerDay": 711,
                    "ProfitScore": 45,
                    "UpdateStatus": 0,
                    "VersionCode": 23
                }]

Detail

"Detail": [
                {
                    "Date": "2015-04-28T12:29:45.95",
                    "Code": "003UYTX1A",
                    "CategoryId": "1055398",
                    "CategoryTitle": "Some Title",
                    "TotalReviews": 31
                },
                {
                    "Date": "2015-04-28T12:29:45.95",
                    "Code": "05461AQDV",
                    "CategoryId": "1055398",
                    "CategoryTitle": "Title",                                                
                    "TotalReviews": 211
                },
                {
                    "Date": "2015-04-28T12:29:45.95",
                    "Code": "003UYTX1A",
                    "CategoryId": "1055398",
                    "CategoryTitle": "Category Title",
                    "TotalReviews": 101
                }]

I need to combine these arrays objects on the basis of same Code value on both just like below

      "Combined": [
               {
                    "Date": "2015-04-28T12:30:19.107",
                    "Code": "003UYTX1A",
                    "Title": "Divatex Dots Microfiber Queen Bed In the Bag",
                    "Description": "Go crazy with lots of dots with Divatex,                    
                    "Weight": 7.60,
                    "WeightUnit": "pounds",                    
                    "AverageRating": 4.1,                    
                    "EstimatedSalesPerDay": 761,
                    "ProfitScore": 50,
                    "UpdateStatus": 0,
                    "VersionCode": 23,
                    "Detail":[
                        {
                            "Date": "2015-04-28T12:29:45.95",
                            "Code": "003UYTX1A",
                            "CategoryId": "1055398",
                            "CategoryTitle": "Some Title",
                            "TotalReviews": 31
                        },
                        {
                            "Date": "2015-04-28T12:29:45.95",
                            "Code": "003UYTX1A",
                            "CategoryId": "1055398",
                            "CategoryTitle": "Category Title",
                            "TotalReviews": 101
                        }]
                },
                {
                    "Date": "2015-04-28T12:29:46.66",
                    "Code": "05461AQDV",
                    "Title": "Wilton 1912-1294 100 Count Party Bags",
                    "Description": "Some Description,                    
                    "Weight": 8.10,
                    "WeightUnit": "pounds",                    
                    "AverageRating": 4.0,                    
                    "EstimatedSalesPerDay": 711,
                    "ProfitScore": 45,
                    "UpdateStatus": 0,
                    "VersionCode": 23,
                    "Detail":[
                        {
                            "Date": "2015-04-28T12:29:45.95",
                            "Code": "05461AQDV",
                            "CategoryId": "1055398",
                            "CategoryTitle": "Title",                                                
                            "TotalReviews": 211
                        }]
                }]

Any best idea to get required results? I have to code in javascript.

3 Answers 3

4

If you use underscore ,you can do that easily, http://underscorejs.org/

 _.each(products,function(product,index){

        var detail = _.where(details,{'Code' : product.Code})
        if(detail){
                product.details=detail;
         }
         products[index] = product
    })
Sign up to request clarification or add additional context in comments.

1 Comment

Hmm, I just realize where also accepts a JSON parameter. Brilliant.
2

You can try this:

var output = {Combined: []};
output.Combined = Products.map(function(product){
    var details = Detail.filter(function(detail){
        return detail.Code === product.Code
    }) || [];
    product.Detail = details;
    return product;
});

What the code does is it iterates through your Products, find the matched Detail which may be multiple or even null, and merge it to your associated product object.

Comments

2
var jObject = {}; 

jObject['Combined'] = Products;

for(var i=0;i<Products.length;i++) {
    for(var j=0;j<Detail.length;j++){
        if(Products[i].Code === Detail[j].Code) {
            if(Products[i].Detail == undefined) {
                Products[i].Detail = [];
                Products[i].Detail.push(Detail[j]);
            } else {
                Products[i].Detail.push(Detail[j]);
            }
        }
    }   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.