On Sun, 23 Jun 2019, Artur Tarassow wrote:
Am 23.06.19 um 15:06 schrieb Allin Cottrell:
> On Mon, 17 Jun 2019, Allin Cottrell wrote:
>
>> On Sat, 15 Jun 2019, Artur Tarassow wrote:
>>
>>> Dear all,
>>>
>>> I hope you had a successful gretl conference in Naples!
>>>
>>> I am currently working with json files again. Using latest git version of
>>> gretl on Ubuntu linux (even though the behavior also occurs for older
>>> versions), I've found that some of the json-elements grabbed are not of
>>> the
>>> expected datatype. See the the following example and the file attached:
>>>
>>>
>>> <hansl>
>>> string PATH2JSON = "/home/at/tmp/" # set your
path
>>> string jsonfile = sprintf("%s/ex_json_num_vector.json", PATH2JSON)
>>> string jread = readfile("@jsonfile")
better: readfile(jsonfile)
>>> bundle B = jsongetb(jread)
>>> b = B.JSON
>>>
>>> # Evaluate type of elements
>>> #-----------------------------------------------------
>>> # Item Expected Is
>>> # v1 string null
>>> # v2 string array string array
>>> # v3 scalar scalar
>>> # v4 matrix string array
>>>
>>> loop i=1..nelem(b) -q
>>> string str = "v$i"
>>> eval b["@str"]
>>> eval typestr(typeof(b["@str"]))
>>> print ""
>>> endloop
>>> </hansl>
>>>
>>> Is there a rational reason for this behavior?
>> Not really. It seems that typeof() is not reaching inside bundles to
>> find the type of their contents. That should be made to happen.
> Ah, that comment was not quite right: the failure of typeof() to reach
> inside bundles (and arrays) was specific to the case of string
> objects. That's now fixed in git; snapshots will follow.
Thanks for the fix, Allin. However, why is a numeric vector (element v4)
treated as a string array even though it comprises numeric values only? Is
there any way to import it is a matrix datatype or some way to transform it
to such a datatype using gretl?
We don't know the type of elements in a JSON array, they could be
anything. So the safest thing is to store them as strings.
But of course there's a way to convert from strings to numerical
values (assuming that makes sense) in gretl. Suppose that bundle b
holds an element v4 which is a suitable array:
<hansl>
n = nelem(b.v4)
matrix m = zeros(1, n)
loop i=1..n -q
m[i] = atof(b.v4[i])
endloop
m
</hansl>
Allin