Google Visualization style roles in object-literal DataTable - javascript

I am creating Google Visualizations (currently ColumnCharts) from AJAX data, by returning an object literal suitable for passing to the DataTable constructor according to these docs:
https://developers.google.com/chart/interactive/docs/reference#dataparam
Data is all working, but I can't work out how to get the style roles working. I thought the documentation was saying to create the below, but it doesn't set the colors; what am I doing wrong?
{
"cols":[
{
"id":"x",
"type":"number",
"label":"Period"
},
{
"id":"quotes",
"type":"number",
"label":"Quotes"
},
{
"id":"quotes_style",
"type":"number",
"p":{
"role":"style"
}
},
{
"id":"orders",
"type":"number",
"label":"Converted to Orders"
},
{
"id":"orders_style",
"type":"number",
"p":{
"role":"style"
}
}
],
"rows":[
{
"c":[
{
"v":0,
"f":"2/2015"
},
{
"v":4
},
{
"p":{
"style":"color:#dddddd"
}
},
{
"v":3
},
{
"p":{
"style":"color:#00d67c"
}
}
],
"p":null
}
],
"p":null
}

Okay, fixed it; first of all in cols the type of the column should be string; then in the rows, the style columns should just have v set to the style string, not p.
(Found this by calling google.visualization.arrayToDataTable() on a working array in the help documentation and calling .toJSON() on the result - very handy!)
Final JSON:
{
"cols":[
{
"id":"x",
"type":"number",
"label":"Period"
},
{
"id":"quotes",
"type":"number",
"label":"Quotes"
},
{
"id":"quotes_style",
"type":"string",
"p":{
"role":"style"
}
},
{
"id":"orders",
"type":"number",
"label":"Converted to Orders"
},
{
"id":"orders_style",
"type":"string",
"p":{
"role":"style"
}
}
],
"rows":[
{
"c":[
{
"v":0,
"f":"2/2015"
},
{
"v":4
},
{
"v":"color:#dddddd"
},
{
"v":3
},
{
"v":"color:#00d67c"
}
],
"p":null
}
],
"p":null
}

Related

Is there a way to get ProvisioningArtifactParameters in order specified in the CloudFormation template?

I am using AWS API(version 2015-12-10, javascript) to fetch Service Catalog products. describeProvisioningParameters method returns an object with ProvisioningArtifactParameters field which contains parameters, but these parameters sorted in a way different from the way provided in the CloudFormation template.
Parameters part of CloudFormation template:
{
"Parameters": {
"DBInputCIDR": {
},
"DBName": {
},
"DBPortNumber": {
},
"DBMasterUsername": {
},
"DBMasterUserPassword": {
},
"DBBackupRetentionPeriod": {
},
"DBAllocatedStorage": {
},
"DBEngineVersion": {
},
"DBInstanceClass": {
},
"MultiAZ": {
}
}
}
Parameters part of API response:
{
"ProvisioningArtifactParameters": [
{
"ParameterKey": "DBBackupRetentionPeriod"
},
{
"ParameterKey": "DBEngineVersion"
},
{
"ParameterKey": "DBPortNumber"
},
{
"ParameterKey": "DBAllocatedStorage"
},
{
"ParameterKey": "DBMasterUserPassword"
},
{
"ParameterKey": "DBInstanceClass"
},
{
"ParameterKey": "DBInputCIDR"
},
{
"ParameterKey": "DBMasterUsername"
},
{
"ParameterKey": "DBName"
},
{
"ParameterKey": "MultiAZ"
}
]
}
Is there any way to sort API response as specified in CloudFormation template?

Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)

I need some help with this error:
Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse ()
at Object.success (dashboard.js:22)
at fire (jquery-3.3.1.js:3268)
at Object.fireWith [as resolveWith] (jquery-3.3.1.js:3398)
at done (jquery-3.3.1.js:9305)
at XMLHttpRequest. (jquery-3.3.1.js:9548)
I try to transform a string to a json object using JSON.parse() that cause that error.
I am using oracleJet and this is my code:
function DashboardViewModel() {
var self = this;
self.lineTypeValue = ko.observable('curved');
var scatterSeries = [];
$.getJSON( "http://localhost:8080/points", function (data) {
console.info(data);
var ch = '{"name":"graphe1","items":'+JSON.stringify(data.results[1])+ '}';
console.info(ch);
console.info(JSON.parse(scatterSeries));
scatterSeries.push(JSON.parse(ch));
});
/* chart data */
this.scatterSeriesValue = ko.observableArray(scatterSeries);
self.lineTypeOptions = [
{id: 'straight', label: 'straight'},
{id: 'curved', label: 'curved'},
{id: 'stepped', label: 'stepped'},
{id: 'segmented', label: 'segmented'},
{id: 'none', label: 'none'}
];
}
The Json that I get from "http://localhost:8080/points" look like this:
{ "results":[
[
{
"b":"0.110547334",
"cost":"0.000000",
"w":"1.998889"
}
],
[
{
"x":0,
"y":0
},
{
"x":1,
"y":2
},
{
"x":2,
"y":4
},
{
"x":3,
"y":6
},
{
"x":4,
"y":8
},
{
"x":5,
"y":10
},
{
"x":6,
"y":12
},
{
"x":7,
"y":14
},
{
"x":8,
"y":16
},
{
"x":9,
"y":18
},
{
"x":10,
"y":20
},
{
"x":11,
"y":22
},
{
"x":12,
"y":24
},
{
"x":13,
"y":26
},
{
"x":14,
"y":28
},
{
"x":15,
"y":30
},
{
"x":16,
"y":32
},
{
"x":17,
"y":34
},
{
"x":18,
"y":36
},
{
"x":19,
"y":38
},
{
"x":20,
"y":40
},
{
"x":21,
"y":42
},
{
"x":22,
"y":44
},
{
"x":23,
"y":46
},
{
"x":24,
"y":48
},
{
"x":25,
"y":50
},
{
"x":26,
"y":52
},
{
"x":27,
"y":54
},
{
"x":28,
"y":56
},
{
"x":29,
"y":58
},
{
"x":30,
"y":60
},
{
"x":31,
"y":62
},
{
"x":32,
"y":64
},
{
"x":33,
"y":66
},
{
"x":34,
"y":68
},
{
"x":35,
"y":70
},
{
"x":36,
"y":72
},
{
"x":37,
"y":74
},
{
"x":38,
"y":76
},
{
"x":39,
"y":78
},
{
"x":40,
"y":80
},
{
"x":41,
"y":82
},
{
"x":42,
"y":84
},
{
"x":43,
"y":86
},
{
"x":44,
"y":88
},
{
"x":45,
"y":90
},
{
"x":46,
"y":92
},
{
"x":47,
"y":94
},
{
"x":48,
"y":96
},
{
"x":49,
"y":98
},
{
"x":50,
"y":100
},
{
"x":51,
"y":102
},
{
"x":52,
"y":104
},
{
"x":53,
"y":106
},
{
"x":54,
"y":108
},
{
"x":55,
"y":110
},
{
"x":56,
"y":112
},
{
"x":57,
"y":114
},
{
"x":58,
"y":116
},
{
"x":59,
"y":118
},
{
"x":60,
"y":120
},
{
"x":61,
"y":122
},
{
"x":62,
"y":124
},
{
"x":63,
"y":126
},
{
"x":64,
"y":128
},
{
"x":65,
"y":130
},
{
"x":66,
"y":132
},
{
"x":67,
"y":134
},
{
"x":68,
"y":136
},
{
"x":69,
"y":138
},
{
"x":70,
"y":140
},
{
"x":71,
"y":142
},
{
"x":72,
"y":144
},
{
"x":73,
"y":146
},
{
"x":74,
"y":148
},
{
"x":75,
"y":150
},
{
"x":76,
"y":152
},
{
"x":77,
"y":154
},
{
"x":78,
"y":156
},
{
"x":79,
"y":158
},
{
"x":80,
"y":160
},
{
"x":81,
"y":162
},
{
"x":82,
"y":164
},
{
"x":83,
"y":166
},
{
"x":84,
"y":168
},
{
"x":85,
"y":170
},
{
"x":86,
"y":172
},
{
"x":87,
"y":174
},
{
"x":88,
"y":176
},
{
"x":89,
"y":178
},
{
"x":90,
"y":180
},
{
"x":91,
"y":182
},
{
"x":92,
"y":184
},
{
"x":93,
"y":186
},
{
"x":94,
"y":188
},
{
"x":95,
"y":190
},
{
"x":96,
"y":192
},
{
"x":97,
"y":194
},
{
"x":98,
"y":196
},
{
"x":99,
"y":198
}
]]}
and what I want the variable scatterSeries to hold is a table like this one:
[ {
name:"graphe1",
items:[
{
x:8,
y:2
},
{
x:15,
y:15
},
{
x:25,
y:26
},
{
x:33,
y:22
},
{
x:36,
y:40
}
]},]
what I get in the console about the string ch is this:
{"name":"graphe1","items":[{"x":0,"y":0},{"x":1,"y":2},{"x":2,"y":4},{"x":3,"y":6},{"x":4,"y":8},{"x":5,"y":10},{"x":6,"y":12},{"x":7,"y":14},{"x":8,"y":16},{"x":9,"y":18},{"x":10,"y":20},{"x":11,"y":22},{"x":12,"y":24},{"x":13,"y":26},{"x":14,"y":28},{"x":15,"y":30},{"x":16,"y":32},{"x":17,"y":34},{"x":18,"y":36},{"x":19,"y":38},{"x":20,"y":40},{"x":21,"y":42},{"x":22,"y":44},{"x":23,"y":46},{"x":24,"y":48},{"x":25,"y":50},{"x":26,"y":52},{"x":27,"y":54},{"x":28,"y":56},{"x":29,"y":58},{"x":30,"y":60},{"x":31,"y":62},{"x":32,"y":64},{"x":33,"y":66},{"x":34,"y":68},{"x":35,"y":70},{"x":36,"y":72},{"x":37,"y":74},{"x":38,"y":76},{"x":39,"y":78},{"x":40,"y":80},{"x":41,"y":82},{"x":42,"y":84},{"x":43,"y":86},{"x":44,"y":88},{"x":45,"y":90},{"x":46,"y":92},{"x":47,"y":94},{"x":48,"y":96},{"x":49,"y":98},{"x":50,"y":100},{"x":51,"y":102},{"x":52,"y":104},{"x":53,"y":106},{"x":54,"y":108},{"x":55,"y":110},{"x":56,"y":112},{"x":57,"y":114},{"x":58,"y":116},{"x":59,"y":118},{"x":60,"y":120},{"x":61,"y":122},{"x":62,"y":124},{"x":63,"y":126},{"x":64,"y":128},{"x":65,"y":130},{"x":66,"y":132},{"x":67,"y":134},{"x":68,"y":136},{"x":69,"y":138},{"x":70,"y":140},{"x":71,"y":142},{"x":72,"y":144},{"x":73,"y":146},{"x":74,"y":148},{"x":75,"y":150},{"x":76,"y":152},{"x":77,"y":154},{"x":78,"y":156},{"x":79,"y":158},{"x":80,"y":160},{"x":81,"y":162},{"x":82,"y":164},{"x":83,"y":166},{"x":84,"y":168},{"x":85,"y":170},{"x":86,"y":172},{"x":87,"y":174},{"x":88,"y":176},{"x":89,"y":178},{"x":90,"y":180},{"x":91,"y":182},{"x":92,"y":184},{"x":93,"y":186},{"x":94,"y":188},{"x":95,"y":190},{"x":96,"y":192},{"x":97,"y":194},{"x":98,"y":196},{"x":99,"y":198}]}
Any help please?!! :( :(
You are calling:
JSON.parse(scatterSeries)
But when you defined scatterSeries, you said:
var scatterSeries = [];
When you try to parse it as JSON it is converted to a string (""), which is empty, so you reach the end of the string before having any of the possible content of a JSON text.
scatterSeries is not JSON. Do not try to parse it as JSON.
data is not JSON either (getJSON will parse it as JSON automatically).
ch is JSON … but shouldn't be. You should just create a plain object in the first place:
var ch = {
"name": "graphe1",
"items": data.results[1]
};
scatterSeries.push(ch);
In short, for what you are doing, you shouldn't have JSON.parse anywhere in your code. The only place it should be is in the jQuery library itself.
I was trying to debug this same error (I was writing code in TypeScript), but no matter what I did, the error didn't go away, it was really persistent.
I tried to look into the solution in many blogs and also this SO thread. I tried everything and nothing worked out.
Finally, I saw that I created the required file named products.json in which I gave a single extra newline (meaning, previously the file had 1 empty line, now it had 2 empty lines) and was following good GitHub practices as mentioned here.
Because of that single extra newline in my products.json file, I wasted 2 hours fixing a bug that didn't exist in the first place.
Finally when I deleted the entire products.json file and re-ran the entire thing, I didn't get any runtime errors and everything was running without any problems.
What I learned by going through all this?
It's that experience is the teacher of all things.
Remove this line from your code:
console.info(JSON.parse(scatterSeries));
Issue is with the Json.parse of empty array - scatterSeries , as you doing console log of scatterSeries before pushing ch
var data = { "results":[
[
{
"b":"0.110547334",
"cost":"0.000000",
"w":"1.998889"
}
],
[
{
"x":0,
"y":0
},
{
"x":1,
"y":2
},
{
"x":2,
"y":4
},
{
"x":3,
"y":6
},
{
"x":4,
"y":8
},
{
"x":5,
"y":10
},
{
"x":6,
"y":12
},
{
"x":7,
"y":14
},
{
"x":8,
"y":16
},
{
"x":9,
"y":18
},
{
"x":10,
"y":20
},
{
"x":11,
"y":22
},
{
"x":12,
"y":24
},
{
"x":13,
"y":26
},
{
"x":14,
"y":28
},
{
"x":15,
"y":30
},
{
"x":16,
"y":32
},
{
"x":17,
"y":34
},
{
"x":18,
"y":36
},
{
"x":19,
"y":38
},
{
"x":20,
"y":40
},
{
"x":21,
"y":42
},
{
"x":22,
"y":44
},
{
"x":23,
"y":46
},
{
"x":24,
"y":48
},
{
"x":25,
"y":50
},
{
"x":26,
"y":52
},
{
"x":27,
"y":54
},
{
"x":28,
"y":56
},
{
"x":29,
"y":58
},
{
"x":30,
"y":60
},
{
"x":31,
"y":62
},
{
"x":32,
"y":64
},
{
"x":33,
"y":66
},
{
"x":34,
"y":68
},
{
"x":35,
"y":70
},
{
"x":36,
"y":72
},
{
"x":37,
"y":74
},
{
"x":38,
"y":76
},
{
"x":39,
"y":78
},
{
"x":40,
"y":80
},
{
"x":41,
"y":82
},
{
"x":42,
"y":84
},
{
"x":43,
"y":86
},
{
"x":44,
"y":88
},
{
"x":45,
"y":90
},
{
"x":46,
"y":92
},
{
"x":47,
"y":94
},
{
"x":48,
"y":96
},
{
"x":49,
"y":98
},
{
"x":50,
"y":100
},
{
"x":51,
"y":102
},
{
"x":52,
"y":104
},
{
"x":53,
"y":106
},
{
"x":54,
"y":108
},
{
"x":55,
"y":110
},
{
"x":56,
"y":112
},
{
"x":57,
"y":114
},
{
"x":58,
"y":116
},
{
"x":59,
"y":118
},
{
"x":60,
"y":120
},
{
"x":61,
"y":122
},
{
"x":62,
"y":124
},
{
"x":63,
"y":126
},
{
"x":64,
"y":128
},
{
"x":65,
"y":130
},
{
"x":66,
"y":132
},
{
"x":67,
"y":134
},
{
"x":68,
"y":136
},
{
"x":69,
"y":138
},
{
"x":70,
"y":140
},
{
"x":71,
"y":142
},
{
"x":72,
"y":144
},
{
"x":73,
"y":146
},
{
"x":74,
"y":148
},
{
"x":75,
"y":150
},
{
"x":76,
"y":152
},
{
"x":77,
"y":154
},
{
"x":78,
"y":156
},
{
"x":79,
"y":158
},
{
"x":80,
"y":160
},
{
"x":81,
"y":162
},
{
"x":82,
"y":164
},
{
"x":83,
"y":166
},
{
"x":84,
"y":168
},
{
"x":85,
"y":170
},
{
"x":86,
"y":172
},
{
"x":87,
"y":174
},
{
"x":88,
"y":176
},
{
"x":89,
"y":178
},
{
"x":90,
"y":180
},
{
"x":91,
"y":182
},
{
"x":92,
"y":184
},
{
"x":93,
"y":186
},
{
"x":94,
"y":188
},
{
"x":95,
"y":190
},
{
"x":96,
"y":192
},
{
"x":97,
"y":194
},
{
"x":98,
"y":196
},
{
"x":99,
"y":198
}
]]};
var scatterSeries = [];
var ch = '{"name":"graphe1","items":'+JSON.stringify(data.results[1])+ '}';
console.info(ch);
scatterSeries.push(JSON.parse(ch));
console.info(scatterSeries);
code sample - https://codepen.io/nagasai/pen/GGzZVB
You define var scatterSeries = [];, and then try to parse it as a json string at console.info(JSON.parse(scatterSeries)); which obviously fails. The variable is converted to an empty string, which causes an "unexpected end of input" error when trying to parse it.
Encountered this error in the following function when I tried to pull 'nothing' from LS
Quick fix for posterity
const getUserFromLocalStorage = () => {
try {
return JSON.parse(localStorage.getItem('user') || '');
} catch (error) {
return null;
}
};
In my case, using .NET 6, the problem was that the controller was declared as void and did not return any value.
Simply changing the method declaration to int and adding return 0; solved this issue for me.
[HttpPost]
public int MyMethod(string text)
{
_logger.Log(text);
return 0;
}
This is because of the data that you are trying to parse is not in a parsable format .
so i suggest to check the data is parsable or not before define the parse.
const IsParsable = (data) => {
try {
JSON.parse(data);
} catch (e) {
return false;
}
return true;
}
var Obj= JSON.stringify({
"name": "graphe1",
"items": data.results[1]
})
return IsParsable (Obj) ? JSON.parse(Obj) : false

How to parse Json and used nested loops

I have 2 sets of JSON data but I'm not sure how to use javascript and parse the information.
API1, data stored in variable called response1:
[{"placeid":1,"place_name":"arora-square","city":"miami","state":"florida","country":"US"}, {"placeid":2,"place_name":"hoover","city":"palo-alto","state":"california","country":"US"}
API2, data stored in variable called response2:
[
{
"manufacturer":"apple",
"market_name":"apple_market1",
"codename":"xx",
"model":"iphone",
"usage_statistics":{
"session_infos":[
{
"building_id":17,
"purchases":[
{
"item_id":9,
"item_category_id":9,
"cost":100
},
{
"item_id":81,
"item_category_id":6,
"cost":300
},
{
"item_id":197,
"item_category_id":2,
"cost":4
}
]
},
{
"building_id":18,
"purchases":[
{
"item_id":94,
"item_category_id":4,
"cost":150
},
{
"item_id":126,
"item_category_id":6,
"cost":400
},
{
"item_id":180,
"item_category_id":0,
"cost":300
}
]
},
{
"building_id":16,
"purchases":[
{
"item_id":78,
"item_category_id":3,
"cost":30
},
{
"item_id":30,
"item_category_id":0,
"cost":101.77
},
{
"item_id":103,
"item_category_id":13,
"cost":52.92
},
{
"item_id":25,
"item_category_id":10,
"cost":106.87
},
{
"item_id":161,
"item_category_id":11,
"cost":145.23
},
{
"item_id":70,
"item_category_id":10,
"cost":19.32
},
{
"item_id":113,
"item_category_id":8,
"cost":136.96
},
{
"item_id":43,
"item_category_id":13,
"cost":115.86
},
{
"item_id":59,
"item_category_id":14,
"cost":114.13
},
{
"item_id":171,
"item_category_id":6,
"cost":46.61
}
]
},
{
"building_id":16,
"purchases":[
{
"item_id":39,
"item_category_id":9,
"cost":128.04
},
{
"item_id":167,
"item_category_id":2,
"cost":65.36
},
{
"item_id":190,
"item_category_id":10,
"cost":79.08
},
{
"item_id":83,
"item_category_id":8,
"cost":116.38
},
{
"item_id":153,
"item_category_id":3,
"cost":125.87
},
{
"item_id":43,
"item_category_id":13,
"cost":115.86
},
{
"item_id":114,
"item_category_id":9,
"cost":112.14
},
{
"item_id":31,
"item_category_id":1,
"cost":136.3
},
{
"item_id":8,
"item_category_id":8,
"cost":110.61
},
{
"item_id":4,
"item_category_id":4,
"cost":80.13
}
]
},
{
"building_id":17,
"purchases":[
{
"item_id":117,
"item_category_id":12,
"cost":138.33
},
{
"item_id":3,
"item_category_id":3,
"cost":82.21
},
{
"item_id":6,
"item_category_id":6,
"cost":51.55
},
{
"item_id":139,
"item_category_id":4,
"cost":132.47
}
]
}
]
}
},
{
"manufacturer":"Samsung",
"market_name":"Galaxy S7 Edge",
"codename":"hero2lte",
"model":"SM-G935F",
"usage_statistics":{
"session_infos":[
{
"building_id":17,
"purchases":[
{
"item_id":137,
"item_category_id":2,
"cost":112.87
},
{
"item_id":53,
"item_category_id":8,
"cost":140.45
},
{
"item_id":21,
"item_category_id":6,
"cost":126.53
},
{
"item_id":177,
"item_category_id":12,
"cost":130.49
},
{
"item_id":41,
"item_category_id":11,
"cost":48.16
},
{
"item_id":130,
"item_category_id":10,
"cost":114.39
},
{
"item_id":66,
"item_category_id":6,
"cost":90.03
},
{
"item_id":192,
"item_category_id":12,
"cost":74.78
},
{
"item_id":27,
"item_category_id":12,
"cost":131.27
},
{
"item_id":182,
"item_category_id":2,
"cost":23.37
}
]
},
{
"building_id":18,
"purchases":[
{
"item_id":82,
"item_category_id":7,
"cost":127.05
},
{
"item_id":143,
"item_category_id":8,
"cost":106.03
},
{
"item_id":86,
"item_category_id":11,
"cost":89.86
}
]
},
{
"building_id":18,
"purchases":[
{
"item_id":57,
"item_category_id":12,
"cost":112.88
},
{
"item_id":10,
"item_category_id":10,
"cost":143.22
},
{
"item_id":171,
"item_category_id":6,
"cost":46.61
},
{
"item_id":136,
"item_category_id":1,
"cost":74.89
},
{
"item_id":115,
"item_category_id":10,
"cost":134.47
},
{
"item_id":160,
"item_category_id":10,
"cost":109.55
},
{
"item_id":70,
"item_category_id":10,
"cost":19.32
},
{
"item_id":26,
"item_category_id":11,
"cost":28.86
},
{
"item_id":8,
"item_category_id":8,
"cost":110.61
},
{
"item_id":43,
"item_category_id":13,
"cost":115.86
}
]
},
{
"building_id":17,
"purchases":[
{
"item_id":102,
"item_category_id":12,
"cost":117.67
},
{
"item_id":136,
"item_category_id":1,
"cost":74.89
},
{
"item_id":167,
"item_category_id":2,
"cost":65.36
},
{
"item_id":173,
"item_category_id":8,
"cost":143.73
},
{
"item_id":125,
"item_category_id":5,
"cost":113.33
},
{
"item_id":140,
"item_category_id":5,
"cost":30.33
}
]
},
{
"building_id":16,
"purchases":[
{
"item_id":149,
"item_category_id":14,
"cost":106.59
},
{
"item_id":152,
"item_category_id":2,
"cost":123.09
},
{
"item_id":87,
"item_category_id":12,
"cost":1.46
},
{
"item_id":49,
"item_category_id":4,
"cost":50.82
},
{
"item_id":86,
"item_category_id":11,
"cost":89.86
},
{
"item_id":48,
"item_category_id":3,
"cost":103.54
},
{
"item_id":10,
"item_category_id":10,
"cost":143.22
},
{
"item_id":162,
"item_category_id":12,
"cost":21.48
},
{
"item_id":11,
"item_category_id":11,
"cost":70.23
},
{
"item_id":118,
"item_category_id":13,
"cost":19.45
}
]
},
{
"building_id":16,
"purchases":[
{
"item_id":20,
"item_category_id":5,
"cost":133.59
},
{
"item_id":82,
"item_category_id":7,
"cost":127.05
},
{
"item_id":111,
"item_category_id":6,
"cost":60.91
}
]
},
{
"building_id":17,
"purchases":[
{
"item_id":22,
"item_category_id":7,
"cost":56.14
},
{
"item_id":91,
"item_category_id":1,
"cost":22.52
},
{
"item_id":149,
"item_category_id":14,
"cost":106.59
}
]
},
{
"building_id":18,
"purchases":[
{
"item_id":168,
"item_category_id":3,
"cost":54.33
},
{
"item_id":22,
"item_category_id":7,
"cost":56.14
}
]
},
{
"building_id":17,
"purchases":[
{
"item_id":79,
"item_category_id":4,
"cost":53.53
},
{
"item_id":60,
"item_category_id":0,
"cost":117.82
},
{
"item_id":72,
"item_category_id":12,
"cost":148.69
},
{
"item_id":107,
"item_category_id":2,
"cost":48.71
},
{
"item_id":18,
"item_category_id":3,
"cost":142.67
},
{
"item_id":145,
"item_category_id":10,
"cost":127.1
},
{
"item_id":139,
"item_category_id":4,
"cost":132.47
}
]
}
]
}
}
]
need to figure out for example : total cost in the country( US) and which building ( name or id) has most total cost.
First thing I'd do is beautify that ugly JSON to a more human-friendly form, obtaining something like this:
[
{
"placeid": 1,
"place_name": "arora-square",
"city": "miami",
"state": "florida",
"country": "US"
},
{
"placeid": 2,
"place_name": "hoover",
"city": "palo-alto",
"state": "california",
"country": "US"
}
]
Once you're done with that, just remember that {} surrounds an object, while [] represents a list. In the first case, to access a specific property just use object.property, while in the second one you can access the i-th element using list[index].
For example, if you have o = {'key1': 'value1', 'key2': 'value2'}, you can easily retrieve value1 using v1 = o.key1.
Without using JSON.parse(), your JSON is basically an object and can be directly accessed from the array using the foreach method
var response2 = [{"placeid":1,"place_name":"arora-square","city":"miami","state":"florida","country":"US"}, {"placeid":2,"place_name":"hoover","city":"palo-alto","state":"california","country":"US"}]
response2.forEach(function(element) {
console.log(element.placeid);
console.log(element.place_name);
console.log(element.city);
console.log(element.state);
console.log("---");
});
To use JSON.parse(), you need to convent your JSON into a string and then assign it to the array. After that, use foreach method to simply loop through the array content and parse each JSON content.
var response1 = ["{\"placeid\":\"1\",\"place_name\":\"arora-square\",\"city\":\"miami\",\"state\":\"florida\",\"country\":\"US\"}", "{\"placeid\": \"2\",\"place_name\":\"hoover\",\"city\":\"palo-alto\",\"state\":\"california\",\"country\":\"US\"}"];
response1.forEach(function(element) {
element = JSON.parse(element);
console.log(element.placeid);
console.log(element.place_name);
console.log(element.city);
console.log(element.state);
console.log("---");
});
The second method will be applicable in cases like AJAX calls, where you are receiving back JSON encoded data.

Elasticsearch completion by url

Using nodejs elasticsearch api I am trying to get completion by url. The problem is when I am trying to define the way the autocompletion should work, it splits my url in several chuncks, not the whole url:
E,g: URL=https://www.edx.org/course/introduction-mongodb-using-mean-stack-mongodbx-m101x-0
This is the index that I created:
elasticClient.index({
index: indexName,
type: 'mooc',
body: {
name: document.name,
url: document.url,
platform: document.platform,
idMooc: document.idMooc,
platformId: document.platformId,
duration: document.duration,
points: document.points,
tags: document.tags,
dirty: document.dirty,
area: document.area,
description: document.description,
suggest: {
input: document.name.split(' '),
output: document.name,
payload: document || {}
},
urlsuggest:{
input: document.url,
output: document.url,
payload: document || {}
}
}
})
This is my search:
elasticClient.search({
index: indexName,
type: 'mooc',
body: {
query: {
filtered: {
query: {
match: {
// match the query agains all of
// the fields in the posts index
_all: input
}
},
filter: {
// only return documents that the flag dirty is false
term: { dirty: false }
}
}
},
suggest: {
urlsuggest: {
text: input,
term: {
field: 'urlsuggest'
}
}
}
}
})
I got the following urlsuggest:
"suggest":{
"urlsuggest":[
{
"text":"https",
"offset":0,
"length":5,
"options":[
]
},
{
"text":"www",
"offset":8,
"length":3,
"options":[
]
},
{
"text":"edx",
"offset":12,
"length":3,
"options":[
]
},
{
"text":"org",
"offset":16,
"length":3,
"options":[
]
},
{
"text":"course",
"offset":20,
"length":6,
"options":[
]
},
{
"text":"introduction",
"offset":27,
"length":12,
"options":[
]
},
{
"text":"mongodb",
"offset":40,
"length":7,
"options":[
]
},
{
"text":"using",
"offset":48,
"length":5,
"options":[
]
},
{
"text":"mean",
"offset":54,
"length":4,
"options":[
]
},
{
"text":"stack",
"offset":59,
"length":5,
"options":[
]
},
{
"text":"mongodbx",
"offset":65,
"length":8,
"options":[
]
},
{
"text":"m",
"offset":74,
"length":1,
"options":[
]
},
{
"text":"x",
"offset":78,
"length":1,
"options":[
]
}
]
}
Which, for some reason splits my url in many inputs. Apart from that is not finding properly since it does not find any match and my elasticsearch contains that field and was properly populated.

Google column chart - column values don't match axis

I have a column chart as follows:
I've added the values of the data, and as you can see the columns don't make sense in relation to the Y axis - why is this?
The chart options:
var options = {
title: 'Task Estimates',
backgroundColor: { fill: 'transparent' },
chartArea: { width: '50%' },
height: '400px',
hAxis: { direction: -1, slantedText: true, slantedTextAngle: 45, title: 'Tasks' },
};
var url = "http://theurl";
$.getJSON(url, function (jsonChartData) {
var data = new google.visualization.DataTable(jsonChartData);
var chart = new google.visualization.ColumnChart(chartHtmlElement);
chart.draw(data, options);
});
The json is:
{
"cols":[
{ "id":"A", "label":"Task", "type":"string", "role":null },
{ "id":"B", "label":"Days Estimated", "type":"number", "role":null },
{ "id":"C", "label":"Days Actual", "type":"number", "role":null }
],
"rows":[
{ "c":[ { "v":"Task 3" }, { "v":"7" }, { "v":"4" } ] },
{ "c":[ { "v":"Task 1" }, { "v":"10" }, { "v":"24" } ] },
{ "c":[ { "v":"Task 2" }, { "v":"100" }, { "v":"0" } ] }
],
"p":null
}
What am I doing wrong? Is there something else I need to specify to make the axis work correctly?
JSFIDDLE
http://jsfiddle.net/6K3Wc/1/
You are entering your numbers as strings, when they should be entered as numbers:
{
"cols":[
{
"id":"A",
"label":"Task",
"type":"string",
"role":null
},
{
"id":"B",
"label":"Days Estimated",
"type":"number",
"role":null
},
{
"id":"C",
"label":"Days Actual",
"type":"number",
"role":null
}
],
"rows":[
{
"c":[
{
"v":"Task 3"
},
{
"v":7
},
{
"v":4
}
]
},
{
"c":[
{
"v":"Task 1"
},
{
"v":10
},
{
"v":24
}
]
},
{
"c":[
{
"v":"Task 2"
},
{
"v":100
},
{
"v":0
}
]
}
],
"p":null
}

Categories