1

Please can you help me with a problem I have, using JSON to customize a view?

I'm creating a glossary, using Lists. The List contains all the metadata for a term – then I'm using the list web part to show some of the fields in a SharePoint page.

If the term has an acronym, I want it to appear in brackets next to the term. So this is what I've got so far:

{
            "elmType": "div",
            "attributes": {
              "class": "sp-row-title"
            },
            "txtContent": "=[$Title] + '(' + [$field_1] + ')'"
          }

field_1  contains the acronym.

But I only want field_1, and the brackets surrounding it, to display if it contains a value.

E.g. at the moment, the code makes this happen:

Protected Designation of Origin (PDO)

Cheese (-)

– where "-" represents a null value in field_1.

So to the question... Do you know if there's any JSON syntax I can use, to make a field and some surrounding text appear conditionally, if the field contains a value?

I've looked at the code samples in the list formatting samples library – and there seems to be a way to do it, using =if, but the samples are all for conditional formatting, like changing the color, rather than showing or hiding text.

Thanks for your help!

1 Answer 1

1

Try using JSON expression like this:

{
    "elmType": "div",
    "attributes": {
        "class": "sp-row-title"
    },
    "txtContent": "=[$Title] + if([$field_1] == '' || [$field_1] == '-', '', '(' + [$field_1] + ')')"
}

Documentation: JSON formatting syntax reference

1
  • Thank you Ganesh, that worked perfectly! And what a lightning response. Many thanks indeed.
    – danhowarth
    Sep 5, 2022 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.