0

I'm working with jquery orgchart, and i want to save the changes of my chart to database, orgchart has a method to get the modified tree structure getHierarchy(). The problem is how to parse the JSON and save to database.

DEMO JSON:

{
  "id": "1",
  "children": [
    {
      "id": "40",
      "children": [
        {
          "id": "53"
        }
      ]
    },
    {
      "id": "57",
      "children": [
        {
          "id": "72",
          "children": [
            {
              "id": "73"
            }
          ]
        }
      ]
    }
  ]
}

I want something like a flat Array to an easy update in DB

(
    [0] => Array
        (
            [id] => 1
            [parentid] => 0
        )

    [1] => Array
        (
            [id] => 40
            [parentid] => 1
        )

    [2] => Array
        (
            [id] => 53
            [parentid] => 40
        )

    [3] => Array
        (
            [id] => 57
            [parentid] => 1
        )

    [4] => Array
        (
            [id] => 72
            [parentid] => 57
        )

    [5] => Array
        (
            [id] => 73
            [parentid] => 72
        )

)
  • The first entry from google "How to parse JSON with jquery" --> https://stackoverflow.com/questions/1607937/how-to-parse-json-array-in-jquery/1608139 – bated Sep 19 '17 at 15:29
  • 1
    Possible duplicate of [How to parse JSON data with jQuery / JavaScript?](https://stackoverflow.com/questions/8951810/how-to-parse-json-data-with-jquery-javascript) – bated Sep 19 '17 at 15:31
  • The real problem was not to parse it with JavaScript or PHP, but create the recursive function that retrieve that structure in that array format.. i tried with array_walk_recursive in PHP and other solutions in JavaScript but after many try and error made it.. thank you anyway – Henry Gabriel González Montejo Sep 19 '17 at 17:40

2 Answers2

2

var jsonData = [JSON.parse('{"id":"1","children":[{"id":"40", "children":[{"id":"53"}]},{"id":"57","children":[{"id":"72","children":[{"id":"73"}]}]}]}')],
  outputData = [],
  parentId = 0;

function convert( data, parentId ){
  $.each( data, function(index, item){
    outputData.push({
      id: item.id,
      parent_id: parentId
    });

    if(item.hasOwnProperty('children')){
       convert(item.children, item.id );
    }
  });
}  

convert( jsonData, 0 );
  
console.log(outputData);
<body>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>

</body>
Daniel W
  • 433
  • 3
  • 8
1

Well this is my solution, not the best but works..The JSON is send to PHP where i made the updates..

 $datax=json_decode($this->request->query['tree'],true);

         function tree_to_array($datas, $father = 0)
        {
            $array = Array();
            foreach ($datas as $val)
            {
                if(isset($val["id"]))
                {
                    $toSaveDB = array("id" => $val["id"], "parent_id" => $father);
                    $array[] = $toSaveDB;
                    $parent_id=$val["id"];

                    if(isset($val["children"]))
                    {
                        //Root problem.. this is only for nodes that have children
                          if($parent_id!=1)
                          {
                              $children = tree_to_array($val["children"], $parent_id);
                              if (!empty($children))
                              {
                                  $array = array_merge($array, $children);
                              }
                          }
                    }

                }
                else
                {
                    if (is_array($val))
                    {
                                    $children = tree_to_array($val, $parent_id);
                                    if (!empty($children))
                                    {
                                        $array = array_merge($array, $children);
                                    }
                    }
                 }
            }
            return $array;
        }


        $new_array=tree_to_array($datax, $father = 0);
        //Finally save to DB    
        foreach($new_array as $node)
        {

            $this->Area->id = $node['id'];
            $this->Area->saveField('parent_orgchart', $node['parent_id']);
        }