0

I have json files where the various fields can change.

       "eventHistory": [{
        "occurredAt": "2018-03-17T10:40:05.707 0000",
        "calluuid": "G8EMGR6EKD7DLDRP79FOEONVQ4000031",
        "eventId": "2018-03-17T10:40:05.707Z_1521283205_G8EMGR6EKD7DLDRP79FOEONVQ4000031",
        "event": "Data",
        "data": {
            "added": {
                "OtherTrunkName": "sbc-trunk",
                "OriginationDN": "1234",
                "BusinessCall": "0",
                "OriginationDN_location": "MNLSwitch"
            }
        }
    }, {
        "occurredAt": "2018-03-17T10:40:06.033 0000",
        "calluuid": "G8EMGR6EKD7DLDRP79FOEONVQ4000031",
        "eventId": "2018-03-17T10:40:06.033Z_1521283206_G8EMGR6EKD7DLDRP79FOEONVQ4000031",
        "event": "Data",
        "data": {
            "added": {
                "IW_CaseUid": "04d575ba-32e3-48da-8986-a19a6ff493b3",
                "IW_BundleUid": "bf3ac19e-e2ea-4d7b-9b48-ef5e17dfdaa1"
            }
        }
    }, {
        "occurredAt": "2018-03-17T10:40:10.407 0000",
        "calluuid": "G8EMGR6EKD7DLDRP79FOEONVQ4000031",
        "eventId": "2018-03-17T10:40:10.407Z_1521283210_G8EMGR6EKD7DLDRP79FOEONVQ4000031",
        "event": "Data",
        "data": {
            "added": {
                "WrapUpTime": "0"
            },
            "deleted": {
                "OriginationDN_location": "MNLSwitch",
                "OriginationDN": "1234"
            }
        }
    }, 

Is there an 'easy' way to simply read through and add these as variables to a js. I gather each field and then post to PHP which saves. then I need to display in a tree... another question likely to appear on that one too. Any help much appreciated.

user1544780
  • 63
  • 1
  • 8

1 Answers1

0

It is unclear if you require the variables to be declared in a JS file, or if you simply want each key and value pair to be extracted out. Thus, I've provided 2 approaches that could be useful for your use case.

Suggestion: If it doesn't matter, why not handle the whole JSON object in your PHP save script?

1. Extract Key Value pairs

If you require a recursive JS function to get all the keys and values, you may use this function, referenced from here.

Caveat: This function ignores keys with nested key value pairs, so you will need to modify it to suit your needs.

var json = `
        {
    "occurredAt": "2018-03-17T10:40:06.033 0000",
    "calluuid": "G8EMGR6EKD7DLDRP79FOEONVQ4000031",
    "eventId": "2018-03-17T10:40:06.033Z_1521283206_G8EMGR6EKD7DLDRP79FOEONVQ4000031",
    "event": "Data",
    "data": {
        "added": {
            "IW_CaseUid": "04d575ba-32e3-48da-8986-a19a6ff493b3",
            "IW_BundleUid": "bf3ac19e-e2ea-4d7b-9b48-ef5e17dfdaa1"
        }
    }
}
        `
var obj = JSON.parse(json);

iterate(obj,'');

function iterate(obj, stack) {
    for (var property in obj) {
        if (obj.hasOwnProperty(property)) {
            if (typeof obj[property] == "object") {
                iterate(obj[property], stack + '.' + property);
            } else {
                console.log(property + "   " + obj[property]); //get key and value pair here
            }
        }
    }
}

2. Declare Variables in JS

If you require variables to be declared in a JS <script> tag, you can consider using PHP to generate a HTML page with the variables declared.

The code below produces a html file with the following script tag:

script tag of html generated

Advantage: This approach allows you to get creative, and generate a lot of JS code that is variable specific.

Disadvantage: Debugging this could be hard.

Caveat: The example below is not recursive, so you'll have to do that on your own.

<?php
$string = <<<EOD
{
    "occurredAt": "2018-03-17T10:40:06.033 0000",
    "calluuid": "G8EMGR6EKD7DLDRP79FOEONVQ4000031",
    "eventId": "2018-03-17T10:40:06.033Z_1521283206_G8EMGR6EKD7DLDRP79FOEONVQ4000031",
    "event": "Data",
    "data": {
        "added": {
            "IW_CaseUid": "04d575ba-32e3-48da-8986-a19a6ff493b3",
            "IW_BundleUid": "bf3ac19e-e2ea-4d7b-9b48-ef5e17dfdaa1"
        }
    }
}

EOD;

$json = json_decode($string);
echo '<script>';
foreach($json as $key => $value) 
{
$output = <<<EOD

var  $key = "$value";
EOD;
echo $output;
}
echo '</script>';
?>
Edwin Chua
  • 658
  • 1
  • 8
  • 20