jrideout / melt.js

Javascript library inspired by the R reshape package
Apache License 2.0
52 stars 5 forks source link

Using melt.js on nested json data #1

Open jefffriesen opened 10 years ago

jefffriesen commented 10 years ago

I tried melt.js on a complex json structure like this: https://gist.github.com/jefffriesen/7325451

I got this error: Uncaught TypeError: Object # has no method 'forEach' (line 43).

My guess is it doesn't work on nested data structures like this. If that is true, would that be something you would consider adding?

For more context, here is the stackoverflow question I am trying to solve to visualize an XML file with d3.js: http://stackoverflow.com/questions/18425019/converting-xml-to-json-with-generic-keys-instead-of-element-names-as-keys

jrideout commented 10 years ago

My guess is it doesn't work on nested data structures like this.

Correct it assumes you have a list of objects. Depending on which keys your are melting/casting the objects can be nested in some cases, but melt will only operate on the top level.

If that is true, would that be something you would consider adding?

I'd be happy to support this use case for any particular nested json file - like the one in your gist. But per your stackoverflow question, I'm not sure I want a generic solution that could work on structures only known at runtime. So the programmer would either need to understand what the nested structure looked like or have code that figured that out external to melt. Does that distinction make sense? I think it would keep the complexity of melt limited to its core goals and a bit more maintainable. Although I'd entertain a more comprehensive PR :smiley:

Perhaps a place to start would be to think about the API. Maybe using JSONPath?

If you could make a version of building_hpxml.json that looked like your expected output (exactly) and a suggested API, I could probably work backwards toward the implementation.

jefffriesen commented 10 years ago

Thanks for considering this.

The json structure that seems to be the most relevant to d3.js layouts is the one in that flare.json example that Mike Bostock uses everywhere:

{
"name": "flare",
"children": [
{
    "name": "analytics",
    "children": [
    {
        "name": "cluster",
        "children": [
        {"name": "AgglomerativeCluster", "size": 3938},
        {"name": "CommunityStructure", "size": 3812},
        {"name": "HierarchicalCluster", "size": 6714},
        {"name": "MergeEdge", "size": 743}
        ]
        },

where the key names describe the structure (node name, children) with some additional attributes. Here are some examples: http://bl.ocks.org/mbostock/4063570 http://bl.ocks.org/mbostock/1093025 http://mbostock.github.io/d3/talk/20111018/cluster.html http://mbostock.github.io/d3/talk/20111018/partition.html

Many json structures, including the one that I am working with, have data in the keys instead, which I don't think will work with a lot of these layouts.

For the example in my gist, even if you were to support that particular json file, there is variation across json files. They have varying depths and varying nodes. These are being converted from complex XML files (but a lot of json that I have seen is similar to mine)

So maybe this library isn't a good fit for this. But it does seem like d3 users would benefit from a library that can turn typical json files into d3 layout consumable json files.