2

I have a json like so:

{
"a": {
    "x": {
        "y": {
            "a": {}, 
            "z": {}, 
            "b": {}
        }
    }, 
    "c": {}, 
    "b": {
        "c": {
            "d": {}
        }
    }, 
    "d": {}, 
     ...
    }
}

Is there a quick way to convert it to flare.json format?

Like so:

{
"name":"a",
"children":[
      {
      "name":"x",
      "children":
            {
              "name":"y",
              "children":[{"name":"a", "size":0},{"name":"z","size":0},{"name":"b","size":0}]

...
}

Thank you.

ComputerFellow
  • 11,710
  • 12
  • 50
  • 61

3 Answers3

6

I have come up with a series of regex transforms for this.

 #replace-this      #with-this

  ^\s*"             \{"name":"
  : \{\},           \},
  : \{\}            \}
  ": \{$            ","children":\[
  ^\s*\},           \]\},
  ^\s*\}            \]\}              


 #in that order

and just delete the first and last line (should be an extra { and ]} )

on applying these regex transforms,

this:

{
"a": {
"x": {
    "y": {
        "a": {}, 
        "z": {}, 
        "b": {}
    }
}, 
"c": {}, 
"b": {
    "c": {
        "d": {}
    }
}, 
"d": {} 
}

}

will become this:

{
"name": "a",
"children": [{
    "name": "x",
    "children": [{
        "name": "y",
        "children": [{
            "name": "a"
        },
        {
            "name": "z"
        },
        {
            "name": "b"
        }]
    }]
},
{
    "name": "c"
},
{
    "name": "b",
    "children": [{
        "name": "c",
        "children": [{
            "name": "d"
        }]
    }]
},
{
    "name": "d"
}]
}

which can then be used with some of the d3js examples.

ComputerFellow
  • 11,710
  • 12
  • 50
  • 61
1

You are going to have to write your own JSON or a function to dynamically change the JSON. Flare.json just follows a schema that adheres to Mike Bostock's d3 files.

I'll give you a hint. The schema that you wrote appears to be (in psuedocode)

array("name":"a", "children":array("name":"x","children":array(..... 

Basically, you need to create a multidimensional array in order to get the desired results. I don't know how you are getting your data, unfortunately, so I can't tell you much more. If using php use the json_encode method

echo json_encode($jsonArray)

or in javascript use json.stringify

var json = JSON.stringify($jsonArray)

in order to get the array to turn into json.

Charles Bishop
  • 105
  • 1
  • 7
-1

You can use d3.nest(), however this function won't give you the size as desired, even . You might want to format your data accordingly before sending it to the app.

If you want to create a custom function, then you can have a look at this answer and iterate on the keys of your object, as it is not even an array.

Community
  • 1
  • 1
Christopher Chiche
  • 15,075
  • 9
  • 59
  • 98