0

I have two JSON objects which are Youtube API responses.

I want to append some part of the second JSON (source) to a particular position in the first JSON (destination) and finally have one merged JSON to send to view.

I've tried like so:

var merged = Object.keys(source).forEach(function(key) {

    destination.items[key].contentDetails = source[key].items[key].contentDetails;

})

They both contain same number of item sets so I use the same index key for destination within the loop of source and append each to the destination JSON.

destination.items[key].contentDetails is a valid reference that returns correct value in the console but when inside this loop it's undefined. What am I doing wrong here? I'm not quite sure about this practice for such task so I'd greatly appreciate for some direction.

Seong Lee
  • 10,314
  • 25
  • 68
  • 106
  • What's the downvote for and how this is off-topic? I'm not experienced with programming but trying to learn something here. – Seong Lee Dec 23 '15 at 13:44
  • Sorry for all the downvotes! I think most people here are tired of newbies and need to take a walk. Could this be the answer to your question? http://stackoverflow.com/questions/18498801/how-to-merge-two-json-objects-values-by-keys – birdoftheday Dec 23 '15 at 13:51
  • or maybe you could try http://stackoverflow.com/questions/736590/add-new-attribute-element-to-json-object-using-javascript – Irtza.QC Dec 23 '15 at 13:52
  • are you sure that destination.items[key].contentDetails is undefined? the weird thing to me is that you use index key twice on the right side, maybe that's your problem – juraj Dec 23 '15 at 13:52
  • Don't worry and get used to it, on this site, people get downvoted without any reason. It sucks, but that's the way it is. About your problem, what is "wrong"? What's the intended result and what do you get instead? – Jeremy Thille Dec 23 '15 at 13:52
  • 1
    @juraj I've used index twice for source because that's how that JSON is structured with nested array inside. – Seong Lee Dec 23 '15 at 13:55
  • @juraj why so? No problem using `source["somekey"].items["somekey"].contentDetails`, as long as it's defined. Sub-keys can very well have the same name as their parent's. – Jeremy Thille Dec 23 '15 at 13:55
  • @JeremyThille I know, but it seems weird :) – juraj Dec 23 '15 at 13:58
  • This question is an extension of this question http://stackoverflow.com/questions/34430504/merge-json-object-response-into-one-using-underscore-js Which later on, I tried lodash _.extend but it merges like {a,b,c}, {a,b,c} while I want it to be {a,a}, {b,b}, {c,c} ... So here I was trying something different..which seems weird to me also but don't know what I can do to resolve. – Seong Lee Dec 23 '15 at 14:00
  • 1
    try using Array.map https://jsfiddle.net/ym1npkg0/ – juraj Dec 23 '15 at 14:14
  • FYI: you're dealing with objects ... json is a string notation of a javasacript object – Jaromanda X Dec 23 '15 at 14:16
  • @JaromandaX Yes..I'm aware of it..I was going to call it JSON object but wanted to simplify my wording. Thanks I was confused about this concept before :) – Seong Lee Dec 23 '15 at 14:18
  • @juraj I will try Array.map as per your fiddle. Will update how I went. Thank you! – Seong Lee Dec 23 '15 at 14:18
  • @juraj Array.map worked for me! I'm happy to accept your answer if you post one. Thanks very much. – Seong Lee Dec 26 '15 at 13:30

0 Answers0