json parse check the array if it is null or undefined

salu
Mega Guru

bawiskar

Hello All,

How can I parse the json and check whether it has null value or 'value' is not added..

e.g.,

When the value is null

[{"number":0,"fields":[{"min":100000,"max":9999999999,"label":"","type":"number","value":null}]},{"number":1,"fields":[{"min":100000,"max":9999999999,"label":"","type":"number","value":null}]}]

When there is no value.

[{"number":0,"fields":[{"min":100000,"max":9999999999,"label":"","type":"number"}]},{"number":1,"fields":[{"min":100000,"max":9999999999,"label":"","type":"number"}]}]

When this value is null and does not have 'value' field.

I want to make the field mandatory.

client script

$scope.mandatory_filled = function() {

  if ( $scope.table_inputs.json_output != "" && $scope.table_inputs.html_output != "" )

  {

  return true;

  }

  return false;

  };

HTML code

<span class="field-decorations">

          <span ng-show="data.is_mandatory" style="padding-right: .25em" title="${Mandatory}" class="fa fa-asterisk mandatory" ng-class="{'mandatory-filled': mandatory_filled()}" aria-hidden="false"></span>

      </span>

      {{data.title}}

Thanks

Saranya

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Saranya,



You can parse the json in client side as below:



var jsonObject = {};



jsonObject.name = "hello";


jsonObject.category = "cat1";



var jsonData = JSON.parse(jsonObject);



var asset_name = parsedData[name];


var category = parsedData[category];



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Saranya,



Please find the link below where I have provided the answer to parse the json



Parse json to incident referal record



Just use JSUtil.nil(value) it will help you to determine if it null or empty or undefined.



You cannot parse the json in client side. Send the json object to script include through ajax and after parsing the json in script include return the flag whether to make it mandatory based on the value.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Parse json to incident referal record


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Saranya,



You can parse the json in client side as below:



var jsonObject = {};



jsonObject.name = "hello";


jsonObject.category = "cat1";



var jsonData = JSON.parse(jsonObject);



var asset_name = parsedData[name];


var category = parsedData[category];



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hello Ankur,



Thanks for the respose. It got worked.



How can I check the value1 is not a duplicate?Is there any built in function for it?



In the below code am checking the value is not undefined and also value must less than 6 and greater than 8.



var json_output=this.getParameter('sysparm_json');


  var parser = new JSONParser();


  var parsedData = parser.parse(json_output);



  var length = parsedData.length; // get length


  var count=0;


  for(var i=0;i<length;i++)


  {


  var value1 = parsedData[i].fields[0].value;


  var value_length=value1.length;




  if((value1==undefined)||(value_length>10 ||value_length<6))


  {


    count=1;


  break;


  }


  else


  {



  count=0;


  }



  }



  if(count==1)


  {


  return true;   // will send to client


  }


  else


  return false;



Thanks


Saranya


Hi Saranya,



You can push the values in an array i.e. values which you receive from parsing and then use ArrayUtil function unique which will return the new array



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader