JSON deserialization
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 !
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 !
3 posts
• Page 1 of 1
JSON deserialization
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.
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
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:
I think that the expression you are looking for is:
Does that work?
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?
-
Morten|Dyalog - Posts: 453
- Joined: Tue Sep 09, 2008 3:52 pm
Re: JSON deserialization
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.
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
3 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group