From 14053777b30141e637f9a011c7f67a7552ae085e Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Tue, 20 Feb 2024 15:50:11 +0530 Subject: [PATCH] return value of yaml and json decode functions in kcl Signed-off-by: d4v1d03 --- .../user_docs/support/faq-kcl.md | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/versioned_docs/version-0.7.0/user_docs/support/faq-kcl.md b/versioned_docs/version-0.7.0/user_docs/support/faq-kcl.md index 88e09382..cfe5dc9a 100644 --- a/versioned_docs/version-0.7.0/user_docs/support/faq-kcl.md +++ b/versioned_docs/version-0.7.0/user_docs/support/faq-kcl.md @@ -2463,7 +2463,7 @@ a: 1 ## 56. How to convert JSON structures into schemas? -To convert a piece of JSON code or code structured in the JSON format, we import the built-in `json` module of KCL and use the json.decode function. In the KCL code below, we have used the `Server` schema to directly verify the integrated JSON data. +To convert a piece of JSON code or code structured in the JSON format, we import the built-in `json` module of KCL and use the `json.decode` function. In the KCL code below, we have used the `Server` schema to directly verify the integrated JSON data. ```KCL import json @@ -2483,4 +2483,23 @@ server: - 8080 ``` - \ No newline at end of file +## 57. What type of return value is json.decode() or yaml.decode() in KCL? + +In KCL, when we use the built-in modules `json` or `yaml` for the functions `json.decode()` or `yaml.decode()`, the return type is 'any' which can be of any type according to our wish. Therefore, we use type annotation or any other method such as using the `Schema` syntax in the below KCL code to ensure type safety. + +```KCL +import yaml + +schema Server: + ports: [int] + +server: Server = yaml.decode("""\ +ports: +- 80 +- 8080 +""") +``` + + + +