JSON from LLM response

21 views
Skip to first unread message

Thomas McGuire

unread,
Sep 23, 2025, 5:50:53 PMSep 23
to fo...@jsoftware.com
So I think the response in JSON format is regular. I had placed code for request I am making under another heading. I get JSON back which I use dec_pjson_ to decode the JSON into J boxed structure. From the original C code I have been looking at they use the following JSON calls to get at the important payload in an otherwise verbose JSON response: 

    cJSON* choices = cJSON_GetObjectItem(root, "choices");
    cJSON* first_choice = cJSON_GetArrayItem(choices, 0);
    cJSON* message = cJSON_GetObjectItem(first_choice, "message");
    cJSON* content = cJSON_GetObjectItem(message, "content");

I have the following equivalent J code: 

choices =: 'choices' jsongetkv parsed


NB. for now just return the first choice. In general there will

NB. likely be multiple array elements that will need to interpreted

NB. 1{:: selects the value of the key-value pair

NB. since it's an array select the first element and unbox

firstChoice =: > 0{ 1 {:: choices


NB. get the 'message' key-value pair

message =: 'message' jsongetkv firstChoice


NB. select the value of the key value pair then look for the content entry

content =: 'content' jsongetkv 1 {:: message


NB. return the value of the content key-value pair which is the

NB. answer string returned by the LLM

1{:: content


So my question is there any best practice for how to navigate J boxes that represent JSON structures? The fetch command can’t navigate the whole way on its own because of the array boxed in value for choices.


Using fetch commands it seems it needs to be broken up into separate fetches: 

(1,1){:: (1,1){:: 0{:: 1{:: 'choices' jsongetkv__mchat parsed__mchat


I have to use an i. access based on the key ‘choices’ because that is not always in the same position in the intial dictionary. jsongetkv does an i. lookup and uses the index to fetch the whole record.


Also if I try to fetch in one combined fetch I get an index error.


Here is the ‘choices’entry so you can see the decoded JSON structure truncated so it can fit on a laptop screen without wrapping:

┌───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

│choices│┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

│ ││┌─────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────

│ │││index │0

│ ││├─────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────

│ │││message │┌───────────┬──────────────────────────────────────────────────────────────────────────────────────────

│ │││ ││role │assistant

│ │││ │├───────────┼──────────────────────────────────────────────────────────────────────────────────────────

│ │││ ││content │One fascinating aspect of space is the phenomenon of gravitational lensing. This occurs wh

│ │││ │├───────────┼──────────────────────────────────────────────────────────────────────────────────────────

│ │││ ││refusal │

│ │││ │├───────────┼──────────────────────────────────────────────────────────────────────────────────────────

│ │││ ││annotations│

│ │││ │└───────────┴──────────────────────────────────────────────────────────────────────────────────────────

│ ││├─────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────

│ │││logprobs │

│ ││├─────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────

│ │││finish_reason│stop

│ ││└─────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────

│ │└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

└───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────


So once you have looked up ‘choices’ that has a value of an array of 1 entry in this particular request-response with chatGPT. The message and content entries appear to be in the same relative location across different LLMs. So just wondering if there is a preferred way to access these embedded JSON structures.

Henry Rich

unread,
Sep 23, 2025, 6:27:43 PMSep 23
to fo...@jsoftware.com
I haven't thought about the best way to navigate these structures.  Boxing seems inefficient to me, but I don't have a better idea.

The left arg of {:: can take you through the whole tree in one go:

   ]r =. 1 2;3 4;<<"1 i. 2 2 2
+---+---+---------+
|1 2|3 4|+---+---+|
|   |   ||0 1|2 3||
|   |   |+---+---+|
|   |   ||4 5|6 7||
|   |   |+---+---+|
+---+---+---------+
   1 {:: r
3 4
   (1;0) {:: r
3
   (2;0) {:: r
+---+---+
|0 1|2 3|
+---+---+
   (2;0 1) {:: r
2 3
   (2;0;1) {:: r
|rank error, executing dyad {::
|   (2;0;1)    {::r
   (2;0 1;1) {:: r
3

Rules to remember:

* if x is unboxed, it is boxed as the first step
* then, x is a list of boxes.  Each does one selection, following the spec for (x { y).  Since they are boxed selectors, the successive elements of the box specify successive axes.
* if the result of a selection is a single box, the box is opened.
* if the result of a selection is multiple boxes, no more selectors are allowed.

Henry Rich


To unsubscribe from this group and stop receiving emails from it, send an email to forum+un...@jsoftware.com.

bill lam

unread,
Sep 23, 2025, 7:52:51 PMSep 23
to fo...@jsoftware.com
This is a direct transliteration of the c code,

JSON object maps to J rank-2 array of shape n,2
key is {."1 object 
val is {:"1 object
You can locate the item
c=. (key i. <'choices') {:: val

Json array maps to J array, so that
fc=. 0{c

fc is a Json object therefore also J rank-2 array and you can access message similarly and so on.

Untested.

To unsubscribe from this group and stop receiving emails from it, send an email to forum+un...@jsoftware.com.
Reply all
Reply to author
Forward
0 new messages