Page 1 of 1

JSON sample not working

Posted: Fri Sep 25, 2015 7:30 pm
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
     ∧

Re: JSON sample not working

Posted: Sat Sep 26, 2015 6:07 pm
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