JSON sample not working

General APL language issues
Post Reply
neeraj
Posts: 82
Joined: Wed Dec 02, 2009 12:10 am
Location: Ithaca, NY, USA

JSON sample not working

Post by neeraj »

I started looking at the JSON I-beams and my very first sample did not work. Here it is

Code: Select all

A
{"first_name" : "Neeraj",
"last_name" : "Gupta",   
"stocks" :               
[                         
  {"S1":"IBM"},           
  {"S2":"MSFT"}           
]                         
}             

 j←0 (7159⌶) A

j.stocks.⎕nl 2 
 S1  S2

       j.stocks.S1
VALUE ERROR
      j.stocks.S1
     ∧
User avatar
Brian|Dyalog
Posts: 120
Joined: Thu Nov 26, 2009 4:02 pm
Location: West Henrietta, NY

Re: JSON sample not working

Post by Brian|Dyalog »

Hi Neeraj,

Actually it is working properly.
j.stocks is a 2 item vector of JSON objects
      j.stocks
#.[JSON object].[JSON object] #.[JSON object].[JSON object]

If you turn ]box on, you can see that j.stocks.⎕NL -2 is a 2 item vector of namelists.
      z.stocks.⎕NL -2
┌────┬────┐
│┌──┐│┌──┐│
││S1│││S2││
│└──┘│└──┘│
└────┴────┘

S1 is in the first item, and S2 is in the second.
j.stocks.S1 is referring to the S1 element in all items, and the VALUE ERROR occurs because there is no S1 element in the second item.
So, to reference S1 and S2 you need to select the appropriate item.
      j.stocks[1].S1
IBM
Post Reply