# Nested JSON

**URL:** https://forum.kx.com/t/nested-json/11772
**Category:** Community Support
**Tags:** kdb-and-q
**Created:** [June 2, 2018, 8:26pm UTC](https://forum.kx.com/t/nested-json/11772 "2018-06-02T20:26:00Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![tstannes](https://avatars.discourse-cdn.com/v4/letter/t/a698b9/32.png) [@tstannes](https://forum.kx.com/u/tstannes)
#### Post date: [June 2, 2018, 8:26pm UTC](https://forum.kx.com/t/nested-json/11772/1 "2018-06-02T20:26:00Z")

</div>

Hi,

How can I parse data in the following format into a table?&nbsp;

{“result”:[[“ABC”,“MSFT”,“Equity”,{“display”:“1.23”,“raw”:5.55214},{“display”:“52.05”,“raw”:52.05}],[“DEF”,“GOOG”,“Equity”,{“display”:“2.34”,“raw”:4.38633},{“display”:“96.34”,“raw”:96.34}]]}

I’ve dropped it into a text file and have been trying the following code, by without success:

\l json.k

.j.k raze read0`:test1.txt;

flip a[`result]

I’d like to display this in a table and only need five columns: code, name, type and the two ‘raw’ values in the nested part of the JSON string.

---

<div class="post-metadata">

### Author: ![ryan\_mccarron](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@ryan\_mccarron](https://forum.kx.com/u/ryan_mccarron)
#### Post date: [June 2, 2018, 10:22pm UTC](https://forum.kx.com/t/nested-json/11772/2 "2018-06-02T22:22:00Z")

</div>

Hi,

the problem here is the nested dictionary - to extract this data you can use indexing, then compose a table with a dictionary of column names and flip:

q)a:flip .j.k[read0[`:test.txt] 0]`result&nbsp;/prepare data

q)a[3 4]:a[3 4;`raw]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /index intoneeded dictionary elements

q)t:flip `code`name`type`raw1`raw2!a&nbsp; &nbsp; &nbsp; /convert to table

q)t

code&nbsp; name&nbsp; &nbsp;type&nbsp; &nbsp; &nbsp;raw1&nbsp; &nbsp; raw2

* * *

“ABC” “MSFT” “Equity” 5.55214 52.05

“DEF” “GOOG” “Equity” 4.38633 96.34

This indexing could more neatly&nbsp;be combined into a single step after reading,&nbsp;avoiding reassignment, by use of apply:

q)flip `code`name`type`raw1`raw2!@[a;3 4;@;`raw]

code&nbsp; name&nbsp; &nbsp;type&nbsp; &nbsp; &nbsp;raw1&nbsp; &nbsp; raw2

* * *

“ABC” “MSFT” “Equity” 5.55214 52.05

“DEF” “GOOG” “Equity” 4.38633 96.34

Hope this is useful,

Ryan

* * *

**From:** [personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com) \<personal-kdbplus@googlegroups.com\> on behalf of Student \<tstannes@gmail.com\>  
**Sent:** 02 June 2018 20:26:33  
**To:** Kdb+ Personal Developers  
**Subject:** [personal kdb+] Nested JSON  
&nbsp;

Hi,

How can I parse data in the following format into a table?&nbsp;

{“result”:[[“ABC”,“MSFT”,“Equity”,{“display”:“1.23”,“raw”:5.55214},{“display”:“52.05”,“raw”:52.05}],[“DEF”,“GOOG”,“Equity”,{“display”:“2.34”,“raw”:4.38633},{“display”:“96.34”,“raw”:96.34}]]}

I’ve dropped it into a text file and have been trying the following code, by without success:

\l json.k

.j.k raze read0`:test1.txt;

flip a[`result]

I’d like to display this in a table and only need five columns: code, name, type and the two ‘raw’ values in the nested part of the JSON string.

–  
Submitted via Google Groups

---

<div class="post-metadata">

### Author: ![sohagan8571](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@sohagan8571](https://forum.kx.com/u/sohagan8571)
#### Post date: [June 3, 2018, 12:42am UTC](https://forum.kx.com/t/nested-json/11772/3 "2018-06-03T00:42:00Z")

</div>

Here’s one very simple (qsql) way to parse that bit of text into a table…

q)colms:`code`name`tipe`raw1`raw2

q)update raw1:raw1[;`raw],raw2:raw2[;`raw] from colms!/:.j.k[raze read0`:file]`result

code&nbsp; name&nbsp; &nbsp;tipe&nbsp; &nbsp; &nbsp;raw1&nbsp; &nbsp; raw2

* * *

“ABC” “MSFT” “Equity” 5.55214 52.05

“DEF” “GOOG” “Equity” 4.38633 96.34

But tbh, it might be better to try and get the json output changed (if possible) so as you can read in directly with .j.k.

Also, the .j namespace doesn’t need loaded in later version of kdb+, so if possible too, try to update your version (its faster etc)

Regards
