Unable to read json data to draw a graph - javascript

I am new to jQuery and json. Trying to draw a graph with reference to the demo from
http://www.highcharts.com/stock/demo/
This graph uses data from a json file
http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?
this file directly started with data. But the json file that I want to use looks something like this
{"name": {
"text1": "on",
"data": {
[1147651200000,67.79],
[1147737600000,64.98],
[1147824000000,65.26],
[1147910400000,63.18],
[1147996800000,64.51],
[1148256000000,63.38],
[1148342400000,63.15],
[1148428800000,63.34],
[1148515200000,64.33],
[1148601600000,63.55],
[1148947200000,61.22],
[1149033600000,59.77],
}
}}
could someone help me with this please

Your json is invalid.
If your data property is an array, so you should use [] to denote the array.
I'm assuming that array is an array of arrays, so inside [], it should have multiple []s.
Like:
[[x1,y1],[x2,y2]...]
Try formatting your code sample as follows...
{
"name": {
"text1": "on",
"data": [
[
1147651200000,
67.79
],
[
1147651200000,
67.79
]
]
}
}
Use something like http://jsonlint.com/ to check your json's validity.

Related

How to link two json array data's with Google apps script

Using a UrlFetchapp request, I retrieve data in json format in a sheet sheet, for non-table objects the processing is not that hard, but when it comes to tables it's a little less Easy,
I retrieve and display the label content in 4 columns in my sheets sheet by browsing the array object "feezeInformationList" then "feezeTypeCode" then "label", outside the "feezeInformationList" object we find additional data to the label, which is present in another array object, which is "glcfeezeInclusionInformationList", the complementary data is boolean and it is "glcfeezeIsIncludedInPrice" next to each column label, I would like to retrieve the true or false status of each label using maybe as input , the code attribute, here for example "2282".
"data": [
{
"glcfeezeInclusionInformationList": [
{
"feezeTypeCode": {
"code": "2282",
"description": "",
"label": "MMG"
},
"glcfeezeIsIncludedInPrice": true
}
feezeInformationList [
"feezeTypeCode": {
"code": "2282",
"description": "",
"label": "MMG"
},
]
Any help would be appreciated! thanks

Delete Nested Reference in Firestore

I am trying to delete a 2 levels deep nested Reference in Firestore. My Schema looks like this:
In Code it looks like this:
{
"folder": "bla",
"title": "myTitle",
"children": [
{
"ref": "firstReference"
},
{
"ref": "secondReference"
},
{
"title": "Subcollection Title",
"children": [
{
"ref": "thirdReference"
},
{
"ref": "forthReference"
}
]
}
]
}
Now i am searching a way to remove the third or forth Reference from the second children array.
To remove an item from the first children array is use this code:
docRef.update({children: firebase.firestore.FieldValue.arrayRemove(folder.children[index])
But this solution works only for the top level ;(
Does somebody know how to remove deeper Nested elements?
I tried:
docRef.update({[`children[${index}].children`]: firebase.firestore.FieldValue.arrayRemove(
folder.children[index].children[secondIndex])});
But it throws an Error (Paths must not contain '~', '*', '/', '[', or ']')
Thanks for your help ;)
Firestore does not support modifying array items by index. FieldValue.arrayRemove only works if you pass the exact contents of the data to remove from an array field. If you only know the index, then what you'll have to do is read the document, modify the array in memory, then write the new document contents back.

trying to get value in JSON string from amount

I've got a JSON string and I can't seem to extract the value from the amount attribute
var jsonString =[{"id":null,"recordtype":null,"columns":{"amount":1049.849}}]
I've tried accessing amount using:
jsonString[0].columns[0].amount
and also tried using:
jsonString.columns.amount
but all seem to generate an error.
It is
jsonString[0].columns.amount
Because the value under "columns" is immediately an object, not an array.
jsonString[0].columns.amount
is the way to go. Columns is not an array
Try this,
jsonString[0].columns.amount;
You have an array, then just objects. So the answer is jsonString[0].columns.amount
It's useful to indent it, if in doubt:
[{
"id": null,
"recordtype": null,
"columns": {
"amount": 1049.849
}
}]
You have to create the JSON object like
var jsonString =[
{
"id":null,
"recordtype":null,
columns:
{
"amount":1049.849
}
}
]
Then you can access with jsonString[0]['columns']['amount'];
Here is the jfiddle link,
https://jsfiddle.net/hhLahiru/n0de3Lc8/
i can do on console
var jsonString =[{"id":null,"recordtype":null,"columns":{"amount":1049.849}}];
jsonString[0]
Object {id: null, recordtype: null, columns: Object}
jsonString[0].columns
Object {amount: 1049.849}
jsonString[0].columns.amount
1049.849

Write JSON parser to format data for pie chart (HighCharts)

I was fighting with HighCharts quite some hours for formatting the data input to the series option.
Finally I saw the link here solved my problem for data formatting and input.
The data format that would be recognized by HighCharts pie chart is like this (format 1) as indicated by the link above:
[["chrome",15],["firefox",20]]
I actually want dynamic data input from external URL and format the data so that HighCharts can recognized it. The data format I got from the URL is like this (format 2):
[
{
"status": "Stopped \/ Idle",
"val": 17.469444444444,
}, {
"status": "Working",
"val": 0,
}, {
"status": "Headland Turning",
"val": 0,
}, {
"status": "Transport",
"val": 0.15333333333333,
}
]
which is already in JSON format.
I just want to know that is that necessary for me to write a parser for the data from format 2 to format 1? Or Am I missing something that HighCharts can recognize the JSON format data and I don't actually need to write a parser?
I am new to HighCharts so feel free to point that out if some of my problem description does not make sense..Thank you!
EDIT: Thanks for all guys answering my question!
When a script expects data in specific format, you often have to map your data to fit format. This can be modified in server code or using javascript
Can use jQuery $.map to reconfigure an object or array to another array.
DEMO: http://jsfiddle.net/qpsSe/1
Note that trailing commas in sample JSON need removing to validate JSON
/* "json" is your response object, modify variable names to match */
var chartData=$.map( json, function( obj,i){
return [[ obj.status, obj.val]];
})
$.map API Docs
Alternate method in native javascript
var chartData=[];
for( i=0; i<json.length; i++){
chartData.push( [ json[i]['status'], json[i]['val'] ])
}
AFAIK that's just how Highcharts wants its data. That being said, the parser is pretty easy:
var data; // this is your received data
var highchartsData = []; // this is data for highcharts
$.each(data, function(i, e) {
highchartsData.push([e.status, e.val]);
});
One thing to note is that if the data you're receiving is in text (say, a response from an AJAX call) then you need to convert it to a javascript object like so:
var data = $.parseJSON(textData);
You need to make a parser when assigning options as stated in HighCharts preprocessing
Basically, you parse the data and include it in the options:
var serie1 = json.map( function(e) {
return [e.status, e.val];
});
options.series.push({data: serie1});
Here is a working example using $.map and options in Fiddle
Since Highcharts 3.0, categories can also be extracted by giving each point a name and setting axis type to "category".
For a column bar chart this would be:
xAxis: {
type: 'category',
},
series: [{
data: [{
name: 'Point 1',
color: '#00FF00',
y: 1
}, {
name: 'Point 2',
color: '#FF00FF',
y: 5
}]
}]
You can bind chart with JSON data, directly.
You just need to set the json property names as highchart standard.
'Y' for value and 'name' for label.
Your JSON should be as like follow:
[
{
name: "Stopped \/ Idle",
y: 17.469444444444
}, {
name: "Working",
y: 0
}, {
name: "Headland Turning",
y: 0
}, {
name: "Transport",
y: 0.15333333333333
}
]

JSON structure for two JSON objects before parsing?

I am new to JSON and I have a rather basic question....
Here is an example of JSON that is a response from a PHP page. My intention is to have two JSON objects, Photos, and Comments. Unfortunately, my JSON is flawed and it is not formatted properly. I do not know how to structure the JSON so that I can get both objects. What should this look like, if the JSON were correct for two objects? In other words, what do I need between Photos and Comments to indicate that these are two different objects?:
{"Photos": [ {"Filename": "5962230079803.jpg", "PhotoID": "39"}] "Comments": [ {"UserID": "100000660901552", "Comment": "Hello!"}]}
You didn't close your array. Try this:
{
"Photos": [
{
"Filename": "5962230079803.jpg",
"PhotoID": "39"
}
],
"Comments": [
{
"UserID": "100000660901552",
"Comment": "Hello!"
}
]
}
Use JSONLint to validate your JSON
Add a comma between each element:
{
"Photos": [{"Filename": "5962230079803.jpg", "PhotoID": "39"}],
"Comments": [{"UserID": "100000660901552", "Comment": "Hello!"}]
}
If you're defining and parsing this JSON result, and storing it in variable obj, access the properties using:
var obj = JSON.parse( response ); //response is the string containing JSON
var photos = obj.Photos; //Array of photo objects
var comments = obj.Comments;//Array of comment objects
I don't think there is one right answer, but this should do fine:
{
"Photos": [{
"Filename": "5962230079803.jpg",
"PhotoID": "39"}],
"Comments": [{
"UserID": "100000660901552",
"Comment": "Hello!"}]
}
Note the added comma. So your structure was almost there. Now, assuming you parsed it into a variable response, you could access the separate parts like so:
response['Photos'];
response['Comments'];

Categories

Resources