• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
      • Introduction to CSPro Language
      • CSPro Program Structure
      • Programming Standards
      • Change Code Properties
      • Code Folding
      • Debugging CSPro Applications
      • Declaration Section
      • Procedural Sections
      • Logic
      • Language Elements
      • JavaScript Use in CSPro
        • Overview
        • JS Namespace
        • JS.eval Function
        • JS.invoke Function
        • JS.getValue Function
        • JS.getValueJson Function
        • JS.hasValue Function
        • JS.setValue Function
        • JS.setValueFromJson Function
        • JavaScript User-Defined Functions
        • CSPro ⇄ JavaScript Value Conversions
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • Data Sources
    • CSPro Statements and Functions
    • Text Templates
    • Templated Reporting System
    • HTML, Markdown, and JavaScript Integration
    • Action Invoker
    • Appendix
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataManager>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

JS.setValueFromJson Function

Format
b = JS.setValueFromJson(name, json);
Description
The JS.setValueFromJson function sets the value of a JavaScript variable or property from JSON representation passed to the function as the string expression json. The JSON is converted to a value or object using JSON.parse. This is a useful way to transfer data from CSPro logic to JavaScript if the data cannot be represented by a CSPro object that can be handled by the JS.setValue function. This function can be used to create new variables or properties, or to modify existing ones.
The string expression name is evaluated and can refer to a variable in CSPro's JavaScript global execution context or an object's property if the object exists globally. For example, the following values can be specified as a name:
  • myFunction
  • globalThis.myFunction
  • myObject.myProperty
  • myObject['my' + 'Property']
Return Value
The function returns a logical value of 1 (true) if the JSON is valid and the variable or property is successfully set. If JSON parsing or JavaScript evaluation ends in an uncaught exception, the function returns 0 (false) and displays an error message.
Example
JS.setValueFromJson("france", ' { "name": "France",        '
                             
'   "capital": "Paris",      '
                             
'   "currency": "Euro",      '
                             
'   "republicNumber": 5,     '
                             
'   "languages": ["French"] }');

JS.setValueFromJson("france.area", "643801");

JS.setValueFromJson("csproVersion", "8.1");

// displays: Using CSPro 8.1, I learned that France has an area of 643801 km²!
errmsg("%s", JS.eval("`Using CSPro ${csproVersion.toFixed(1)}, "
                     
"I learned that ${france.name} has an area of ${france.area} km²!`"));
See also: JavaScript Use in CSPro, JS Namespace, JS.getValue Function, JS.hasValue Function, JS.setValueFromJson Function