### Use Settings JSON Format

Example of reading settings from a JSON file.
This example based on home automation solution Domoticz (www.domoticz.com) which outputs device information in JSOM format.
[
{"idx":1,"keys":"v1"},
{"idx":2,"keys":"v1,v2"},
{"idx":3,"keys":"v1,v2,v3"}
]
B4J Code Snippets
`Sub Process_Globals
Private fx As JFX
Private MainForm As Form
'Define the settings file in json format
Private SettingsFile As String = "settings.json"
Private SettingsList As List
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
...
AppInit
End Sub`
`Sub AppInit
If ReadSettings Then
Log("List the setting keys for index 2 (idx=2):")
Dim l As List = GetSettingsKeys(2)
If l.Size > 0 Then
For Each s As String In l
Log(s)
Next
End If
Log("Check the settings key v3 for index 3:")
Log(GetSettingsKey(3, "v3"))
Log("Get content for index 2 and key v2:")
Log(GetSettingsValue($"{"idx":2, "v2":"Value of v2 for idx 2"}"$, 2, "v3"))
End If
End Sub`
'Read the settings from the settings file containing json string.
'Return true if file read and parsed else false.
`Sub ReadSettings As Boolean
Dim Result As Boolean
' Read the content of the settings file to a string and parse
Dim Parser As JSONParser
Try
Parser.Initialize(File.ReadString(File.DirApp, SettingsFile))
SettingsList = Parser.NextArray
Result = True
Catch
Log($"Error reading settings: ${LastException}"$)
Result = True
End Try
Return Result
End Sub`
'Get the settings keys as list for an index. Example:
'Dim l As List = GetSettingsKeys(2)
'If l.Size > 0 Then
' For Each s As String In l
' Log(s)
' Next
'End If
`Sub GetSettingsKeys(idx As Int) As List
Dim Result As List
Result.Initialize
For Each colroot As Map In SettingsList
If colroot.Get("idx") = idx Then
Dim keys As String = colroot.Get("keys")
If Not(keys.EndsWith(",") ) Then keys = keys & ","
Dim k() As String = Regex.Split(",", keys)
Result.AddAll(k)
End If
Next
Return Result
End Sub`
'Get the settings keys as list for an index. Example:
'Dim l As List = GetSettingsKeys(2)
'If l.Size > 0 Then
' For Each s As String In l
' Log(s)
' Next
'End If
`Sub GetSettingsKey(idx As Int, key As String) As String
Dim Result As String = "not found"
For Each colroot As Map In SettingsList
If colroot.Get("idx") = idx Then
Dim keys As String = colroot.Get("keys")
If Not(keys.EndsWith(",") ) Then keys = keys & ","
Dim k() As String = Regex.Split(",", keys)
For Each s As String In k
If s.EqualsIgnoreCase(key) Then
Result = s
Exit
End If
Next
End If
Next
Return Result
End Sub`
'Get the settings value for an index from a JSON string
'Example: {"idx":2, "v2":"Value of v2 for idx 2"}
`Sub GetSettingsValue(content As String, idx As Int, key As String) As String
Dim Result As String
Dim Parser As JSONParser
Parser.Initialize(content)
Dim root As Map = Parser.NextObject
If root.Get("idx") = idx Then Result = root.Get(key)
If Result.EqualsIgnoreCase("null") Then Result = "not found"
Return Result
End Sub`
Example
JSON file settings.json in the dirapp folder
[
{"idx":1,"keys":"v1"},
{"idx":2,"keys":"v1,v2"},
{"idx":3,"keys":"v1,v2,v3"}
]
Output from above code
List the setting keys for index 2 (idx=2):
v1
v2
Check the settings key v3 for index 3:
v3
Get content for index 2 and key v2:
Value of v2 for idx 2

Changed: , Category: JSON, File: T6C210M1541.htm, ID: 1541