Append JSON to another JSON - javascript

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.

Related

using Javascript to get unique XML child nodes

I'm a noob trying to build a map for my Com. Prog. class with the Google Maps API.
I have data that I want to output as markers and infowindows.
I used Codeigniter to output an XML from my database. (data modified for privacy)
<markers>
<item0>
<city>Boston</city>
<latitude>0.01111</longitude>
<longitude>1.2345</longitude>
</item0>
<item1>
<city>Los Angeles</city>
<latitude>9.99999</latitude>
<longitude>2.00011</longitude>
</item1>
</markers>
Now I'm trying to use Javascript to get all the child nodes of each "item" tag.
My callback function:
var xml = data.responseXML;
var items = xml.documentElement.getElementsByTagName("markers");
// for loop iterates through each the child nodes of the "item" tags.
Array.prototype.forEach.call(items, function(markerElem) {
var city = nodeValue(markersElem.children.item[0]);
var point = new google.maps.LatLng(
parseFloat(nodeValue(markersElem.children.item[1])),
parseFloat(nodeValue(markersElem.children.item[2])));
});
The main problem (I believe) is that the "item" tags each contain a unique number, so I can't call them with the getElementsByTagName method.
I've tried modifying the items variable to get the children nodes of the "markers" tag:
var items = xml.documentElement.getElementsByTagName("markers").children;
But the console outputs an error where the items variable is now undefined.
I've also tried to modify the XML file, but I can only seem to output XML through Codeigniter's built-in helper. Maybe there is a way to create a DOM document in PHP 7 that I don't know about?
It will be much easier if you convert XML in your controller with the simplexml_load_string() to a PHP array or object and then use the json_encode() to convert it to JSON format, so it will be much easier to work with this JSON array or object in JS code.
In your controller do this and send it to your view:
json_encode(simplexml_load_string(your XML here), JSON_UNESCAPED_UNICODE);
I would suggest that you would use XPath for that it will allow you to pull all the element under a cetrain path which you can then iterate over as normal.

getting last element in JSON not working

I am trying to take the last element in a JSON. I know order is not preserved, but the keys are unix timestamps so I can just sort. I am working from this question:
get last element of a json object in javascript
But I get nothing logged to the console and no errors.
my code:
var mydata = {"1509937402379":"7348.01","1509937412486":"7348.01","1509937422253":"7348.01","1509937426286":"7348.01","1509937430066":"7345.54"}
console.log(mydata[Object.keys(obj).sort().pop()]);
https://jsfiddle.net/Lmb5sd1m/1/
You missed.. .You require to use Object.keys(mydata).
var mydata = {"1509937402379":"7348.01","1509937412486":"7348.01","1509937422253":"7348.01","1509937426286":"7348.01","1509937430066":"7345.54"}
Review same fiddle https://jsfiddle.net/Lmb5sd1m/1/

Access JSON reply

I am using Amazon Web Services database dynamodb. It returns a a JSON which looks liek this:
{"Responses":{"friends":[{"to_username":"u1","from_username":"u2"}]},"UnprocessedKeys":{}}
I need to get the length of the friends array and also get individual values (e.g to_username in the first element of the array which is "u1" in the example).
I have tried something like this:
console.log(data.responses.friends.length); //get length (data is the object I get returned from my async call
console.log(data.responses.friends.to_username[0]); //get to_username of the first element in the array
Both return undefined.
Case matters!
console.log(data.Responses.friends.length); //get length (data is the object I get returned from my async call
console.log(data.Responses.friends.to_username[0]); //get to_username of the first element in the array
produces the correct results. Note the uppercase R in Responses.
Javascript is a case sensitive language. Please ensure the case in your code matches with the case in your response.

How To Get javascript Array in Code behind

This is javascript array
var data =['athar','naveed','123','abx'];
Now I want to access this array in code behind array or list variable. Don't want to use Hidden field.
If you want to use in anyother javascript function,you can simply use data[0] to access first element i.e.,athar.
data.length will give you the count of values present in the array.

How to retrieve the JSON object key which having numbered name

I'm trying to pass a number of JSON objects from my server which have names appended with a number to differentiate each other.
I am getting a problem while retrieving these objects. It is unable to append a number with its common name.
Objects look like this:
{"shareInfo":[{"uname1":"abc","uname2":"sds","uname3":"uuh",.....}]}
I tried appending an integer variable to it.
data.shareInfo[i].uname+''+i;
But it is not reading the variable together. I mean it is not taking it like uname0,uname1,...
Please anyone tell me how to append the number to retrieve the data, thanks.
data.shareInfo[i]["uname" + i] should work.
It's worth noting that your example data only contains one array element in data.shareInfo, so what you might actually need is:
data.shareInfo[0]["uname" + i]
But I can't tell which would be correct from your test data.

Categories