It seems I can't parse my JSON data from Parse into my web app. When I try alert my JSON data, it shows like this
[{
"address1": "Test",
"address2": "Test",
"bathroom": "1",
"bedroom": "1",
"builtUpArea": "123",
"cityId": "1",
"countryId": "1",
"description": "Test",
"exclusive": true,
"facingDirectionId": "1",
"floorlevel": "1",
"furnishTypeId": "1",
"landArea": "123",
"landAreaTypeId": "1",
"name": "Test",
"ownerContact": "Test",
"ownerEmail": "Test",
"ownerIc": "Test",
"ownerName": "Test",
"poscode": "123",
"price": "Test",
"purchaserId": "xZyLAKnCnXt",
"remark": "Test",
"stateId": "1",
"statusId": "1",
"tenureId": "1",
"typeId": "1",
"user": {
"__type": "Pointer",
"className": "_User",
"objectId": "rquoctPnNz"
},
"objectId": "0nfSPUwgvm",
"createdAt": "2015-04-10T02:16:54.509Z",
"updatedAt": "2015-04-10T02:16:54.509Z"
}]
But nothing appear in my view page, only createdAt and updatedAt with "" symbol.
My code:
JavaScript:
var Property = Parse.Object.extend("Property");
var user = Parse.User.current();
var query = new Parse.Query(Property);
query.equalTo("user", user);
query.find({
success: function(data) {
alert(JSON.stringify(data));
$scope.properties = data;
},
error: function(object, error) {
alert(JSON.stringify(error));
}
});
HTML:
<tr ng-repeat="property in properties | filter:query">
<td>{{property.name}}</td>
<td>RM {{property.price}}</td>
<td>{{property.createdAt}}</td>
<td>{{property.updatedAt}}</td>
</tr>
I already found the answer.
$scope.properties = [];
var Property = Parse.Object.extend("Property");
var user = Parse.User.current();
var query = new Parse.Query(Property);
query.equalTo("user", user);
query.find({
success: function(data) {
var index = 0;
var Arrlen = results.length;
for (index = 0; index < Arrlen; ++index) {
var obj = results[index];
$scope.properties.push({
objectId: obj.attributes.objectId,
name: obj.attributes.name,
price: obj.attributes.price,
createdAt: obj.createdAt,
updatedAt: obj.updatedAt
});
}
},
error: function(object, error) {
alert(JSON.stringify(error));
}
});
Related
I have two json array and object
I would like to compare two json and push another object
obj1 = ["user1", "user2"]
obj2 = [
{
"userName": "user1",
"id": "14"
},
{
"userName": "user2",
"id": "9",
},
{
"userName": "user3",
"id": "3",
},
{
"userName": "user4",
"id": "1",
}
]
I would like to get the result as below
[
{
"userName": "user1",
"id": "14"
},
{
"userName": "user2",
"id": "9",
}
]
After, I tried to compare two arrays and get a result what I need.
var obj1 = ["user1","user2"]
var obj2 = [
{
"userName": "user1",
"id": "14"
},
{
"userName": "user2",
"id": "9",
},
{
"userName": "user3",
"id": "3",
},
{
"userName": "user4",
"id": "1",
}
]
var objArray = [];
for (var i = 0; i < obj.length; i++) {
if (obj1[i] === obj2.userName) {
objArray.push(obj2[i]);
}
return objArray;
Please help me to solve the issue.
Thanks in advance
You could filter by checking the name with Array#includes.
var array1 = ["user1", "user2"],
array2 = [{ userName: "user1", id: "14" }, { userName: "user2", id: "9", }, { userName: "user3", id: "3", }, { userName: "user4", id: "1", }],
result = array2.filter(({ userName }) => array1.includes(userName));
console.log(result);
You need two loops:
iterate through obj1[i], and for each index of obj1...
iterate through obj2[] and test each object to see whether the username property's value equals that of obj[i].
var obj1 = ["user1","user2"]
var obj2 = [
{
"userName": "user1",
"id": "14"
},
{
"userName": "user2",
"id": "9",
},
{
"userName": "user3",
"id": "3",
},
{
"userName": "user4",
"id": "1",
}]
function myFunction(){
var objArray = [];
for (var i=0; i < obj1.length; i++) {
for (var userIndex=0; userIndex < obj2.length; userIndex++){
if (obj1[i] === obj2[userIndex].userName){
objArray.push( obj2[userIndex] );
}
}
}
return objArray;
};
console.log(myFunction());
I am creating a Tableau Web Data Connector as per the documentation HERE.
I am running the Simulator and have setup a HTML page (as per tutorial) which calls a Javascript file that runs the Tableau WDC script as below.
(function () {
var myConnector = tableau.makeConnector();
myConnector.init = function(initCallback) {
initCallback();
tableau.submit();
};
myConnector.getSchema = function (schemaCallback) {
var cols = [
{ id : "date_of_work", alias : "Date of Work", dataType: tableau.dataTypeEnum.date },
{ id : "supervisor", alias : "Supervisor", dataType: tableau.dataTypeEnum.string }
];
var tableInfo = {
id : "test",
alias : "test",
columns : cols
};
schemaCallback([tableInfo]);
};
myConnector.getData = function (table, doneCallback) {
$.getJSON("http://myDataCall.php", function(response) {
// ERROR HERE!
var resp = response.job.job_workflows; // Response
var parsedResp = JSON.parse(resp); // Parse the response
var tableData = []; // Temp array
// Iterate over the JSON object
for (var i = 0, len = resp.length; i < len; i++) {
tableData.push({
"date_of_work": parsedResp[i]['job_status'],
"supervisor": parsedResp[i]['job_workflow_1197927'],
});
}
table.appendRows(tableData);
doneCallback();
});
};
tableau.registerConnector(myConnector);
})();
When I run the script I get the error: The WDC reported an error:
Uncaught SyntaxError: Unexpected token o in JSON at position 1 stack:SyntaxError:
Unexpected token o in JSON at position 1 at JSON.parse () at Object.success
The (abbreviated) JSON that is being returned looks as follows:
{
"employee": {
"id": 23940,
},
"company": {
"id": 1059,
},
"job": {
"id": 13712707,
"job_status_logs": [{
"id": 17330391,
}],
"company": {
"id": 1059,
},
"created_by": {
"id": 23940,
},
"job_workflows": [{
"id": 1087689283,
"job_id": 13712707,
"employee_id": null,
"template_workflow_id": 1251218,
"name": "Date of work",
"action": "datepicker",
"optional": 0,
"action_values": "",
"action_value_entered": "2017-10-12",
"nested_workflow_id": 0,
}, {
"id": 1087689284,
"job_id": 13712707,
"employee_id": null,
"template_workflow_id": 1251219,
"name": "Supervisor",
"action": "list",
"optional": 0,
"action_values": "John Doe",
"action_value_entered": "John Doe",
"nested_workflow_id": 0,
}],
"job_fields": [{
"id": 50456098,
}],
"job_status_change_messages": [{
"id": 59957985}],
"job_assets":[]
}
}
I am trying to access the job.job_workflows.action_value_entered value but keep getting the error as above.
How can I fix this error?
There are a couple of issues here.
1) The JSON sent back from your server is invalid. Here is the valid version. I recommend using a site like https://jsonformatter.curiousconcept.com/ to validate your JSON.
{
"employee": {
"id": 23940
},
"company": {
"id": 1059
},
"job": {
"id": 13712707,
"job_status_logs": [{
"id": 17330391
}],
"company": {
"id": 1059
},
"created_by": {
"id": 23940
},
"job_workflows": [{
"id": 1087689283,
"job_id": 13712707,
"employee_id": null,
"template_workflow_id": 1251218,
"name": "Date of work",
"action": "datepicker",
"optional": 0,
"action_values": "",
"action_value_entered": "2017-10-12",
"nested_workflow_id": 0
}, {
"id": 1087689284,
"job_id": 13712707,
"employee_id": null,
"template_workflow_id": 1251219,
"name": "Supervisor",
"action": "list",
"optional": 0,
"action_values": "John Doe",
"action_value_entered": "John Doe",
"nested_workflow_id": 0
}],
"job_fields": [{
"id": 50456098
}],
"job_status_change_messages": [{
"id": 59957985}],
"job_assets":[]
}
}
2) jQuery's getJson method returns an object, so you don't need to parse it. You can just use the resp variable directly like so:
var resp = response.job.job_workflows; // Response
var tableData = []; // Temp array
// Iterate over the JSON object
for (var i = 0, len = resp.length; i < len; i++) {
tableData.push({
"date_of_work": resp[i]['job_status'],
"supervisor": resp[i]['job_workflow_1197927'],
});
}
table.appendRows(tableData);
doneCallback();
Fixing those two issues should unblock you. However, you'll want to think through what data you are sending back. The current values you are sending back do not exist in the JSON (i.e. resp[i]['job_workflow_1197927']).
Instead, you could do something like this: resp[1].job_id, which would give you the job_id of each job_workflow.
I have a json arry
{
"result": "sucess",
"senderids": [{
"id": "2",
"senderid": "powers",
"status": "0",
"type": "2",
"availa": "0",
"user": "admin"
}, {
"id": "3",
"senderid": "powert",
"status": "0",
"type": "2",
"availa": "0",
"user": "admin"
}, {
"id": "4",
"senderid": "powerd",
"status": "0",
"type": "2",
"availa": "0",
"user": "admin"
}, {
"id": "5",
"senderid": "pavank",
"status": "0",
"type": "1",
"user": "pavan"
}]
}
Javascript:
var res = xhr.responseText;
var s = res.senderids;
for (i = 0; i < s.senderids.length; i++) {
var contact = JSON.parse(s.senderids[i].senderid);
alert(contact);
}
How i can parse this json array using JSON.parse. I have tried this code
thanks in advance
Try this:
var res = JSON.parse(xhr.responseText);
var s = res.senderids;
for (i = 0; i < s.senderids.length; i++) {
var contact = s.senderids[i].senderid;
alert(contact);
}
I am trying to format a json object by groupping city name. I am trying to take a separate array and move the items based on found and not found to build a string. I am using javascript/angularjs to achieve this.
My current string
CurrentDataFormat =
[
{"Id":17,"code":"123","cityName":"Los Angeles","startDate":"1/20/2016","endDate":"1/20/2016"},
{"Id":18,"code":"456","cityName":"Chicago ","startDate":"1/22/2016","endDate":"1/25/2016"},
{"Id":19,"code":"789","cityName":"Los Angeles","startDate":"1/13/2016","endDate":"1/21/2016"}
]
I am trying to achieve this format
ExpectedDataFormat =
[{
"name":"Los Angeles",
"CityData":
[
{"Id":"17","code":"123","startDate":"1/20/2016","endDate":"1/20/2016"},
{"Id":"19","code":"789","startDate":"1/13/2016","endDate":"1/21/2016"}
]},{
"name":"Chicago",
"CityData":
[
{"Id":"18","code":"456","startDate":"1/22/2016","endDate":"1/25/2016"},
]}
}
Logic What am I trying (trying with different data though)
var array = [
{ "name": "project1", "url": "picture1-1.jpg"},
{ "name": "project1", "url": "picture1-2.jpg"},
{ "name": "project2", "url": "picture2-1.jpg"},
{ "name": "project3", "url": "picture3-1.jpg"},
{ "name": "project1", "url": "picture1-3.jpg"},
{ "name": "project4", "url": "picture4-1.jpg"},
{ "name": "project3", "url": "picture3-2.jpg"},
{ "name": "project1", "url": "picture1-4.jpg"}
];
var separateArray = [];
$.each(array, function (i, item) {
var foundItem = false;
$.each(separateArray, function (y, newItem) {
if (newItem.name == item.name) {
if (!(newItem.url instanceof Array)) {
newItem.url = [newItem.url];
}
newItem.url.push(item.url);
foundItem = true;
}
});
if (!foundItem) {
separateArray.push(item);
}
});
console.log(separateArray);
Would this code do what you need ?
var newarray = [];
CurrentDataFormat.forEach(function(item){
if(newarray[item.cityName] != undefined){
var key = item.cityName;
delete item.cityName;
newarray[key].CityData.push(item);
}else{
var o = {};
o.name = item.cityName;
o.CityData = [];
o.CityData.push(item);
newarray[o.name] = o;
}
});
console.log(newarray);
Check following code,
CurrentDataFormat = [{
"Id": 17,
"code": "123",
"cityName": "Los Angeles",
"startDate": "1/20/2016",
"endDate": "1/20/2016"
},
{
"Id": 18,
"code": "456",
"cityName": "Chicago",
"startDate": "1/22/2016",
"endDate": "1/25/2016"
},
{
"Id": 19,
"code": "789",
"cityName": "Los Angeles",
"startDate": "1/13/2016",
"endDate": "1/21/2016"
}
]
var refinedArray = {};
for (i = 0; i < CurrentDataFormat.length; i++) {
refinedArray[CurrentDataFormat[i].cityName] = refinedArray[CurrentDataFormat[i].cityName] ? refinedArray[CurrentDataFormat[i].cityName] : {};
refinedArray[CurrentDataFormat[i].cityName].name = CurrentDataFormat[i].cityName;
refinedArray[CurrentDataFormat[i].cityName].CityData = refinedArray[CurrentDataFormat[i].cityName].CityData ? refinedArray[CurrentDataFormat[i].cityName].CityData : [];
refinedArray[CurrentDataFormat[i].cityName].CityData.push({
"Id": CurrentDataFormat[i].Id,
"code": CurrentDataFormat[i].code,
"startDate": CurrentDataFormat[i].startDate,
"endDate": CurrentDataFormat[i].endDate
});
}
var ExpectedDataFormat = [];
for (singleCityName in refinedArray){
ExpectedDataFormat.push({'name' : refinedArray[singleCityName].name, 'CityData' : refinedArray[singleCityName].CityData});
};
ExpectedDataFormat is holding your desired output
Working demo at jsFiddle
How can I get the id of info1 or info2 with each information of the inside loop by jquery loop. Fo example I want to get id 1 from info1 then all the information within id 1 similarly same as from info2. I need the output to show in the browser.
var data = {
"info1": {
"1": {
"clientname": "ruhul yahoo",
"clientemail": "ruhul080#yahoo.com",
"clientusername": "ruhulya"
},
"2": {
"clientname": "kaosar yahoo",
"clientemail": "kaosar080#yahoo.com",
"clientusername": "kaosar"
}
},
"info2": {
"3": {
"id": "24",
"receiver": "5",
"content": "chat system",
"time": "2015-08-19 12:09:19"
},
"4": {
"id": "23",
"receiver": "4",
"content": "chat system",
"time": "2015-08-19 12:09:19"
}
},
}
Thanks in advance.
Iterate the json array and access the object like the following code.
var data = {
"info1": {
"1": {
"clientname": "ruhul yahoo",
"clientemail": "ruhul080#yahoo.com",
"clientusername": "ruhulya"
},
"2": {
"clientname": "kaosar yahoo",
"clientemail": "kaosar080#yahoo.com",
"clientusername": "kaosar"
}
},
"info2": {
"3": {
"id": "24",
"receiver": "5",
"content": "chat system",
"time": "2015-08-19 12:09:19"
},
"4": {
"id": "23",
"receiver": "4",
"content": "chat system",
"time": "2015-08-19 12:09:19"
}
},
};
for(var j in data){
for(var k in data[j]){
console.log(data[j][k]);
}
}
Your browser's Console will log the following objects if you run the above example.
Object {clientname: "ruhul yahoo", clientemail: "ruhul080#yahoo.com", clientusername: "ruhulya"}
Object {clientname: "kaosar yahoo", clientemail: "kaosar080#yahoo.com", clientusername: "kaosar"}
Object {id: "24", receiver: "5", content: "chat system", time: "2015-08-19 12:09:19"}
Object {id: "23", receiver: "4", content: "chat system", time: "2015-08-19 12:09:19"}
Then you can access the values like a normal object console.log(data[j][k].clientname);
This function will find you the first instance of a variable name in the object. If you need to find a variable in a specific path you could amend this function fairly easily to do that. Certainly the function as is passes the test case you've provided.
function findVar(data, varName) {
for (var i in data) {
if (i === varName) return data[i];
if (typeof (data[i]) === 'object') {
var findResult = findVar(data[i], varName)
if (typeof(findResult) !== 'undefined')
{
return findResult;
}
}
}
return undefined;
}
Firstly, This is not a valid JSON, Rmove the last , before last {
Secondly , parse it as a JSON and get the info as
data.info1[1].clientname
var data = JSON.parse('{"info1":{"1":{"clientname":"ruhul yahoo","clientemail":"ruhul080#yahoo.com","clientusername":"ruhulya"},"2":{"clientname":"kaosar yahoo","clientemail":"kaosar080#yahoo.com","clientusername":"kaosar"}},"info2":{"3":{"id":"24","receiver":"5","content":"chat system","time":"2015-08-19 12:09:19"},"4":{"id":"23","receiver":"4","content":"chat system","time":"2015-08-19 12:09:19"}}}');
alert(data.info1[1].clientname);
alert(data.info1[2].clientname);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
You can iterate over this object like this
for(var i in data){
for(var j in data[i]){
console.log(data[i][j]);
}
}