0

I have a CSV file full of data that has been converted into JSON. Right now I'm trying to figure out how I can send multiple JSON objects into the API JSON array. I'm currently using ractive as my chocie of framework. The JSON objects will be added by clicking on the "Send JSON Data" button

enter image description here

CSV to JSON Conversion

<html>

<body>

<?php 

    function csvToJson($fname) {
        if (!($fp = fopen($fname, 'r') )) {
            die("Can't open file");
        }

        $key = fgetcsv($fp, "1024", ",");

        $json = array();
            while ($row = fgetcsv($fp, "1024", ",")) {
                $json[] = array_combine($key, $row);
        }

        fclose($fp);

        foreach ( $json as $k=>$v ) {


            $json[$k]['dateRequested']     = $json[$k]['DATE']; 
            $json[$k]['assignedAgent']     = $json[$k]['AGENT'];
            $json[$k]['finalCompanyName']  = $json[$k]['COMPANY NAME'];
            $json[$k]['unitNumber']        = $json[$k]['UNIT'];
            $json[$k]['floorNumber']       = $json[$k]['FLOOR_LEVEL'];
            $json[$k]['buildingName']      = $json[$k]['BUILDING NAME'];
            $json[$k]['streetNumber']      = $json[$k]['NUMBER'];
            $json[$k]['streetName']        = $json[$k]['STREET'];
            $json[$k]['subdivisionName']   = $json[$k]['SUBDIVISION_COMPOUND_VILLAGE'];
            $json[$k]['barangayName']      = $json[$k]['BARANGAY'];
            $json[$k]['districtNum']       = $json[$k]['DISTRICT'];
            $json[$k]['cityMunicipality']  = $json[$k]['CITY_MUNICIPALITY'];
            $json[$k]['provinceName']      = $json[$k]['PROVINCE'];
            $json[$k]['region']            = $json[$k]['REGION'];
            $json[$k]['area']              = $json[$k]['AREA'];
            $json[$k]['longitude']         = $json[$k]['LONGITUDE'];
            $json[$k]['latitude']          = $json[$k]['LATITUDE'];
            $json[$k]['secRanking']        = $json[$k]['Sec Ranking'];
            $json[$k]['affiliate']         = $json[$k]['Affiliates'];
            $json[$k]['profiling']         = $json[$k]['Profiling'];
            $json[$k]['contactNumber']     = $json[$k]['CONTACT NUMBER'];
            $json[$k]['industry']          = $json[$k]['INDUSTRY']; 
            $json[$k]['amAssigned']        = $json[$k]['AM ASSIGNED'];
            $json[$k]['source']            = $json[$k]['SOURCE'];
            $json[$k]['gbu']               = $json[$k]['GBU'];
            $json[$k]['retention']         = $json[$k]['RETENTION'];
            $json[$k]['sec']               = $json[$k]['SEC'];
            $json[$k]['tier']              = $json[$k]['TIER'];
            $json[$k]['acqui']             = $json[$k]['ACQUI'];
            $json[$k]['finalReco']         = $json[$k]['FINAL RECO'];
            $json[$k]['remarksFindings']   = $json[$k]['FINAL REMARKS'];

            unset($json[$k]['DATE']);
            unset($json[$k]['AGENT']);
            unset($json[$k]['COMPANY NAME']);
            unset($json[$k]['UNIT']);
            unset($json[$k]['FLOOR_LEVEL']);
            unset($json[$k]['BUILDING NAME']);
            unset($json[$k]['NUMBER']);
            unset($json[$k]['STREET']);
            unset($json[$k]['SUBDIVISION_COMPOUND_VILLAGE']);
            unset($json[$k]['BARANGAY']);
            unset($json[$k]['DISTRICT']);
            unset($json[$k]['CITY_MUNICIPALITY']);
            unset($json[$k]['PROVINCE']);
            unset($json[$k]['REGION']);
            unset($json[$k]['AREA']);
            unset($json[$k]['LONGITUDE']);
            unset($json[$k]['LATITUDE']);
            unset($json[$k]['Sec Ranking']);
            unset($json[$k]['Affiliates']);
            unset($json[$k]['Profiling']);
            unset($json[$k]['CONTACT NUMBER']);
            unset($json[$k]['INDUSTRY']);
            unset($json[$k]['AM ASSIGNED']);
            unset($json[$k]['SOURCE']);
            unset($json[$k]['GBU']);
            unset($json[$k]['RETENTION']);
            unset($json[$k]['SEC']);
            unset($json[$k]['TIER']);
            unset($json[$k]['ACQUI']);
            unset($json[$k]['FINAL RECO']);
            unset($json[$k]['FINAL REMARKS']);

        }

        return json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

    }

 ?>



<?php
$json_data = csvToJson("lms.csv");
?>

Javascript

<div id="container">

</div>


<script src="https://cdnjs.cloudflare.com/ajax/libs/ractive/0.9.0-build-48/ractive.js"></script>


<script id="template" type="text/ractive">
     <a class="button cta" on-click="addData">Send JSON Data</a>
</script>

<script type="text/javascript">

var app = new Ractive({
    el       : '#container',
    template : '#template',
});

var proxy       = 'http://192.168.1.126/lms-dev-noel/proxy.php';
var endpoint    = 'account/leads/';
var rt          = 'POST';
var url         = proxy+'?endpoint='+endpoint+'&rt='+rt;

var lms_json    = <?php echo json_encode($json_data); ?>;

var jobjects = JSON.parse(lms_json);
console.log(jobjects)




app.on('addData', function(event) {
    $.ajax({
        type     : 'POST',
        url      : url,
        data     : jobjects,
        dataType : 'json',
        success  : function() {
            console.log(jobjects);
        },
        error    : function(error) {
            console.log('Error')
        }

    })

})


</script>

</body>

</html>
Ashish Bahl
  • 1,482
  • 1
  • 18
  • 27
clestcruz
  • 1,081
  • 3
  • 31
  • 75
  • little ambiguous, could you please confirm if you just want your json objects to be inside an array? something like [jobjects, jobjects, jobjects....] – MGA Jan 16 '17 at 09:57
  • Yes, Basically I'm trying to add or push the JSON objects inside an array. – clestcruz Jan 16 '17 at 09:59
  • you can simply have an array declared before the ajax request like below var arr = []; and inside the success handler you can push the result object into this array, arr.push[jobjects]; – MGA Jan 16 '17 at 10:00
  • Well what If I wanted to push the JSON objects inside an API JSON array. – clestcruz Jan 16 '17 at 10:21

1 Answers1

0

JSON is just a notation, it doesn't change anything. You should be able to just push objects/values on your desired key like usual after having parsed the JSON string.

array[key].push(object);

for example:

jobjects['myKey1']['myKey2'].push({"key1":"value1", "key2":"value2"});

ref: Adding a new array element to a JSON object

Community
  • 1
  • 1
mTv
  • 1,074
  • 17
  • 28