Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #18759 > unrolled thread

JSON encode "corrupted"?

Started byTorakiki <torakiki.sanNIENTE@SPAMgmail.com>
First post2021-08-05 17:43 +0200
Last post2021-08-08 11:44 +0200
Articles 7 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  JSON encode "corrupted"? Torakiki <torakiki.sanNIENTE@SPAMgmail.com> - 2021-08-05 17:43 +0200
    Re: JSON encode "corrupted"? Arno Welzel <usenet@arnowelzel.de> - 2021-08-05 18:41 +0200
      Re: JSON encode "corrupted"? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-05 17:20 +0000
        Re: JSON encode "corrupted"? Arno Welzel <usenet@arnowelzel.de> - 2021-08-08 11:43 +0200
    Re: JSON encode "corrupted"? Torakiki <torakiki.sanNIENTE@SPAMgmail.com> - 2021-08-05 21:25 +0200
      Re: JSON encode "corrupted"? "J.O. Aho" <user@example.net> - 2021-08-05 21:49 +0200
      Re: JSON encode "corrupted"? Arno Welzel <usenet@arnowelzel.de> - 2021-08-08 11:44 +0200

#18759 — JSON encode "corrupted"?

FromTorakiki <torakiki.sanNIENTE@SPAMgmail.com>
Date2021-08-05 17:43 +0200
SubjectJSON encode "corrupted"?
Message-ID<seh0um$ket$1@gioia.aioe.org>
json_encode it seems that, in the case of only one element with index = 
0, it returns an incorrect result.

	$array = [];
	$array[0] = "Some text"

it should return (in Firefox 90.0.2)

	{"0": "Some text"}

and instead returns a string like

	["Some text"]

If I add other elements, like

	$array[101] = "Some text 2"
	$array[1234] = "Some text 3"

the result is correct:

	{"0":"Some text","101":"Some text 2","1234":"Some text 3"}

Try this code:

<?php

$array=[];
$array[0]="Some text";
var_dump($array);

$json=json_encode($array);
var_dump($json);


$array=[];
$array[0]="Some text";
$array[101]="Some text 2";
$array[1234]="Some text 3";
var_dump($array);

$json=json_encode($array);
var_dump($json);

?>

Thank's

-- 
 >> Torakiki

[toc] | [next] | [standalone]


#18760

FromArno Welzel <usenet@arnowelzel.de>
Date2021-08-05 18:41 +0200
Message-ID<in2il0Fg6j9U1@mid.individual.net>
In reply to#18759
Torakiki:

> json_encode it seems that, in the case of only one element with index = 
> 0, it returns an incorrect result.
> 
> 	$array = [];
> 	$array[0] = "Some text"
> 
> it should return (in Firefox 90.0.2)
> 
> 	{"0": "Some text"}

Why? $array is still just an array with one scalar element.

> and instead returns a string like
> 
> 	["Some text"]

Which is valid json as well. This defines an array with one element.

See: <https://www.json.org/json-en.html>


-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [next] | [standalone]


#18761

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2021-08-05 17:20 +0000
Message-ID<seh6k8$opv$1@dont-email.me>
In reply to#18760
On Thu, 05 Aug 2021 18:41:04 +0200, Arno Welzel wrote:

> Torakiki:
> 
>> json_encode it seems that, in the case of only one element with index = 
>> 0, it returns an incorrect result.
>> 
>> 	$array = [];
>> 	$array[0] = "Some text"
>> 
>> it should return (in Firefox 90.0.2)
>> 
>> 	{"0": "Some text"}
> 
> Why? $array is still just an array with one scalar element.
> 
>> and instead returns a string like
>> 
>> 	["Some text"]
> 
> Which is valid json as well. This defines an array with one element.
> 
> See: <https://www.json.org/json-en.html>

From that link, the OP's "unexpected" string
>> 	["Some text"]
expresses an array, which (in this case) contains one element.

Had the OP added
	$array[1] = "Other text"
the results probably would have been expressed as
	["Some text","Other text"]

But...

The OP changed the sequential array into a sparse array by
assigning additional elements asequentially:
>>	$array[101] = "Some text 2"
>>	$array[1234] = "Some text 3"

Because of the "sparseness" of this object, it can't be
expressed as a JSON array, so json_encode() encoded the
input sparse array as a JSON object.

-- 
Lew Pitcher
"In Skills, We Trust"

[toc] | [prev] | [next] | [standalone]


#18764

FromArno Welzel <usenet@arnowelzel.de>
Date2021-08-08 11:43 +0200
Message-ID<in9n9dFu47pU4@mid.individual.net>
In reply to#18761
Lew Pitcher:

> On Thu, 05 Aug 2021 18:41:04 +0200, Arno Welzel wrote:
> 
>> Torakiki:
>>
>>> json_encode it seems that, in the case of only one element with index = 
>>> 0, it returns an incorrect result.
>>>
>>> 	$array = [];
>>> 	$array[0] = "Some text"
>>>
>>> it should return (in Firefox 90.0.2)
>>>
>>> 	{"0": "Some text"}
>>
>> Why? $array is still just an array with one scalar element.
>>
>>> and instead returns a string like
>>>
>>> 	["Some text"]
>>
>> Which is valid json as well. This defines an array with one element.
>>
>> See: <https://www.json.org/json-en.html>
> 
> From that link, the OP's "unexpected" string

Why? The string

["some text"]

is a valid JSON array.

Also see here:

<https://jsonformatter.curiousconcept.com>
<http://json.parser.online.fr>

[...]
> The OP changed the sequential array into a sparse array by
> assigning additional elements asequentially:
>>> 	$array[101] = "Some text 2"
>>> 	$array[1234] = "Some text 3"
> 
> Because of the "sparseness" of this object, it can't be
> expressed as a JSON array, so json_encode() encoded the
> input sparse array as a JSON object.

Correct.


-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [next] | [standalone]


#18762

FromTorakiki <torakiki.sanNIENTE@SPAMgmail.com>
Date2021-08-05 21:25 +0200
Message-ID<sehdv4$1ah6$1@gioia.aioe.org>
In reply to#18759
 > Why? $array is still just an array with one scalar element.

Try this (last PHP 7.4.22 x64):

<?php

$array=[];
$array[0]="Some text";
var_dump($array);

$json=json_encode($array);
var_dump($json);


$array=[];
$array[1]="Some text";
var_dump($array);

$json=json_encode($array);
var_dump($json);

?>

you get

---------------------------------------------
array(1) { 
 
 
                                       [0]=> 
 
 
 
     string(9) "Some text" 
 
 
                                        } 
 
 
 
      string(13) "["Some text"]" 
 
 


array(1) { 
 
 
                                       [1]=> 
 
 
 
     string(9) "Some text" 
 
 
                                        } 
 
 
 
      string(17) "{"1":"Some text"}"
---------------------------------------------

IMHO, seem to be a bug!

-- 
 >> Torakiki

[toc] | [prev] | [next] | [standalone]


#18763

From"J.O. Aho" <user@example.net>
Date2021-08-05 21:49 +0200
Message-ID<in2tmsFidbeU1@mid.individual.net>
In reply to#18762
On 05/08/2021 21:25, Torakiki wrote:
> 
>  > Why? $array is still just an array with one scalar element.
> 
> Try this (last PHP 7.4.22 x64):
> 
> <?php
> 
> $array=[];
> $array[0]="Some text";
> var_dump($array);
> 
> $json=json_encode($array);
> var_dump($json);
> 
> 
> $array=[];
> $array[1]="Some text";
> var_dump($array);
> 
> $json=json_encode($array);
> var_dump($json);
> 
> ?>
> 
> IMHO, seem to be a bug!

nope, not a bug, just a standard how to represent different types of arrays.
https://cswr.github.io/JsonSchema/spec/arrays/

[toc] | [prev] | [next] | [standalone]


#18765

FromArno Welzel <usenet@arnowelzel.de>
Date2021-08-08 11:44 +0200
Message-ID<in9nchFu47pU5@mid.individual.net>
In reply to#18762
Torakiki:

> 
>  > Why? $array is still just an array with one scalar element.
> 
> Try this (last PHP 7.4.22 x64):
> 
> <?php
> 
> $array=[];
> $array[0]="Some text";
> var_dump($array);
> 
> $json=json_encode($array);
> var_dump($json);
> 
> 
> $array=[];
> $array[1]="Some text";
> var_dump($array);
> 
> $json=json_encode($array);
> var_dump($json);
> 
> ?>
> 
> you get

Can you PLEASE format your output to NOT take up so much space, thank you!

And no this is not a bug, since JSON arrays can't have keys and therefor
PHP MUST create a JSON object list instead of an array.



-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web