Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
graphical tool for display the JSON structure
#1
Background:
In some cases, I've noticed that the JSON returned after sending an HTTP request has a structure that is too deeply nested. It's a headache to extract the value of a particular node.

Suggestion:
Could a graphical tool be created in LA to intuitively display the JSON structure?
Maybe even right-clicking on a node to generate some common code snippets from the options available?
A tool similar to the one in the link: https://embed-ssl.wistia.com/deliveries/...ccd19d.mp4
Create a dump function for the JSON object, which will display the JSON structure when executed, as shown in the image below.
   

For example, the JSON text returned after executing the following LA code is as follows:
 
Code:
Copy      Help
var j = internet.http.Post(uri, internet.jsonContent(body), [$"Authorization: Bearer {token}"]).Json();
print.it(j);

LA Output:  
 
Code:
Copy      Help
[messages, [
  {
    "role": "assistant",
    "type": "verbose",
    "content": "{\u0022msg_type\u0022:\u0022knowledge_recall\u0022,\u0022data\u0022:\u0022{\\\u0022prompt\\\u0022:\\\u0022\\\\n# \u56DE\u7B54\u89C4\u5219\\\\n\u6839\u636E\u5F15\u7528\u7684\u5185\u5BB9\u56DE\u7B54\u95EE\u9898:\\\\n1.\u5982\u679C\u5F15\u7528\u7684\u5185\u5BB9\u91CC\u9762\u5305\u542B \\\\u003cimg src=\\\\\\\u0022\\\\\\\u0022\\\\u003e \u7684\u6807\u7B7E, \u6807\u7B7E\u91CC\u7684 src \u5B57\u6BB5\u8868\u793A\u56FE\u7247\u5730\u5740, \u53EF\u4EE5\u5728\u56DE\u7B54\u95EE\u9898\u7684\u65F6\u5019\u5C55\u793A\u51FA\u53BB, \u8F93\u51FA\u683C\u5F0F\u4E3A\\\\\\\u0022![\u56FE\u7247\u540D\u79F0](\u56FE\u7247\u5730\u5740)\\\\\\\u0022 \\\\n2.\u5982\u679C\u5F15\u7528\u7684\u5185\u5BB9\u4E0D\u5305\u542B \\\\u003cimg src=\\\\\\\u0022\\\\\\\u0022\\\\u003e \u7684\u6807\u7B7E, \u4F60\u56DE\u7B54\u95EE\u9898\u65F6\u4E0D\u9700\u8981\u5C55\u793A\u56FE\u7247 \\\\n\\\\n# \u5F15\u7528\u7684\u5185\u5BB9 \\\\nsection 1 start:\\\\n \u83B7\u53D6\u7535\u8111\u9ED8\u8BA4\u6253\u5370\u673A\\\\nGet-CimInstance -ClassName Win32_Printer | Where-Object Default -eq $true | Select-Object Name\\\\n\\\\nsection 1 ends.\\\\n\\\u0022,\\\u0022chunks\\\u0022:[{\\\u0022slice\\\u0022:\\\u0022 \u83B7\u53D6\u7535\u8111\u9ED8\u8BA4\u6253\u5370\u673A\\\\nGet-CimInstance -ClassName Win32_Printer | Where-Object Default -eq $true | Select-Object Name\\\\n\\\u0022,\\\u0022score\\\u0022:0.935546875,\\\u0022meta\\\u0022:{\\\u0022dataset\\\u0022:{\\\u0022id\\\u0022:\\\u00227373435684420698118\\\u0022,\\\u0022name\\\u0022:\\\u0022powershell\u547D\u4EE4\u4EE3\u7801\\\u0022},\\\u0022document\\\u0022:{\\\u0022id\\\u0022:\\\u00227373435684420714502\\\u0022,\\\u0022name\\\u0022:\\\u0022Powershell.txt\\\u0022,\\\u0022format_type\\\u0022:0,\\\u0022source_type\\\u0022:0},\\\u0022link\\\u0022:{\\\u0022title\\\u0022:\\\u0022powershell\u547D\u4EE4\u4EE3\u7801_Powershell.txt\\\u0022,\\\u0022url\\\u0022:\\\u0022\\\u0022}}}]}\u0022}",
    "content_type": "text"
  },
  {
    "role": "assistant",
    "type": "function_call",
    "content": "{\u0022name\u0022:\u0022psCall\u0022,\u0022arguments\u0022:{\u0022code\u0022:\u0022Get-CimInstance -ClassName Win32_Printer | Where-Object Default -eq $true | Select-Object Name\u0022},\u0022plugin_id\u0022:8,\u0022api_id\u0022:8,\u0022plugin_type\u0022:1}",
    "content_type": "text"
  },
  {
    "role": "assistant",
    "type": "tool_response",
    "content": "{\u0022msg\u0022:\u0022Name\\r\\n----\\r\\nHP418WiFi\u0022,\u0022code\u0022:200}",
    "content_type": "text"
  },
  { 
    "role": "assistant",
    "type": "answer",
    "content": "\u4F60\u7535\u8111\u4E0A\u7684\u9ED8\u8BA4\u6253\u5370\u673A\u662F\uFF1AHP418WiFi\u3002",
    "content_type": "text"
  },
  {
    "role": "assistant",
    "type": "verbose",
    "content": "{\u0022msg_type\u0022:\u0022generate_answer_finish\u0022,\u0022data\u0022:\u0022\u0022}",
    "content_type": "text"
  }
]]
[conversation_id, none]
[code, 0]
[msg, success]

Below is the display effect after formatting with an online JSON tool, which seems more intuitive. My idea is to display the following text in a tree-like structure
 
Code:
Copy      Help
[messages, [
  {
    "role": "assistant",
    "type": "verbose",
    "content": {
      "msg_type": "knowledge_recall",
      "data": {
        "prompt": "\n# 回答规则\n根据引用的内容回答问题:\n1. 如果引用的内容里面包含 <img src=\"\"> 的标签, 标签里的 src 字段表示图片地址, 可以在回答问题的时候展示出去, 输出格式为\"![图片名称](图片地址)\"\n2. 如果引用的内容不包含 <img src=\"\"> 的标签, 你回答问题时不需要展示图片 \n\n# 引用的内容 \nsection 1 start:\n 获取电脑默认打印机\nGet-CimInstance -ClassName Win32_Printer | Where-Object Default -eq $true | Select-Object Name\n\nsection 1 ends.\n",
        "chunks": [
          {
            "slice": " 获取电脑默认打印机\nGet-CimInstance -ClassName Win32_Printer | Where-Object Default -eq $true | Select-Object Name\n",
            "score": 0.935546875,
            "meta": {
              "dataset": {
                "id": "8",
                "name": "powershell命令代码"
              },
              "document": {
                "id": "8",
                "name": "Powershell.txt",
                "format_type": 0,
                "source_type": 0
              },
              "link": {
                "title": "powershell命令代码_Powershell.txt",
                "url": ""
              }
            }
          }
        ]
      }
    },
    "content_type": "text"
  },
  {
    "role": "assistant",
    "type": "function_call",
    "content": {
      "name": "psCall",
      "arguments": {
        "code": "Get-CimInstance -ClassName Win32_Printer | Where-Object Default -eq $true | Select-Object Name"
      },
      "plugin_id": 8,
      "api_id": 8,
      "plugin_type": 1
    },
    "content_type": "text"
  },
  {
    "role": "assistant",
    "type": "tool_response",
    "content": {
      "msg": "Name\r\n----\r\nHP418WiFi",
      "code": 200
    },
    "content_type": "text"
  },
  {
    "role": "assistant",
    "type": "answer",
    "content": "你电脑上的默认打印机是:HP418WiFi。",
    "content_type": "text"
  },
  {
    "role": "assistant",
    "type": "verbose",
    "content": {
      "msg_type": "generate_answer_finish",
      "data": ""
    },
    "content_type": "text"
  }
]]
[conversation_id, none]
[code, 0]
[msg, success]


Suggestion: When I right-click on a menu item in the context menu below the content node, I can generate C# code to retrieve the text of that node.

    "role": "assistant",
    "type": "answer",
    "content": "你电脑上的默认打印机是:HP418WiFi。",

By the way, is there a simple way to retrieve the value of the content node in the LA library?
#2
To print JSON and select a node I use this code

Code:
Copy      Help
using System.Text.Json.Nodes;

print.clear();

var j = JsonNode.Parse("""
[
  {
    "role": "assistant",
    "type": "verbose",
    "content": {
      "msg_type": "knowledge_recall",
      "data": {
        "prompt": "\n# 回答规则\n根据引用的内容回答问题:\n1. 如果引用的内容里面包含 <img src=\"\"> 的标签, 标签里的 src 字段表示图片地址, 可以在回答问题的时候展示出去, 输出格式为\"![图片名称](图片地址)\"\n2. 如果引用的内容不包含 <img src=\"\"> 的标签, 你回答问题时不需要展示图片 \n\n# 引用的内容 \nsection 1 start:\n 获取电脑默认打印机\nGet-CimInstance -ClassName Win32_Printer | Where-Object Default -eq $true | Select-Object Name\n\nsection 1 ends.\n",
        "chunks": [
          {
            "slice": " 获取电脑默认打印机\nGet-CimInstance -ClassName Win32_Printer | Where-Object Default -eq $true | Select-Object Name\n",
            "score": 0.935546875,
            "meta": {
              "dataset": {
                "id": "8",
                "name": "powershell命令代码"
              },
              "document": {
                "id": "8",
                "name": "Powershell.txt",
                "format_type": 0,
                "source_type": 0
              },
              "link": {
                "title": "powershell命令代码_Powershell.txt",
                "url": ""
              }
            }
          }
        ]
      }
    },
    "content_type": "text"
  },
  {
    "role": "assistant",
    "type": "function_call",
    "content": {
      "name": "psCall",
      "arguments": {
        "code": "Get-CimInstance -ClassName Win32_Printer | Where-Object Default -eq $true | Select-Object Name"
      },
      "plugin_id": 8,
      "api_id": 8,
      "plugin_type": 1
    },
    "content_type": "text"
  },
  {
    "role": "assistant",
    "type": "tool_response",
    "content": {
      "msg": "Name\r\n----\r\nHP418WiFi",
      "code": 200
    },
    "content_type": "text"
  },
  {
    "role": "assistant",
    "type": "answer",
    "content": "你电脑上的默认打印机是:HP418WiFi。",
    "content_type": "text"
  },
  {
    "role": "assistant",
    "type": "verbose",
    "content": {
      "msg_type": "generate_answer_finish",
      "data": ""
    },
    "content_type": "text"
  }
]
"""
);

print.it(j.ToJsonString2());

var j2 = j[0]["content"];
print.it(j2.ToJsonString2());

var s = (string)j[0]["content"]["msg_type"];
print.it(s);

var d = (double)j[0]["content"]["data"]["chunks"][0]["score"];
print.it(d);



static class C {
    public static string ToJsonString2(this System.Text.Json.Nodes.JsonNode t) => t.ToJsonString(s_jso1);
    
    static System.Text.Json.JsonSerializerOptions s_jso1 = new() { WriteIndented = true, Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
}
#3
Thanks for your help.
I used the following method to get the value I needed, but it might fail in some cases, such as when the order of arrays in the JSON string changes.
Displaying and processing JSON objects, arrays, and strings is something that almost all programming languages need to handle. It seems necessary to provide a tool to address this issue.
 
Code:
Copy      Help
        var j = internet.http.Post(uri, internet.jsonContent(body), [$"Authorization: Bearer {token}"]).Json();
        var messagesArray = (JsonArray)j["messages"];
        var firstMessageObject = (JsonObject)messagesArray[3];
        string r = (string)firstMessageObject["content"];
        print.it(r);


Forum Jump:


Users browsing this thread: 1 Guest(s)