Json format issue in d3 pack layout - javascript

I am very new to d3. I have 3 days old d3 knowledge. I was trying to make one pack layout but I am not able to call translate(of transform) function based on the data in external json file. My json file is not formatted as name, children order (which has been used most of the examples). So, can any one clarify that whether we must have the json file in proper format like in tree structure to get the proper pack or tree layout. My json file format is:
{
"sourcefile":"Script",
"structure":{
"Links":[
[
"step1",
"port1",
"step2",
"port2"
],
[
"step3",
"port3",
"step4",
"port4"
]
],
"device":{
"step1":{
"args":{
"pin":[
"XXXX",
100
]
},
"device_type":"console"
},
"lock":{
"args":{
"username":[
"XXXX",
"test"
],
"address":[
"XXXX",
"10.0.0.1"
]
},
"device_type":"Light"
}
}
}
}
It it's true..I was wondering if anyone can tell me about some online tool to format this json file into the following format..
{
"name": "Names",
"children":
[
{ "name": "John", "size": 100 }
]
}

Your intuition is correct. The pack layout requires input data formatted as a hierachy. It seems unlikely that an online tool to convert general JSON data to this structure would exist, as such a tool would almost have to be custom-built for each particular data set. However, d3 itself has utility functions to help you to create the required structure. I'm referring to the Nest utilities. Looking at your input data, it's not at all obvious how to structure it in a hierarchy, so I can't offer any specific implementations suggestions. In general, though, I'd suggest transforming your data into a simple array of objects and then use the d3.nest utilities to extract the hierarchy from the data.

Related

Conveter of CSV file into JSON with child element

I got these CSV to be converted to JSON, I got million of them:
"pcid","category","synonym_category"
20019,"Accountant","accounting,tax"
20047,"Acoustical Consultant","acoustic, acoustics, noise, sound"
watch the synonym_category that I need to convert it into nested elements/array.
like the following JSON:
[
{
"pcid": 2119,
"category":"Accountant",
"synonym_category":[
{"1":"accounting"},
{"2":"tax"}
]
},
{
"pcid": 2047,
"category":"Acoustical Consultant",
"synonym_category":[
{"1":"acoustic"},
{"2":"acoustics"},
{"3":"noise"},
{"4":"sound"}
]
}
]
I don't need PHP, Javascript or any programming solution, I need a converter (online or maybe a software that solve this situation. Any suggestion?

File Naming Conventions .json or .js?

So, I'm trying to create some helpful JSON files that other can use to prototype out applications using State JSON data with image names that correspond to actual images I have in the same directory.
Here are my files : https://github.com/tannerjt/state_images.json
My question is this... since JSON is just javascript, should I use .json or .js for files.
One of my files is technically JSON, but I named it states.js, since it is an array of javascript object...
[
{
"name": "Alabama",
"abbreviation": "AL",
"capital": "",
"image": {
"image_name": "alabama.jpg",
"location": "Monte Sano State Park",
"credit": {
"author": "Wes Thomas (BamaWester on Flickr)",
"url": "http://www.flickr.com/photos/bamawester/3493269235/"
}
}
},....
]
Because I called it a .js file, I added
var states = [{...
to the beginning. With it being an Array of json object... JSONlint still validated it.
Any thoughts? I want to make this useful for people to play around with, but not sure how I name the files correctly.
if you included var states = [{... in your raw text and tried to parse the thing as a json object, it would probably fail. Saving it as a .json file and leaving it only with data would be a preferred option.

result not coming with d3js?

After lot of help from stackoverflow folks,finally resolved my json and now its looking good.
luck.json--->
{
"PERFECT_JSON_object":
{
"51b59c1bbae1c":
[
{ "id": "parties", "float_1": "0.006" , "float_2": "0.9"},
{ "id": "royal-challenge", "float_1": "0.02" , "float_2": "0.333" },
{ "id": "star-speak","float_1": "0.02","float_2":"0.1" }
],
"51b59c1bbae3c":
[
{ "id": "parties","float_1": "0.006" , "float_2": "0.9"},
{ "id": "star-speak","float_1": "0.02", "float_2": "0.009" }
],
"51b59c1bbae5c":
[
{ "id": "parties","float_1": "0.006" , "float_2": "0.9"}
]
}
}
I have been trying to get my head around d3js with json,and I must say I have progressed quite a bit.But I am still not able to get the output with json data.
I went through these link`s but dint help.
https://github.com/mbostock/d3/wiki/Requests
d3.js & json - simple sample code?
Access / process (nested) objects, arrays or JSON
MyFIDDLE with json(no output,something wrong in here)
same fiddle with some static values( without Json)--
This is the result that I want.
I know that d3.json method requires json file to be on server.For temporary basis,as the json file is small can we include it directly in a variable in our d3 script??
I think I am messing up with json data in a wrong way.Can somebody help me with it
Yes, you can just add the JSON in a variable and run it this way. See here for the updated jsfiddle. You basically just add your JSON after var data =.

Creating nested json object using mysql query

I currently have a json file that is hard coded. Here is a snippet:
{
"name": "Projects",
"children": [
{
"name":"Project_Category(example:MobileApps)",
"description":"category",
"children": [{
"name":"Sub_category(example:MusicPlayer)",
"description":"some_description(example:The music app will play music from an android device...)",
"children":[
{//child 1 of the MusicPlayer subcategory
"name": "Actual_project_name(example:JukeBox)",
"description":"Actual_project_description",
"children":[
{"name":"projectGroupMember1", "email":"groupMember1Email#yahoo.com"},
{"name":"projectGroupMemeber2", "email":"groupMember2Email#yahoo.com"}
]},
{ //child 2 of the MusicPlayer subcategory
"name": "another_project_title",
"description":"another_project_description",
"children":[
{"name":"projectGroupMember1", "email":"groupMember1Email#gmail.com"},
{"name":"projectGroupMember2", "email":"groupMember2Email#gmail.com"}
]
}
]//end of MusicPlayer's children
}, //end of MobileApps children
...
Having a database where all this data is stored, I have been trying to use php and nested mysql queries to generate this output to a json file which I will use for other tasks (data visual). My goal was to generate this file everytime the web application is opened so I can the have the most up to date version of it from the database. However, I have been having difficulties using nested mysql queries to create this output. So my question is, given the way my json file is structured is this feasible? Is there a better approach I should do instead? Any suggestions would help.
mysql does not generate any nested structure for you. You have to do it by yourself.
There are many frameworks that would do it for you. The question is what server you have.
For example there is cakephp for Php which could give you a very similar structure. Also ASP.NET MVC with code first can do that. There are tons of possiblities.
If you want to do it without any framework, we still need to know, which server you use.

URL GET parameter representing and parsing embedded objects

I'm working on an API that consists of several collections that have relations to each other. I want to give users the opportunity to eagerly fetch records from associated collections via a GET url parameter.
For instances
/api/clients/
Would return an array of objects, each representing a client.
But clients have "employees" and "templates". "Templates" also have "revisions." And lastly, "revisions" have "groups"
My strategy for the format of the url parameter is something like this:
/api/clients?expand=[employees][templates[revisions[groups]]]
Which represents:
clients
+ employees
+ templates
+ revisions
+ groups
My question is, what is a good way to go about parsing a string in this format: [employees][templates[revisions[groups]]]
to arrive at an object like this:
{
"employees": {},
"templates": {
"revisions": {
"groups": {}
}
}
}
Or something similar that's easy to work with. I'm working in NodeJS so any answers specific to that environment are a plus. Regex?
Going off #Kovge 's suggestion, I'm going to handle this situation with a JSON string passed in my URL get request. It's a bit verbose, but should be acceptable.
Here's how I'd represent my eager-fetching associated collection records:
[
{
"resource": "employees"
},
{
"resource": "templates",
"extend": [
{
"resource": "revisions",
"extend": [
{
"resource": "groups"
}
]
}
]
}
]
Basically using arrays of objects with "resource" parameters and optional "extend" parameters. My request will end up looking like this: (url encoded)
http://example.com/api/clients?extend=%5B%7B%22resource%22%3A%22employees%22%7D%2C%7B%22resource%22%3A%22templates%22%2C%22extend%22%3A%5B%7B%22resource%22%3A%22revisions%22%2C%22extend%22%3A%5B%7B%22resource%22%3A%22groups%22%7D%5D%7D%5D%7D%5D
EDIT:
This is what my result ended up looking like, still playing with things.

Categories

Resources