1

We have some Text fields on an object that are storing values in a JSON array.
["value1", "value2"...]
We want to display these values in a custom lighting component. The natural thing to do would be:

<lightning:recordViewForm recordId="{!v.recordId}" objectApiName="MyObject__c">
    <lightning:outputField fieldName="MyJsonField__c"/>
</lightning:recordViewForm>

The problem is that there the output is in a JSON format (because that's the string in the field). We want to just display the list of values value1, value2....
Is there a way to do that in Salesforce? Something like lightning:formattedNumber but for JSON? That will work in lightning:outputField?

2

1 Answer 1

1

Lightning doesn't have the same notion of pipes or formatters that are present in other templating frameworks. You'll have to do the heavy lifting yourself. <lightning:outputField> does take the data type of the sObject field into account when it renders data, but Salesforce does not have a JSON field type - so that component doesn't know how to handle it.

Reworking to display a formatted notion of a JSON field is going to require you to go outside the <lightning:recordViewForm> layer here, and hence may cost you more code.

One possibility would be to establish a new component attribute,

<aura:attribute name="formattedJSON" type="String" />

and a change handler

<aura:handler name="change" value="{! v.ATTRIBUTE_WHERE_JSON_IS }" action="{! c.formatJSON}" />

where in your controller you consume the JSON and populate it into v.formattedJSON, which can then be rendered with <lightning:formattedText>:

formatJSON: function(component, event, handler) {
    component.set('v.formattedJSON', JSON.parse(component.get('v.ATTRIBUTE_WHERE_JSON_IS')).join(', '));
},

To have the JSON available in an attribute, you'd have to implement <force:recordData>, separately query it from the server, or take some similar tactic.

1
  • I ended up doing something similar. It's frustrating that I have these pre-built components, but I still need to write tons of code. Jan 2, 2019 at 15:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .