I need your help.
I want to change this graph: http://bl.ocks.org/mbostock/3885211 to read from a json file instead of tsv. That is the format of my Json file.
{
"name":"chrome" ,
"values": [
{ "time" : 1232323 , "value:" : 12},
{ "time" : 12323323 , "value:" : 19
]},
{
"counter":"Mozilla" ,
"values": [
{ "time" : 1232sd3 , "value:" : 18},
{ "time" : 11232323 , "value:" : 20}
]},
{
"name":"IE" ,
"values": [
{"time" : 12387823 ,"value:" : 100},
{"time" : 12323 ,"value:" : 123123}
]}
]
I have tried a lot but it does not work with Json file.
Really appreciate your help :(
OK. So I decided to spend some time and help you solve the problem. The conversion to JSON was poorly done (for the code that is written on the page).
A quick way to get json from the data would be to use vim to retab the data file before running it through a script like this one https://github.com/turian/common-scripts/blob/master/tsv-to-json.py
I created had to manually reformat the data (since I was just trying to set up an example) but you might want to change the code so that it writes into an array first before producing json (you may also need to change the common.json to json).
The complete example is hosted here http://jsfiddle.net/dvqFj/1/
Good luck
Related
I'm trying to loop through JSON data that I'm pulling from a file. I'm using the require function to get the JSON file but it keeps telling me there is an unexpected '{' token? positions on IDE and console are a little off but I'm pretty sure it's the second objects opening { -position 44-
Any suggestions?
myData.json
{
"colour" : "blue",
"cm" : 2
}
{
"colour" : "red",
"cm" : 5
}
index.js
const fileContents = require("./myData.json");
Because the JSON is invalid. Your JSON file needs to define one entity, where currently you're defining two. Even if the code could read the data, there's no logical way to indicate which of those two objects you want to refer to.
It looks like you meant to put them in an array:
[
{
"colour" : "blue",
"cm" : 2
},
{
"colour" : "red",
"cm" : 5
}
]
Then you can import the whole array, and refer to elements of the array for each object.
I'm currently using node-ews in a project to access an Exchange Web Server via node.js. Now i ran into a weird problem. To run for example a "CreateItem" request, which can be an E-Mail or Appointment for example, I'm giving the function the arguments as a json similar to this
var args = {
"attributes" : {
"SendMeetingInvitations" : "SendToAllAndSaveCopy"
},
"SavedItemFolderId": {
"DistinguishedFolderId": {
"attributes": {
"Id": "calendar"
}
}
},
"Items" : {
"CalendarItem" : {
"Subject" : "",
"Body" : {
"attributes" : {
},
"$value" : ""
},
"ReminderIsSet" : "true",
"ReminderMinutesBeforeStart" : "30",
"Start" : "",
"End" : "",
"IsAllDayEvent" : "false",
"LegacyFreeBusyStatus" : "Busy",
"Location" : ""
}
}
};
As the REST-API I'm writing will receive Attributes like Subject, Start, End etc. I initially stripped out those out of the JSON and would define them later like
args.Items.CalendarItem.Subject = req.body.Subject;
Oddly this will make the internal validation of node-ews fail and tell me that CalendarItem has an invalid Child Subject. If i leave Subject als an empty string in the initial args and later change it set it to req.body.Subject it works just fine.
My question is this: Is the Object somewhat different if I later add attributes and if yes is there a way to do it right? Because I don't think its the best way to have a bunch of empty Attributes in my Object if they aren't used and define standard values for all of them even if api won't require them to be sent.
Would great if someone knew the answer. Hope i could clarify what the problem is
Ok,
so the problem seems to be this. Internally the JSON "object" seems to have an order based on the order the variable is defined. in JavaScript this is no problem. But when the xml is parsed, the tag that was defined last will also be at the end of the xml so if you had
<someTag>
<variable2 />
<variable3 />
</someTag>
and you add variable1 to your json by someTag.variable1 = x in the end the xml will look like this after beeing parsed by node-ews
<someTag>
<variable2 />
<variable3 />
<variable1 >x</variable1>
</someTag>
Now unfortunately the exchange web server seems to be picky about the order of the xml tags. so when you build your json be sure to use the direct order. Changing content of the json later will not affect the order.
I'm having a javascript passing a json array to a cgi script like below.
getData.cgi?jsontext=JSON.strigify(array)
The array is below.
[ { "attr" : "1",
"id" : "1",
"name" : "John"
},
{ "attr" : "2",
"id" : "2",
"name" : "foobar"
}
]
CGI script(getData.cgi):
#!/usr/bin/perl
use CGI;
my $q = CGI->new();
print $q->header;
print $q->param('jsontext');
The script works when I pass only 1 value in array. It is giving 406 error when I pass 2 or more elements like the one I mentioned above.
What can be the reason? The same script works in my loacalhost with multiple elements of array. Is there any htaccess settings problem?
So here is the issue. I have a straight export from mongodb collection as json flat file. I am trying to get my phantomjs app to read and parse the flat file from MongoDB and convert it into an object for phantomjs to parse. For some reason I cannot parse the JSON string normally.
(note: NO jQuery solutions. This needs to be raw javascript)
Here is my flat file from mongodb. It seems fine:
{ "host" : "www.myfoxphilly.com", "path" : "/category/233430/entertainment", "created_at" : { "$date" : 1375199393295 }, "_id" : { "$oid" : "51f7e0a1dc12a13510000002" } }{ "host" : "www.news9.com", "path" : "/category/112032/news", "created_at" : { "$date" : 1375285798173 }, "_id" : { "$oid" : "51f9322668ee1e660c000001" } }{ "host" : "www.myfoxphilly.com", "path" : "/category/233430/entertainment", "created_at" : { "$date" : 1375285823602 }, "_id" : { "$oid" : "51f9323f68ee1e660c000002" } }
Here is the config file that is attempting to parse the above JSON flat file
var fs = require('fs');
var data = fs.read('configData.json');
var newdata = JSON.stringify(data);
var dataobj = eval("["+newdata+"]");
console.log(typeof(newdata));
exports.tests = dataobj;
// Sample object (works fine like this):[{path:'/category/112043/sports' ,host:'www.newson6.com'}];
exports.getFileName = function(test,local) {
return 'results/' + test.host.replace(/\./g,'-').replace(/\:[0-9]+/,'').replace('-com','').replace('www-','') + test.path.replace(/\//g,'-').replace(/\?clienttype=/g, "clienttype") + ((local) ? '-locl' : '-prod')
}
So when I run phantom, I get no data. That JSON becomes one single object, instead of the object example I have in the comment section.
If I just replace the JS common library flat file import and make "data" a string, it works just fine, like so:
var data = '{"host" : "www.myfoxphilly.com", "path" : "/some/path/233409"}';
Is there some sort of issue going on with the js common library file import when I import the JSON in as a string? Help please.
ONCE AGAIN, no jQuery, I will down vote you without looking. I <3 jQuery, but you guys need to realize when it's appropriate to use (i.e. browser-based js).
OMG! You used eval ... :P I'm suprised this question hasn't already been down voted 5 times.
On the real, excellent question.
Your problem, if #DCoder has actually posted an answer, is your JSON. A 'flat file from mongodb' is not necessarily valid JSON. Further, to make it valid you're gonna need to parse the string first:
wrap it in square braces
make sure you have a comma after each data line exported from mongo.
Seriously, eval? Use JSON.parse twice on your converted string.
That should solve it. Everything else looks clean.
(.. eval .. I cant believe this scrub)
var location = { "location" : {
"name" : $("#user_loc_name").val(),
"street_address" : $("#user_loc_street_address").val(),
"city" : $("#user_loc_city").val(),
"province" : $("#user_loc_province").val(),
"country" : $("#user_loc_country").val(),
"postal_code" : $("user_loc_postal_code").val(),
"public" : $("#user_loc_public").attr('checked')
}};
( ... )
$.post(url, location, success_callback);
The reason I need this 'nested' map is because I'm sending this to my rails backend, and I'm hoping I can make a simple update _ attributes(params[:location]) in the controller. Unfortunately, with this solution I get Parameters:
{"location"=>"[object Object]", ...}
Not what I'm hoping for. I'm hoping for:
{"location"=> {"name" => "valforname", "street_address" => "valforstreetadress", ...}, <other params>...}
If I get rid of the 'nesting' and just send the inner map it works fine, but each attribute shows up separately in the params hash and it's just cumbersome and messy. If I could get the whole map nested under a key of "location" it would be much nicer.
Have a look at this:
Serializing Objects in Javascript
jQuery does not support JSON serializing OOTB. Try any number of libraries. Here's the standard one:
<script src="http://www.json.org/json2.js"></script>
Your code then looks like:
$.post(url, JSON.stringify(location), success_callback);