How to get customised json in angular - javascript

I want to create dynamic json from response coming from api?? How can i acheive this?? I want to have json as shown below. But i am getting invalid json. What am i doing wrong??
{
"data":[
{
"dataOverallCount":response.dataOverallCount,
"dataTotalCount":response.dataTodaysCount
}
],
"item":[
{
"itemOverallCount":response.itemOverallCount,
"itemTotalCount":response.itemTotalCount
}
]
}
here response is an object as
{dataOverallCount: 2, dataTodaysCount:0, itemOverallCount:6, itemTotalCount:3}

Related

DataTables not showing data

Hi I have the following data but cannot get DataTables to display it:
const data = [{
"School":"Arsenal 2011",
"Group":{
"Name":"Previous",
"ParentGroup":{
"Name":"Arsenal",
"ParentGroup":{
"Name":"USA",
"ParentGroup":null
}
}
},
"GroupDisplayText":null,
"Publisher":"Abbot",
"PublishedDate":"2011",
"PublishersWebsite":"http://google.com/USA/ADW%202011/Arsenal%202011.pdf"
},
{
"School":"New York 2000",
"Group":{
"Name":"New York",
"ParentGroup":{
"Name":"USA",
"ParentGroup":null
}
},
"GroupDisplayText":null,
"Publisher":"DoE",
"PublishedDate":"2000",
"PublishersWebsite":"http://google.com/USA/New York%202000%20Tables.pdf"
}];
$(document).ready(function () {
$('#example').DataTable( {
"ajax": data
} );
}
I have created a project on playcode
https://playcode.io/470603
I am getting a datatables error
DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
I have checked the json and all is ok?
Thanks
$(document).ready(function () {
$('#example').DataTable( {
data:data // ajax for to make ajax request to retrieve data.
"columns": [ // also needs to specify column config to display it
{ "data": "School" },
]
} );
}
for javascript object use data instead.

How to add a json object to a json array in an external file in javascript / capserjs

I am writing to a json file in casperjs and am trying to add new objects to it.
json file looks like
{ "visited": [
{
"id": "258b5ee8-9538-4480-8109-58afe741dc2f",
"url": "https://................"
},
{
"id": "5304de97-a970-48f2-9d3b-a750bad5416c",
"url": "https://.............."
},
{
"id": "0fc7a072-7e94-46d6-b38c-9c7aedbdaded",
"url": "https://................."
}]}
The code to add to the array is
var data;
if (fs.isFile(FILENAME)) {
data = fs.read(FILENAME);
} else {
data = JSON.stringify({ 'visited': [] });
}
var json = JSON.parse(data);
json.visited.push(visiteddata);
data = JSON.stringify(json, null, '\n');
fs.write(FILENAME, data, "a");
This is starting off by adding an new { "visited" : [ ] } array with first couple of objects, below the existing { "visited" : [ ] } array and subsequently the script breaks because the json array is no longer valid json.
Can anybody point me in the right direction. Thank you in advance.
You have a JSON file containing some data.
You:
Read that data
Modify that data
Append the modified version of that data to the original file
This means the file now has the original data and then, immediately after it, a near identical copy with a little bit added.
You don't need the original. You only need the new version.
You need to write to the file instead of appending to it.
Change the 'a' flag to 'w'.

how to access the information1 data

Am trying to access the data from the following JSON . Am Getting response after hitting the server and i need to access the data present in the information1 field.
{
"Test1":[
{
"id1":0,
"Test2":[
{
"Information1":"info1",
"name":"Testing",
"defnitions":[
{
"displayname":"displayame"
},
{
"displayname2":"displayame2"
}
],
"information2":"info2"
}
],
"information3":"info3",
"information4":"info4"
}
]
}
Cause you are using square brackets, it means you have an array. Consequently, to access to your neccessary object you should use:
var data={
"Test1":[
{
"id1":0,
"Test2":[
{
"Information1":"info1",
"name":"Testing",
"defnitions":[
{
"displayname":"displayame"
},
{
"displayname2":"displayame2"
}
],
"information2":"info2"
}
],
"information3":"info3",
"information4":"info4"
}
]
};
var yourObject=data.Test1[0].Test2[0];
To parse JSON response just use stringify method:
var theWholeObject=JSON.stringify(data);

json response handling from soap wsdl

below is the response from soapWSDL in json.i need to print the pname,pjob .i can able to print "client":"http://xmlns.oracle.com/InternetMobile/AbsManagement/BPELProcessSubList", using alert(result.responseJSON.Envelope.Body.processResponse.client); but cant able to print sublist.pname which displays undefined error
{
"Envelope":{
"Body":{
"processResponse":{
"client":"http:\/\/xmlns.oracle.com\/InternetMobile\/AbsManagement\/BPELProcessSubList",
"subList":[
{
"personid":"30979",
"pjob":"Senior Consultant",
"pname":"Imad El Kustomany"
},
{
"personid":"30980",
"pjob":"Senior Consultant",
"pname":"Abdul Rahman Zaky"
}
],
"xmlns":"http:\/\/xmlns.oracle.com\/InternetMobile\/AbsManagement\/BPELProcessSubList"
}
},
}
Try result.responseJSON.Envelope.Body.processResponse.subList[0].pname and result.responseJSON.Envelope.Body.processResponse.subList[1].pname. subList is an array so you can loop and use index as well
Sublist is an array so you need:
alert(result.responseJSON.Envelope.Body.processResponse.client.subList[0].pname);
or if you want to display pnames of all items
result.responseJSON.Envelope.Body.processResponse.client.subList.forEach(function(el){
alert(el.pname);
});

Error Reading JSON from namespace which contains alphanumeric

i have a JSON js obejct in
res.metrics.load.1-min
the problem is that this is coming from the server.
i cannot extract anything as it gives illegal number
since res.metrics.load.1-min contains 1-min
Any suggestion i can i parse my JSON. my JSON is an Array
"metrics" : {
"load" : {
"1-min" : [
[
5.87,
1437031875
],
[
5.87,
1437031890
]
]}}
Please help i am using
res.metrics.load.1-min = res.metrics.load.1-min.map(
function (map)
{
return { x: map[1], y: map[0] };
});
to map values to x and y. its throwing an error.
You need to update
res.metrics.load.1-min
to
res.metrics.load["1-min"]

Categories