JSON deserialization

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !

JSON deserialization

Postby michael on Mon Mar 26, 2018 1:36 pm

I am trying to import from a json file.
What I basicly do is this.

string←⎕NGET filename
jsondoc←⎕JSON string

Now jsondoc is one long string, which is not really what I wanted. I wanted either a namespace or at least a matrix with the info properly deserialized.
michael
 
Posts: 2
Joined: Fri Mar 23, 2018 4:26 pm

Re: JSON deserialization

Postby Morten|Dyalog on Tue Mar 27, 2018 7:39 am

Hi Michael!

I think your problem is that ⎕NGET returns a three element vector: The data from the file is in the first, followed by information about the encoding and the line endings found in the file.

⎕JSON is "self inverse": If you give it a simple character vector, it assumes it is JSON and you want to convert FROM JSON to an APL structure. If you give it anything else, like the nested array returned by ⎕NGET, it assumes you want to convert TO JSON. This guessing is perhaps slightly questionable design, note that you can specify whether you want the result to be JSON or not with a boolean left argument. You will need this in the case where want to convert a simple character vector to JSON, as in:

Code: Select all
     1 ⎕JSON 'Hello'
"Hello"

I think that the expression you are looking for is:

Code: Select all
ns←⎕JSON ⊃⎕NGET filename

Does that work?
User avatar
Morten|Dyalog
 
Posts: 453
Joined: Tue Sep 09, 2008 3:52 pm

Re: JSON deserialization

Postby michael on Tue Mar 27, 2018 8:57 am

Thanks Morten

Yes it did produce the desired to pick the NGET result. And you are right I just assumed that if ⎕JSON did not return an error then it would be importing and not exporting.
michael
 
Posts: 2
Joined: Fri Mar 23, 2018 4:26 pm


Return to APL Chat

Who is online

Users browsing this forum: No registered users and 1 guest