php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76730 fgetcsv don't parse correctly JSON in a cell
Submitted: 2018-08-11 13:56 UTC Modified: 2018-08-11 18:20 UTC
From: alex at render dot es Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.1.20 OS: centos 7, osx 10.13.6, debian 8
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
22 - 20 = ?
Subscribe to this entry?

 
 [2018-08-11 13:56 UTC] alex at render dot es
Description:
------------
---
From manual page: http://www.php.net/function.fgetcsv
---

fgetcsv don't parse correctly JSON in a cell. 
When php parse the cell with;
csv_delimiter = ;
enclosure = "

and the csv has a cell with JSON text like:

{"test":"this is \"test\""}

the fgetcsv parse will done:

{"test":"this is \"test\"""}

this a bad JSON format and the error come when when you try to make a json_decode() with the result.

Test script:
---------------
//CSV
"{""test"":""this is \""test\""""}"

<?php
	ini_set('auto_detect_line_endings',TRUE);
	$f = fopen('test.csv', "r");
	$csv_array=array(); // , $enclosure
	$i=0; 
	while (($line = fgetcsv($f, 0, "\"")) !== false) { 
	foreach ($line as $cell) {
			$final_json = json_decode($cell);
			var_dump($final_json);
		}
	}
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-11 14:20 UTC] alex at render dot es
-Operating System: centos 7 +Operating System: centos 7, osx 10.13.6, debian 8
 [2018-08-11 14:20 UTC] alex at render dot es
add more oses
 [2018-08-11 14:59 UTC] alex at render dot es
-Status: Open +Status: Closed
 [2018-08-11 14:59 UTC] alex at render dot es
Is possible do the escape with the '"' and the error disappear
 [2018-08-11 15:05 UTC] a at b dot c dot de
Are your parameters to fgetcsv correct? You're specifying " as the delimiter as the field delimiter but you're using it as the enclosure character. Also, fgetcsv() defaults to using \ as the escape character.
 [2018-08-11 15:08 UTC] a at b dot c dot de
I think you have too many '"' in your CSV.

"{""test"":""this is \"test\"""}"
 [2018-08-11 15:42 UTC] alex at render dot es
Thanks for your answers.

the number of CSV '"' are correct (the CSV escape with '"').

The problem was with the escape character that need to be '"':
fgetcsv($f, 0, ';', '"', '"')
work ok.

Is confusing because fgetcsv work with the JSON:

"{""test"":""this is test""}" 

without change the default format:

fgetcsv($f, 0)

and in this case the escape is '"' not the default '\'

Why... ??????
 [2018-08-11 15:50 UTC] spam2 at rhsoft dot net
did you try to create a csv with fputcsv and look at the differences between that and you and crafted input? anyways, one would normale use base64 encoding to avoid such issues from start
 [2018-08-11 18:20 UTC] ab@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 17:01:30 2024 UTC