{"id":30964,"date":"2025-06-30T08:38:07","date_gmt":"2025-06-30T08:38:07","guid":{"rendered":"https:\/\/www.greytrix.com\/blogs\/sagex3\/?p=30964"},"modified":"2025-06-30T11:46:46","modified_gmt":"2025-06-30T11:46:46","slug":"how-to-read-json-using-json-parsing-4gl-functions-in-sage-x3","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/sagex3\/2025\/06\/30\/how-to-read-json-using-json-parsing-4gl-functions-in-sage-x3\/","title":{"rendered":"How to Read JSON Using JSON Parsing 4GL Functions in Sage X3"},"content":{"rendered":"\n<p>Since the 2023R2 release, <a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/development-services\/\" data-type=\"link\" data-id=\"https:\/\/www.greytrix.com\/sage-x3-erp\/development-services\/\">Sage<\/a><a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/development-services\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.greytrix.com\/sage-x3-erp\/development-services\/\" rel=\"noreferrer noopener\"> <\/a><a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/development-services\/\" data-type=\"link\" data-id=\"https:\/\/www.greytrix.com\/sage-x3-erp\/development-services\/\">X3<\/a> has introduced JSON parsing 4GL functions, enabling developers to efficiently fetch and process data from JSON-formatted strings. This blog explores the key JSON parsing functions\u2014ParseInstance, Contains$, and Select$\u2014and provides practical examples and best practices for their use.<\/p>\n\n\n\n<p><strong>Introduction to JSON Parsing in Sage X3<\/strong><\/p>\n\n\n\n<p>JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used for structured data. With Sage X3&#8217;s JSON parsing functions, you can parse JSON strings, check for specific properties, and extract values seamlessly within 4GL scripts. These functions are particularly useful for integrating external data or processing structured inputs.<\/p>\n\n\n\n<p><strong>New Stuff :-<\/strong> <a href=\"https:\/\/www.greytrix.com\/blogs\/sagex3\/2025\/05\/30\/implementing-user-specific-access-control-in-sage-x3-for-the-receipt-line-inquiry-screen\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.greytrix.com\/blogs\/sagex3\/2025\/05\/30\/implementing-user-specific-access-control-in-sage-x3-for-the-receipt-line-inquiry-screen\/\" rel=\"noreferrer noopener\">Implementing User-Specific Access Control in Sage X3 for the Receipt Line Inquiry Screen<\/a><\/p>\n\n\n\n<p><strong>Key JSON Parsing Functions<\/strong><\/p>\n\n\n\n<p><strong>1.ParseInstance<\/strong><br>The ParseInstance function is used to parse a JSON string into an object that can be queried or manipulated.<\/p>\n\n\n\n<p><strong>Syntax<\/strong>:<br>ParseInstance OBJECT With JSON_OBJECT<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>OBJECT: The object used to parse the input JSON.<\/li>\n\n\n\n<li>JSON_OBJECT: The JSON string to be parsed (recommended to use Clbfile).<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"UTF-8\">\n  <title>JSON Parser Procedure<\/title>\n  <style>\n    body {\n      background-color: #ffffff;\n      color: #000000;\n      font-family: 'Courier New', Courier, monospace;\n      padding: 20px;\n    }\n    .code-editor {\n      background: #f5f5f5;\n      border: 1px solid #ccc;\n      border-radius: 6px;\n      padding: 20px;\n      overflow-x: auto;\n      white-space: pre-wrap;\n    }\n    .comment { color: #008000; }\n    .keyword { color: #0000ff; }\n    .function { color: #795e26; }\n    .string { color: #a31515; }\n    .variable { color: #267f99; }\n  <\/style>\n<\/head>\n<body>\n\n<div class=\"code-editor\">\n<code>\n<span class=\"comment\"># Procedure to parse a JSON object<\/span>\n<span class=\"keyword\">Local<\/span> <span class=\"variable\">Clbfile<\/span> <span class=\"variable\">SRCJSON<\/span>\n<span class=\"variable\">SRCJSON<\/span> = <span class=\"string\">'{\"msg\":\"Hello World!\"}'<\/span>\n\n<span class=\"comment\"># Create the object<\/span>\n<span class=\"keyword\">Local<\/span> <span class=\"function\">Instance<\/span> <span class=\"variable\">OBJ<\/span> <span class=\"keyword\">Using<\/span> <span class=\"variable\">OBJECT<\/span>\n\n<span class=\"comment\"># Parse the object<\/span>\n<span class=\"function\">ParseInstance<\/span> <span class=\"variable\">OBJ<\/span> <span class=\"keyword\">With<\/span> <span class=\"variable\">SRCJSON<\/span>\n\n<span class=\"comment\"># Free memory<\/span>\n<span class=\"function\">FreeGroup<\/span> <span class=\"variable\">OBJ<\/span>\n<\/code>\n<\/div>\n\n<\/body>\n<\/html>\n\n\n\n<p>This code parses a simple JSON string {&#8220;msg&#8221;:&#8221;Hello World!&#8221;} into the OBJ instance for further processing.<\/p>\n\n\n\n<p><strong>2.Contains$<\/strong><br>The Contains$ function checks if a parsed JSON object contains a specific property, returning 1 if the property exists and 0 if it does not.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><br>RETURN_VALUE = JSON_OBJECT.Contains$(JSON_POINTER)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>RETURN_VALUE: Integer (1 for exists, 0 for does not exist).<\/li>\n\n\n\n<li>JSON_OBJECT: The parsed JSON object.<\/li>\n\n\n\n<li>JSON_POINTER: The JSON pointer expression to check (use square brackets for array indices).<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"UTF-8\">\n  <title>Check JSON Properties<\/title>\n  <style>\n    body {\n      background-color: #ffffff;\n      color: #000000;\n      font-family: 'Courier New', Courier, monospace;\n      padding: 20px;\n    }\n    .code-editor {\n      background: #f5f5f5;\n      border: 1px solid #ccc;\n      border-radius: 6px;\n      padding: 20px;\n      overflow-x: auto;\n      white-space: pre-wrap;\n    }\n    .comment { color: #008000; }\n    .keyword { color: #0000ff; }\n    .function { color: #795e26; }\n    .string { color: #a31515; }\n    .number { color: #098658; }\n    .variable { color: #267f99; }\n  <\/style>\n<\/head>\n<body>\n\n<div class=\"code-editor\">\n<code>\n<span class=\"comment\"># Procedure to check properties<\/span>\n<span class=\"keyword\">Local<\/span> <span class=\"variable\">Clbfile<\/span> <span class=\"variable\">SRCJSON<\/span>\n<span class=\"variable\">SRCJSON<\/span> = <span class=\"string\">'{\"msg\":\"Hello World!\", \"list\" : [\"one\", \"two\", \"three\"]}'<\/span>\n\n<span class=\"comment\"># Create the object<\/span>\n<span class=\"keyword\">Local<\/span> <span class=\"function\">Instance<\/span> <span class=\"variable\">OBJ<\/span> <span class=\"keyword\">Using<\/span> <span class=\"variable\">OBJECT<\/span>\n\n<span class=\"comment\"># Parse the object<\/span>\n<span class=\"function\">ParseInstance<\/span> <span class=\"variable\">OBJ<\/span> <span class=\"keyword\">With<\/span> <span class=\"variable\">SRCJSON<\/span>\n\n<span class=\"comment\"># Check if the JSON contains a property<\/span>\n<span class=\"keyword\">Local<\/span> <span class=\"variable\">Integer<\/span> <span class=\"variable\">R<\/span>\n<span class=\"variable\">R<\/span> = <span class=\"variable\">OBJ<\/span>.Contains$<span class=\"string\">(\"\/msg\")<\/span> <span class=\"comment\"># Returns 1<\/span>\n\n<span class=\"comment\"># Check array element<\/span>\n<span class=\"variable\">R<\/span> = <span class=\"variable\">OBJ<\/span>.Contains$<span class=\"string\">(\"\/list[1]\")<\/span> <span class=\"comment\"># Returns 1<\/span>\n\n<span class=\"comment\"># Free memory<\/span>\n<span class=\"function\">FreeGroup<\/span> <span class=\"variable\">OBJ<\/span>\n<\/code>\n<\/div>\n\n<\/body>\n<\/html>\n\n\n\n<p>This example checks for the msg property and the second element in the list array.<\/p>\n\n\n\n<p><strong>3.Select$<\/strong><br>The Select$ function retrieves the value of a specific property from a parsed JSON object.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><br>RETURN_VALUE = JSON_OBJECT.Select$(JSON_PATH)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>RETURN_VALUE: String containing the selected property\u2019s value.<\/li>\n\n\n\n<li>JSON_OBJECT: The parsed JSON object.<\/li>\n\n\n\n<li>JSON_PATH: The JSON path expression to select (use square brackets for array indices).<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><br>Procedure to select properties<\/p>\n\n\n\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"UTF-8\">\n  <title>Select JSON Properties<\/title>\n  <style>\n    body {\n      background-color: #ffffff;\n      color: #000000;\n      font-family: 'Courier New', Courier, monospace;\n      padding: 20px;\n    }\n    .code-editor {\n      background: #f5f5f5;\n      border: 1px solid #ccc;\n      border-radius: 6px;\n      padding: 20px;\n      overflow-x: auto;\n      white-space: pre-wrap;\n    }\n    .comment { color: #008000; }\n    .keyword { color: #0000ff; }\n    .function { color: #795e26; }\n    .string { color: #a31515; }\n    .number { color: #098658; }\n    .variable { color: #267f99; }\n  <\/style>\n<\/head>\n<body>\n\n<div class=\"code-editor\">\n<code>\n<span class=\"comment\"># Procedure to select properties<\/span>\n<span class=\"keyword\">Local<\/span> <span class=\"variable\">Clbfile<\/span> <span class=\"variable\">SRCJSON<\/span>\n<span class=\"variable\">SRCJSON<\/span> = <span class=\"string\">'{\"msg\":\"Hello World!\", \"list\" : [\"one\", \"two\", \"three\"]}'<\/span>\n\n<span class=\"comment\"># Create the object<\/span>\n<span class=\"keyword\">Local<\/span> <span class=\"function\">Instance<\/span> <span class=\"variable\">OBJ<\/span> <span class=\"keyword\">Using<\/span> <span class=\"variable\">OBJECT<\/span>\n\n<span class=\"comment\"># Parse the object<\/span>\n<span class=\"function\">ParseInstance<\/span> <span class=\"variable\">OBJ<\/span> <span class=\"keyword\">With<\/span> <span class=\"variable\">SRCJSON<\/span>\n\n<span class=\"comment\"># Select a property<\/span>\n<span class=\"keyword\">Local<\/span> <span class=\"variable\">Char<\/span> <span class=\"variable\">MYSTR<\/span>(30)\n<span class=\"variable\">MYSTR<\/span> = <span class=\"variable\">OBJ<\/span>.Select$<span class=\"string\">(\"$.msg\")<\/span> <span class=\"comment\"># Returns \"Hello World!\"<\/span>\n\n<span class=\"comment\"># Select array element<\/span>\n<span class=\"variable\">MYSTR<\/span> = <span class=\"variable\">OBJ<\/span>.Select$<span class=\"string\">(\"$.list[1]\")<\/span> <span class=\"comment\"># Returns \"two\"<\/span>\n\n<span class=\"comment\"># Free memory<\/span>\n<span class=\"function\">FreeGroup<\/span> <span class=\"variable\">OBJ<\/span>\n<\/code>\n<\/div>\n\n<\/body>\n<\/html>\n\n\n\n<p>This code extracts the msg property and the second element of the list array.<\/p>\n\n\n\n<p><strong>Best Practices for JSON Parsing<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To handle JSON data efficiently, especially when dealing with arrays or dynamic data, follow these best practices:<\/li>\n\n\n\n<li>Use Clbfile for JSON Strings: Store JSON strings in Clbfile variables to handle large or complex JSON data effectively.<\/li>\n\n\n\n<li>Always Free Memory: Use FreeGroup to release memory allocated to the JSON object and prevent memory leaks.<\/li>\n\n\n\n<li>Iterate Arrays with Loops: For JSON arrays, use a WHILE loop with Contains$ to dynamically check and extract elements.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example (Iterating an Array):<\/strong><\/p>\n\n\n\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"UTF-8\">\n  <title>Code Viewer<\/title>\n  <style>\n    body {\n      background-color: #ffffff; \/* White background *\/\n      color: #000000; \/* Default dark text *\/\n      font-family: 'Courier New', Courier, monospace;\n      padding: 20px;\n    }\n    .code-editor {\n      background: #f5f5f5; \/* Light gray for code block *\/\n      border: 1px solid #ccc;\n      border-radius: 6px;\n      padding: 20px;\n      overflow-x: auto;\n      white-space: pre-wrap;\n    }\n    .comment { color: #008000; }       \/* Green *\/\n    .keyword { color: #0000ff; }       \/* Blue *\/\n    .string { color: #a31515; }        \/* Dark red *\/\n    .function { color: #795e26; }      \/* Brownish *\/\n    .number { color: #098658; }        \/* Greenish *\/\n    .variable { color: #267f99; }      \/* Blue-green *\/\n  <\/style>\n<\/head>\n<body>\n\n<div class=\"code-editor\">\n<code>\n<span class=\"comment\"># Sample JSON<\/span>\n<span class=\"keyword\">Local<\/span> <span class=\"variable\">Clbfile<\/span> <span class=\"variable\">SRCJSON<\/span>\n<span class=\"variable\">SRCJSON<\/span> = <span class=\"string\">'{\"msg\":\"Hello World!\", \"list\" : [\"one\", \"two\", \"three\"]}'<\/span>\n\n<span class=\"comment\"># Create the object<\/span>\n<span class=\"keyword\">Local<\/span> <span class=\"function\">Instance<\/span> <span class=\"variable\">OBJ<\/span> <span class=\"keyword\">Using<\/span> <span class=\"variable\">OBJECT<\/span>\n\n<span class=\"comment\"># Parse the object<\/span>\n<span class=\"function\">ParseInstance<\/span> <span class=\"variable\">OBJ<\/span> <span class=\"keyword\">With<\/span> <span class=\"variable\">SRCJSON<\/span>\n\n<span class=\"comment\"># Array to store results<\/span>\n<span class=\"keyword\">Local<\/span> <span class=\"variable\">Char<\/span> <span class=\"variable\">MYSTR<\/span>(30)(0..)\n<span class=\"keyword\">Local<\/span> <span class=\"variable\">Integer<\/span> <span class=\"variable\">I<\/span> : <span class=\"variable\">I<\/span> = <span class=\"number\">0<\/span>\n\n<span class=\"comment\"># Iterate over list array<\/span>\n<span class=\"keyword\">WHILE<\/span> <span class=\"variable\">I<\/span> &gt;= <span class=\"number\">0<\/span>\n    <span class=\"comment\"># Check if the property exists<\/span>\n    <span class=\"keyword\">If<\/span> <span class=\"variable\">OBJ<\/span>.Contains$<span class=\"string\">(\"\/list\/\"<\/span> + NUM$(<span class=\"variable\">I<\/span>)) = <span class=\"number\">1<\/span>\n        <span class=\"variable\">MYSTR<\/span>(<span class=\"variable\">I<\/span>) = <span class=\"variable\">OBJ<\/span>.Select$<span class=\"string\">(\"$.list[\"<\/span> + NUM$(<span class=\"variable\">I<\/span>) + <span class=\"string\">\"]\"<\/span>)\n        <span class=\"variable\">I<\/span> += <span class=\"number\">1<\/span>\n    <span class=\"keyword\">Else<\/span>\n        BREAK\n    <span class=\"keyword\">Endif<\/span>\n<span class=\"keyword\">WEND<\/span>\n\n<span class=\"comment\"># Free memory<\/span>\n<span class=\"function\">FreeGroup<\/span> <span class=\"variable\">OBJ<\/span>\n<\/code>\n<\/div>\n\n<\/body>\n<\/html>\n\n\n\n<p>This script iterates through the list array, extracting each element until no more elements are found.<\/p>\n\n\n\n<p><strong>Conclusion<\/strong><br>Sage X3\u2019s JSON parsing 4GL functions (ParseInstance, Contains$, and Select$) provide a robust way to handle JSON data within your scripts. By following the syntax and best practices outlined above, you can efficiently parse, query, and extract data from JSON strings. Always ensure proper memory management with FreeGroup and use loops for dynamic array processing to make your code scalable and maintainable.<\/p>\n\n\n[about_us_blog_common]\n","protected":false},"excerpt":{"rendered":"<p>Since the 2023R2 release, Sage X3 has introduced JSON parsing 4GL functions, enabling developers to efficiently fetch and process data from JSON-formatted strings. This blog explores the key JSON parsing functions\u2014ParseInstance, Contains$, and Select$\u2014and provides practical examples and best practices for their use. Introduction to JSON Parsing in Sage X3 JSON (JavaScript Object Notation) is\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/sagex3\/2025\/06\/30\/how-to-read-json-using-json-parsing-4gl-functions-in-sage-x3\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,13],"tags":[4391,4386,4389,4387,4385,4388,4384,4390],"class_list":["post-30964","post","type-post","status-publish","format-standard","hentry","category-sage-erp-x3","category-integration","tag-best-practices-for-json-parsing-in-sage-x3","tag-contains-in-sagex3","tag-json-functionality-in-sage-x3","tag-json-in-sage-x3","tag-json-parsing","tag-parseinstance-object-in-sage-x3","tag-read-json-in-sage-x3","tag-select-in-sage-x3"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/posts\/30964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/comments?post=30964"}],"version-history":[{"count":14,"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/posts\/30964\/revisions"}],"predecessor-version":[{"id":31013,"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/posts\/30964\/revisions\/31013"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/media?parent=30964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/categories?post=30964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/sagex3\/wp-json\/wp\/v2\/tags?post=30964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}