Can't Convert Json to jQuery - javascript

This is my Ajax response from database :
{
"docs": [{
"_id":"5be81e62-f91d-4185-bef5-2eabdf048578",
"_rev":"6-171639a97982fd7d04a81ed070b2e752",
"profile_id":"5be81e62-f91d-4185-bef5-2eabdf048578",
"name":"omar"
}],
"bookmark": "g2wAAAABaANkAB9kYmNvcmVAZGI1LmplbmV2ZXIuY2xvdWRhbnQubmV0bAAAAAJuBAAAAADAbgQA_____2poAkY_8AAAAAAAAGEBag"
}
I want to convert it to jQuery for Example :
var JsonData = jQuery.parseJSON(responseData.Value);
alert(JsonData.docs._id);
But the alert message show : undefined
Need your help please and thanks.

docs is an array. An arrays store multiple docs in a single variable.
You need to tell the runtime which doc you want to attain the _id property of.
In your case there is only one doc so you can access the first using an index initializer:
alert(JsonData.docs[0]._id);
The [0] part pulls the first doc from the array.

Related

How to retrieve data from json data

[{"id":7,"message":"This is another test message","taker_id":"131","giver_id":"102","status":"0","stamp":"2016-08-11"}]
That's my response. I try to get a datum. I have tried data.id but it fails and returns undefined.
As I assume that you are working with a JSON string, you first have to parse the string into and JSON object. Else you couldn't reach any of the properties.
parsedData = JSON.parse(data);
Then you can get your property:
parsedData[0].id
This seems to work just fine
var data = [{
"id":7,
"message":"This is another test message",
"taker_id":"131",
"giver_id":"102",
"status":"0",
"stamp":"2016-08-11"
}];
console.log(data[0].id);
https://jsbin.com/jewatakize/
if you just want to get the id from this one object then data[0].id will work just fine.
If you expect to have multiple objects in that same array then you can use a loop.
for example if this is angular you can do:
<div ng-repeat='info in data'>
<p>{{info.id}}</p>
</div>
This will allow you to iterate through multiple objects within the array and get all id's.
The problem here is that you have here an array of objects, and you are trying to access it without indexing.
You should first parse it using and then access the object by indexing
let objects = JSON.parse(data)
console.log(objects[0].id)

Converting json array to query string gives null

What I am trying to achieve is to send a json object as a querystring to an API.
The json object I have is:
{
"Name":"Dlsajdsa",
"ImageUrls":["/Images/Facility/8353/85e26a18-4366-4412-b37a-72d94f2ccda5.jpg"]
}
I would like to convert this to something like ?Name=&ImageUrls=
However, when using $.param(json) I get the following:
?Name=Dlsajdsa&ImageUrls%5B%5D=%2FImages%2FFacility%2F8353%2F85e26a18-4366-4412-b37a-72d94f2ccda5.jpg
This result, on the API point of view, that the ImageUrls array is null.
What am I missing here?
you can try $.param(json,true)
Based on the documentation of jQuery.param, I'd say this is the piece of code you want to use:
var json = {
"Name":"Dlsajdsa",
"ImageUrls":["/Images/Facility/8353/85e26a18-4366-4412-b37a-72d94f2ccda5.jpg"]
};
var recursiveDecoded = decodeURIComponent($.param(json));
// "Name=Dlsajdsa&ImageUrls[]=/Images/Facility/8353/85e26a18-4366-4412-b37a-72d94f2ccda5.jpg"
The query parameter is called ImageUrls%5B%5D, that means ImageUrls[], not ImageUrls as you expect.

Read a HashMap in App Script

I try to read my data on a Hash Table but I search in the internet but i don't find a solution.
KPIs.push( {name: [data[0][j]], unite :[data[1][j]], order: [data[2][j]], column:[j] , area:[getArea(data[0][j])] } ) ;
I try :
KPIs.value["name"] // doesn't work
KPIs.length // work
How can I read this HashTable ?
Thanks for your help.
Based on your code it appears you are pushing an Object onto an Array, but you attempt to access the object properties directly on the Array, rather than on the element in the Array.
You'll first need to access the correct Array element, before attempting to access your object properties:
KPIs[0].name
or, to loop over them:
for(var i in KPIs){
var name = KPIs[i].name;
Logger.log(name);
}
See details on Arrays here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Json associative array accessing in jQuery

I am getting response in below format for every product and in a single call there can be many products. I am trying to access this data via jQuery but I'm not able to access it.
Productdata['someid'] = { "Product Json data"}
I am using below syntax in jQuery but not getting the data. Please suggest.
alert(Productdata['someid']);
Its not going as JSON format .
JSON is a key : value pair format ;
so your Productdata should be in below format:
Productdata = { 'someid' : "Product Json data"}
A Json like this
var data={"name":"somebody"};
To call
return data.name
Or
return data["name"]
The problem here is that JavaScript does not support associative arrays (scroll down to "Associative arrays, no way!"). It has some internal workarounds which make it appear as if it does, but really all it does is just adding the keys as properties.
So you would most likely be able to access it using Productdata.someid = ....
EDIT:
So assuming you have the following JSON string: {"id":"123"} (which is valid JSON), you can use it like this:
var jsonString = '{"id":"123"}';
var parsedJSON = $.parseJSON(jsonString);
var productID = "product_" + parsedJSON.id;
Does this help?
Some useful links: JSON format checker to make sure the JSON is valid.
Unfortunately I wasn't allowed to add more than 2 links, so the jQuery parseJSON function link is still in the comment below.

Parse External JSON File with jQuery

I have a 2 MB JSON object that I'm hoping to parse with jQuery. I dumped the whole object into a file named "timeline.js" and I'm hoping to parse through it as a dataset to grab records as needed.
My dataset started as an XML file, but I read that JSON would be more efficient as I'm using jQuery to pull the data and place it in the DOM.
Below is the first record of my object. How would I parse this object to grab the record with a 'profileid' of 1016?
{
timeline:{
record:[
{
profileid:1016,
title:'Adam',
parentprofileid:0,
type:'Person',
minzoomlevel:29,
maxzoomlevel:66,
isapproxstart:1,
isapproxend:1,
startdate:-4181,
enddate:-3251,
shortdescription:'Name means "red" or "man" he is...',
article:'<div><span>The first member of...',
status:22,
scriptures:{
scripture:[
{
profileid:1016,
scripturetext:'Genesis 2:7',
referencetext:'Birth'
},
{
profileid:1016,
scripturetext:'Genesis 5:4',
referencetext:'Death'
}
]
}
},
jQuery parseJSON works fine, however is unnecessary when using jQuery AJAX and set the dataType to JSON (it is already parsed by jQuery after receiving the data).
However, I guess your actual question is how to find the record with the profileid of e.g. 1016. Since all items are in an array, the only way to find it, is to loop the array and check what profileId is set for the current item. For example:
for(var i in items){
if(items[i].profileid == 1016){
//execute whatever you want to do.
}
}
Make use of :jQuery.parseJSON( json ) - Takes a well-formed JSON string and returns the resulting JavaScript object.
Example
var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

Categories

Resources