SOLVED

JSON if statement for conditional formatting

%3CLINGO-SUB%20id%3D%22lingo-sub-2263010%22%20slang%3D%22en-US%22%3EJSON%20if%20statement%20for%20conditional%20formatting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2263010%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20new%20at%20JSON%20and%20would%20like%20some%20help%20on%20how%20to%20write%20an%20if%20statement%20for%20conditional%20formatting%20in%20a%20SharePoint%20list%20that%20will%20change%20a%20columns%20background%20(%40currentField)%20color%20if%20the%20Position%20columns%20item%20is%20selected%20as%20Chief%20Cook%2C%20Cook%2C%20or%20Steward%20.%20%3Dif(%5B%24Position%5D%20%3D%3D%20'Chief%20Cook%20or%20Cook%20or%20Steward'%2C%20'%2300afff'%2C%20'')%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20JSON%20below%20I%20found%20on%20the%20web%20works%20when%20only%20'one'%20choice%20is%20entered.%20Is%20there%20a%20way%20to%20use%20a%20character%20to%20include%20the%20other%202%20choices%20in%20the%20syntax%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%7B%3CBR%20%2F%3E%22elmType%22%3A%20%22div%22%2C%3CBR%20%2F%3E%22txtContent%22%3A%20%22%40currentField%22%2C%3CBR%20%2F%3E%22style%22%3A%20%7B%3CBR%20%2F%3E%22background-color%22%3A%20%22%3Dif(%5B%24Position%5D%20%3D%3D%20'Cook'%2C%20'%2300afff'%2C%20'')%22%2C%3CBR%20%2F%3E%22color%22%3A%20%22%3Dif(%40currentField%20%26gt%3B%3D%20%40now%20-%2094672800000%20%2C'green'%2C%20(if(%40currentField%20%26gt%3B%3D%20%40now%20-%2094672800000%2C%20'%23ffa59b'%2C'red'))%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20would%20be%20appreciated.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2263010%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2263697%22%20slang%3D%22en-US%22%3ERe%3A%20JSON%20if%20statement%20for%20conditional%20formatting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2263697%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1020606%22%20target%3D%22_blank%22%3E%40JTFInc%3C%2FA%3E%26nbsp%3Byou%20need%20to%20use%20%7C%7C%20which%20stands%20for%20%3CEM%3Eor%3C%2FEM%3E%20(%26amp%3B%26amp%3B%20is%20%3CEM%3Eand%3C%2FEM%3E)%20as%20follows%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%7B%0A%20%20%22%24schema%22%3A%20%22https%3A%2F%2Fdeveloper.microsoft.com%2Fjson-schemas%2Fsp%2Fcolumn-formatting.schema.json%22%2C%0A%20%20%22elmType%22%3A%20%22div%22%2C%0A%20%20%22txtContent%22%3A%20%22%40currentField%22%2C%0A%20%20%22style%22%3A%20%7B%0A%20%20%20%20%22padding-left%22%3A%20%2214px%22%2C%0A%20%20%20%20%22background-color%22%3A%20%22%3Dif(%5B%24Position%5D%20%3D%3D%20'Chief%20Cook'%20%7C%7C%20%5B%24Position%5D%20%3D%3D'Cook'%20%7C%7C%20%5B%24Position%5D%20%3D%3D%20'Steward'%20%20%2C%20'%2300afff'%2C%20'%23d7dae1')%22%2C%0A%20%20%20%20%22color%22%3A%20%22black%22%0A%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20used%20this%20JSON%20to%20format%20the%20Contract%20column%20in%20the%20image%20below%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%220-SP-list.png%22%20style%3D%22width%3A%20855px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F271466i8419BAD8FCD2C3FB%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%220-SP-list.png%22%20alt%3D%220-SP-list.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERob%3CBR%20%2F%3ELos%20Gallardos%3CBR%20%2F%3E%3CFONT%20size%3D%222%22%20color%3D%22%23006400%22%3EMicrosoft%20Power%20Automate%20Community%20Super%20User%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2580280%22%20slang%3D%22en-US%22%3ERe%3A%20JSON%20if%20statement%20for%20conditional%20formatting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2580280%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F174092%22%20target%3D%22_blank%22%3E%40RobElliott%3C%2FA%3E%26nbsp%3BHi.%20I%20attempted%20this%20but%20I%20got%20all%20the%20same%20color%20as%20my%20background.%26nbsp%3B%20Is%20this%20possible%20to%20use%20on%20a%20calculated%20column%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

I am new at JSON and would like some help on how to write an if statement for conditional formatting in a SharePoint list that will change a columns background (@currentField) color if the Position columns item is selected as Chief Cook, Cook, or Steward . =if([$Position] == 'Chief Cook or Cook or Steward', '#00afff', '')

 

The JSON below I found on the web works when only 'one' choice is entered. Is there a way to use a character to include the other 2 choices in the syntax?

 

 

{
"elmType": "div",
"txtContent": "@currentField",
"style": {
"background-color": "=if([$Position] == 'Cook', '#00afff', '')",
"color": "=if(@currentField >= @now - 94672800000 ,'green', (if(@currentField >= @now - 94672800000, '#ffa59b','red'))"
}
}

 

Any help would be appreciated.

4 Replies
best response confirmed by JTFInc (New Contributor)
Solution

@JTFInc you need to use || which stands for or (&& is and) as follows:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "style": {
    "padding-left": "14px",
    "background-color": "=if([$Position] == 'Chief Cook' || [$Position] =='Cook' || [$Position] == 'Steward'  , '#00afff', '#d7dae1')",
    "color": "black"
  }
}

 

I've used this JSON to format the Contract column in the image below:

 

0-SP-list.png

 

Rob
Los Gallardos
Microsoft Power Automate Community Super User

Rob your a champion! That did the trick. I was playing with all kinds of operands and could not make it work. Thanks again for your help. Your awesome.
Regards
Jay

@RobElliott Hi. I attempted this but I got all the same color as my background.  Is this possible to use on a calculated column?

i have a column approver2 with the value of the name in it.
there is also a approver1.
if approver2 rejected, the name should be bold, if approver1 rejected name sould be normal.

is that possible?



thanks kuno