-1

I am using ajax to get a json response and fill my datatable however for some reason some random values in the json have a line break. In the database the value will be lets say "90 g per 100 m of row" but it outputs in the json like below.

enter image description here

All other rows are fine and here is how I make the json object.

for($i = 0; $i < $numRows; $i++) {

        $chemicals = getPestsChemicals($connection, $rows[$i]['code']);
        $chemNames = '';

        foreach($chemicals as $chem) {
          $chemNames .= $chem['chemical'].'<br>';
        }

        $mrls = getPestsMrls($connection, $rows[$i]['code']);
        $mrlNames = '';

        foreach($mrls as $mrl) {
          $mrlNames .= (strlen($mrl['mrl']) == 1 ? $mrl['mrl'].".0" : $mrl['mrl']).'<br>';
        }

        if($i != $numRows-1) {
          $body .= '
          {
            "crop": "'.$rows[$i]['crop'].'",
            "diseases": "'.$rows[$i]['pest_name'].'",
            "chemical": "'.$chemNames.'",
            "product": "'.$rows[$i]['product']." ".($rows[$i]['footnote'] != NULL ? '<sup class=\"text-danger font-weight-bold\">'.$rows[$i]['footnote'].'</sup>' : NULL ).'",
            "rate": "'.$rows[$i]['rate'].'",
            "max_no": "'.$rows[$i]['max_no'].'",
            "hi": "'.$rows[$i]['hi'].'",
            "mrl": "'.$mrlNames.'",
            "pcs_no": "'.($rows[$i]['pcs_no'] == 0 ? 'DR' : '0'.$rows[$i]['pcs_no']).'",
            "supplier": "'.$rows[$i]['supplier'].'",
            "use_by_date": "'.$rows[$i]['use_by_date'].'"
          },
        ';
        } else {
          $body .= '
          {
            "crop": "'.$rows[$i]['crop'].'",
            "diseases": "'.$rows[$i]['pest_name'].'",
            "chemical": "N/A",
            "product": "'.$rows[$i]['product']." ".($rows[$i]['footnote'] != NULL ? '<sup class=\"text-danger font-weight-bold\">'.$rows[$i]['footnote'].'</sup>' : NULL).'",
            "rate": "'.$rows[$i]['rate'].'",
            "max_no": "'.$rows[$i]['max_no'].'",
            "hi": "'.$rows[$i]['hi'].'",
            "mrl": "N/A",
            "pcs_no": "'.($rows[$i]['pcs_no'] == 0 ? 'DR' : '0'.$rows[$i]['pcs_no']).'",
            "supplier": "'.$rows[$i]['supplier'].'",
            "use_by_date": "'.$rows[$i]['use_by_date'].'"
          }
        ';
        }
      }

I have tried to wrap the relevant row in strval or json_encode but no success. Also here is how it looks in the db

enter image description here

BranOIE
  • 400
  • 4
  • 19
  • There is a chance that your json gets encoded by [chunked-encoding](https://stackoverflow.com/questions/3829145/can-you-explain-chunked-encoding) – Luuk Mar 05 '21 at 12:44
  • @Luuk How can I counter that? – BranOIE Mar 05 '21 at 12:47
  • 1
    Would your db manager show a linebreak if it was in the data? If you get the stored data as bytes it'll reveal what's actually in there. – Gavin Mar 05 '21 at 12:48
  • @BranOIE: If that really is the problem, you have to read the links in the links to understand what is going on. After that it might be a change in you webserver, but .... searching is your friend see: https://stackoverflow.com/questions/14315224/disable-chunked-encoding-for-http-server-responses – Luuk Mar 05 '21 at 13:06
  • @Luuk chunked encoding would apply on the HTTP level, it should not affect what the actual data looks like after it has been decoded again. – CBroe Mar 05 '21 at 13:38
  • @CBroe: Because of "lack of information" on how the data is received, I gave him a "dive into this pit, and drown". Because how should anyone know where this line-break is coming from with the data give in the question? – Luuk Mar 05 '21 at 16:28

1 Answers1

0

For a quick fix try

$rate_string = (string) $rows[$i]['rate'];

then just replace:

"rate": "'.$rate_string .'",

If it fails to cast into string you might have a further clue.

Mbotet
  • 170
  • 2
  • 17