Parsing Inconsistent JSON data: PHP -


the application pipedrive gives me inconsistent json data. example, in array elements, gives me "formatted_value":"$3,500","weighted_value":2100,"formatted_weighted_value":"$2,100","rotten_time":null, while in others, gives me, "formatted_value":"$2,950","rotten_time":null,"weighted_value":2950,"formatted_weighted_value":"$2,950". json data in order of formatted_value,weighted_value,formatted_weighted_value,rotten_time in every array element, that's not case sadly.

does know of way check right data written right column based on column name , key name?

below code parse json data:

function parsefunction($startpos) { $url = 'urltocalljsondata'; $ch = curl_init($url); //initialize connection url   if(is_callable('curl_init')) {     echo "enabled"; } else {     echo "not enabled"; }  curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, 1);  curl_setopt($ch, curlopt_ssl_verifypeer, true); curl_setopt($ch, curlopt_ssl_verifyhost, 2); curl_setopt ($ch, curlopt_cainfo, dirname(__file__)."/cacert.pem"); $json_response = curl_exec($ch); $info = curl_getinfo($ch); $status = curl_getinfo($ch, curlinfo_http_code); if ( $status != 200 ) {     die("error: call url $url failed status $status, response $json_response, curl_error " . curl_error($ch) . ", curl_errno " . curl_errno($ch)); } curl_close($ch);  $response = json_decode($json_response, true);  $count = count($response['data']);  ($x=0; $x<$count; $x++) {     $currentrecord = $response['data'][$x]; }  //open writing file if($startpos == 0) {     $fp = fopen('cachedeals.csv', 'w'); } else {     $fp = fopen('cachedeals.csv', 'a'); }  $test_array = $response['data'][0];//test_array = first row of data  // writes headers csv file.  if($startpos == 0) {     $keys = array_keys($test_array);     fputcsv($fp, $keys); }  $array_records = $response['data'];//all of incoming data //write data csv file foreach ($array_records $fields) {     fputcsv($fp, $fields); }  //check see if more data should written $more = $response[additional_data][pagination][more_items_in_collection]; $nextstart = $response[additional_data][pagination][next_start]; if($more =="true") {     downloadpipedrivedealsdata($nextstart); }  }//end of function  parsefunction(0); 

sorry cant point out in comments alone, of cleaner such

    //open writing file     if($startpos == 0)     {         $fp = fopen('cachedeals.csv', 'w');     }     else     {         $fp = fopen('cachedeals.csv', 'a');     }      $test_array = $response['data'][0];//test_array = first row of data      // writes headers csv file.      if($startpos == 0)     {         $keys = array_keys($test_array);         fputcsv($fp, $keys);     } 

could this

  // writes headers csv file.      if($startpos == 0){         $fp = fopen('cachedeals.csv', 'w');         fputcsv($fp, array_keys($response['data'][0]));     }else{         $fp = fopen('cachedeals.csv', 'a');     } 

i don't see purpose whole block @ all

$count = count($response['data']);  ($x=0; $x<$count; $x++) {     $currentrecord = $response['data'][$x]; } 

this syntax invalid

$more = $response[additional_data][pagination][more_items_in_collection]; $nextstart = $response[additional_data][pagination][next_start]; 

and issue notice ( undefined constant assuming '' ) etc. because there no quotes around string keys in arrays. in unlikly event 1 of keys constant, that's whole other can of worms. because never data out then. should done way ' or " 's around them. see what php error message "notice: use of undefined constant" mean?

$more = $response['additional_data']['pagination']['more_items_in_collection']; $nextstart = $response['additional_data']['pagination']['next_start']; 

just saying.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -