Search code examples
javascriptphpjsonhandsontable

Constructing JSON with children


I am trying to construct my JSON to look like so...with the __children: [ coming right after Qty Shipped: null,

[
    {
        'Job Number': '22983321',
        'LN #': null,
        'Description': null,
        'Qty': null,
        'AS400 Ship Date': null,
        'Date Showed on Report': null,
        'Days to Manufacture': null,
        'Notes': null,
        'Date Shown Completed': null,
        'Actual Ship Date': null,
        'Qty Shipped': null,
        __children: [
            {
                //values here
            },
        ]
    },
]

With my current code what I am getting is this:

{
    "0": {
        "Job Number": "22359501",
        "LN #": null,
        "Description": null,
        "Qty": null,
        "AS400 Ship Date": null,
        "Date Showed on Report": null,
        "Days to Manufacture": null,
        "Notes": null,
        "Date Shown Completed": null,
        "Actual Ship Date": null,
        "Qty Shipped": null
    },
    "_children": [
        {
            //values here
        },
    ]
}

This is my current code:

$checkJob = '';

//output data into spreadsheet view
while($row = $stmt->fetch(PDO::FETCH_BOUND)) {
    if ($checkJob == $job_number) {
        //print "<b>checkJob: " . $checkJob . " | " . "currentJob: " . $job_number . "</b><br>";
        $json['__children'][] = array(
            'LN #' => $line_item,
            'Description' => $description,
            'Qty' => $qty,
            'AS400 Ship Date' => $as400_ship_date,
            'Date Showed on Report' => $date_showed_on_report,
            'Days to Manufacture' => "5",
            'Notes' => $notes,
            'Date Shown Completed' => $date_shown_complete,
            'Actual Ship Date' => $actual_ship_date,
            'Qty Shipped' => $qty_shipped
        );
    }
    else {
        $checkJob = $job_number;
        //print "checkJob: " . $checkJob . " | " . "currentJob: " . $job_number . "<br>";
        $json[] = array(
            'Job Number' => $job_number,
            'LN #' => null,
            'Description' => null,
            'Qty' => null,
            'AS400 Ship Date' => null,
            'Date Showed on Report' => null,
            'Days to Manufacture' => null,
            'Notes' => null,
            'Date Shown Completed' => null,
            'Actual Ship Date' => null,
            'Qty Shipped' => null,
        );
    }
}

I tried changing $json[][] = array( to $json['__children'][] = array( and it's still not structuring the JSON correctly. I am trying to build the JSON using built in functions as suggested...


Solution

  • You can loop over the data, checking if there is the base data set for each job number (using isset()) and then just add in the children data on each loop...

    while($row = $stmt->fetch(PDO::FETCH_BOUND)) {
        if ( !isset($json[$job_number]))  {
            $json[$job_number] = array(
                'Job Number' => $job_number,
                'LN #' => null,
                'Description' => null,
                'Qty' => null,
                'AS400 Ship Date' => null,
                'Date Showed on Report' => null,
                'Days to Manufacture' => null,
                'Notes' => null,
                'Date Shown Completed' => null,
                'Actual Ship Date' => null,
                'Qty Shipped' => null,
            );
        }
        $json[$job_number]['__children'][] = array(
                'LN #' => $line_item,
                'Description' => $description,
                'Qty' => $qty,
                'AS400 Ship Date' => $as400_ship_date,
                'Date Showed on Report' => $date_showed_on_report,
                'Days to Manufacture' => "5",
                'Notes' => $notes,
                'Date Shown Completed' => $date_shown_complete,
                'Actual Ship Date' => $actual_ship_date,
                'Qty Shipped' => $qty_shipped
            );
        }
    }
    // Remove job number keys
    $json = array_values($json);