Skip to main content
March 29, 2022
Question

Show a JSON text in an interface

  • March 29, 2022
  • 18 replies
  • 0 views

Hi All,

We have data stored in our DB in text format. But data added can either be text or json. We would need to display the same in the interface in proper format.

That is if data is in plain text we would show it as text and if it is in JSON, we would need to format it and show.

For eg : if data is stored as "XYZ" then we would show either in Rich text editor/text field as XYZ.

If data is stored as text (in actual it is a JSON) - 

{"Field1":"Test","Field2":"value is "not editable" test it.","Field3":"314146282515","Timestamp":"2019-06-19T11:05:23.678+0000","Field4":"XYZ","Reason":"XYZ","Time":"2022-02-19T05:09:26.046+0000","Field5":"ABC","Field6":"yui","Value":"OK","Action":"","Field7":"[abcged]","Field8":"","Field9":{"Value1":"test","Value2":"Test","Value3":"Test","Value4":"Test","Value5":null,"Value6":[{"value":"C:","name":"instance"},{"value":"ty","name":"object"},{"value":"er46","name":"test"}],"Field9":300,"Periods":1,"Operator":"LessThanOrEqualTo","Field10":25.0,"Feild11":"ignore","Field12":""}}

Then it should be shown either in Rich text editor/text field as : 

Field1 : "Test"

Field2 : "value is "not editable" test it." 

so on... in dictionary format.

Please guide how to achieve the above.

    18 replies

    stefanhelzle0001
    Brainy
    March 29, 2022

    a!localVariables(
      local!json: {"Field1":"Test","Field2":"value is ""not editable"" test it.","Field3":"314146282515","Timestamp":"2019-06-19T11:05:23.678+0000","Field4":"XYZ","Reason":"XYZ","Time":"2022-02-19T05:09:26.046+0000","Field5":"ABC","Field6":"yui","Value":"OK","Action":"","Field7":"[abcged]","Field8":"","Field9":{"Value1":"test","Value2":"Test","Value3":"Test","Value4":"Test","Value5":null,"Value6":{{"value":"C:","name":"instance"},{"value":"ty","name":"object"},{"value":"er46","name":"test"}},"Field9":300,"Periods":1,"Operator":"LessThanOrEqualTo","Field10":25.0,"Feild11":"ignore","Field12":""}},
      a!forEach(
        items: a!keys(local!json),
        expression: a!textField(
          label: fv!item,
          labelPosition: "JUSTIFIED",
          value: local!json[fv!item],
          readOnly: true
        )
      )
    )

    You might have to add recursion to also display nested structures.

    March 29, 2022

    Thank you. One more question will it work for a normal text field also?
    is there any way how we can do a check whether the data is in text format or json?

    harshitb6843
    March 29, 2022

    Not a full proof idea tho but you can check if the string starts and ends with a  curly brace then it is a JSON else it is a text. 
    If that is a JSON, then you can use the fromJson() function to convert JSON to a dictionary.