Color Formatting Of A SharePoint Online List Using JSON

Here, we are going to see how to perform conditional formatting using JSON in a SharePoint online list. Nowadays, many people are seeking for a code to format the SharePoint list to make it more attractive to the end users.

In this way, I just tried to format my list items with some color code and here is my code by which the background color will get changed based on the item values.
 
Here, I have created one list with one column called colors and I have entered some color name, so based on the color name, it will change the background color.
 
Here is the JSON code to change the background color and text color as well. You can just copy and paste it.
  1. {  
  2.   "elmType": "div",  
  3.   "txtContent": "@currentField",  
  4.   "style": {  
  5.     "color": "#fff",  
  6.     "padding-left": "14px",  
  7.     "background-color": {  
  8.       "operator": "?",  
  9.       "operands": [  
  10.         {  
  11.           "operator": "==",  
  12.           "operands": [  
  13.             "@currentField",  
  14.             "Green"  
  15.           ]  
  16.         },  
  17.         "#2ECC71",  
  18.         {  
  19.           "operator": "?",  
  20.           "operands": [  
  21.             {  
  22.               "operator": "==",  
  23.               "operands": [  
  24.                 "@currentField",  
  25.                 "Red"  
  26.               ]  
  27.             },  
  28.             "#E74C3C",  
  29.             {  
  30.               "operator": "?",  
  31.               "operands": [  
  32.                 {  
  33.                   "operator": "==",  
  34.                   "operands": [  
  35.                     "@currentField",  
  36.                     "Yellow"  
  37.                   ]  
  38.                 },  
  39.                 "#F1C40F",  
  40.                 {  
  41.                   "operator": "?",  
  42.                   "operands": [  
  43.                     {  
  44.                       "operator": "==",  
  45.                       "operands": [  
  46.                         "@currentField",  
  47.                         "Purple"  
  48.                       ]  
  49.                     },  
  50.                     "#76448A",  
  51.                     ""  
  52.                   ]  
  53.                 }  
  54.               ]  
  55.             }  
  56.           ]  
  57.         }  
  58.       ]  
  59.     }  
  60.   }  
  61. }  
You can add the JSON code in that particular column in two ways,
  • Navigate to list setting -> column -> then add JSON code
  • You can click the title of column in list view itself then column setting and format this column and JSON code editor will open on  the right of the screen
 And the final view of the list would be like below.