Validating JSON?

IamAmolB
Tera Guru

Hi Experts,

I am trying to find out if the JSON we are getting from REST is validate or not?

we are getting few rows in JSON response but one of the row is terminated incorrectly, if i want to do some validation from my side do we have any solution for this?

Regards,

Amol Bavaskar

10 REPLIES 10

Deepak Ingale1
Mega Sage

Hi Amol,



Could you please explain what do you mean by "terminated". Does it mean some properties are missing?



if that is the case, iterator over properties might be helpful to check if property exists against object or not.



somethins like if ( typeof objectOfYours['propertyName'] != undefined )


IamAmolB
Tera Guru

Hi Deepak,



It is like



{


"RequestDetails": {


"ROW": [


{


"REQUEST_ID": "94092",


"CCR_TRANS_ID": "null",


"REQUEST_VERSION_ID": "796231",


"RESERVATION_ID": "1079056",


"REQ_STATUS": "Confirm",


"REQUEST_TYP_NM": "STANDARD TEST",


"CUSTOMER_ID": "18910",


"CUSTOMER_NM": "ITSCE TMT ("CO",


"START_DATE": "09/25/2017",


"START_TIME": "8",


"TIME_ZONE_TEST": "EST",


"EARLIEST_CONFIRM_OR_HELD_DATE": "11/10/2016",


    "ARS_R2C_SR": "n",


"ARS_R2C_SRM": "n",


"ARS_R2C_EVAULT": "n",



},


{


"REQUEST_ID": "94092",


"CCR_TRANS_ID": "null",


"REQUEST_VERSION_ID": "796231",


"RESERVATION_ID": "1079056",


"REQ_STATUS": "Confirm",


"REQUEST_TYP_NM": "STANDARD TEST",


"CUSTOMER_ID": "18910",


"CUSTOMER_NM": "ITSCE TMT ("CO")",


"START_DATE": "09/25/2017",


"START_TIME": "8",


"TIME_ZONE_TEST": "EST",


"EARLIEST_CONFIRM_OR_HELD_DATE": "11/10/2016",


    "ARS_R2C_SR": "n",


"ARS_R2C_SRM": "n",


"ARS_R2C_EVAULT": "n",



}, {


"REQUEST_ID": "94092",


"CCR_TRANS_ID": "null",


"REQUEST_VERSION_ID": "796231",


"RESERVATION_ID": "1079056",


"REQ_STATUS": "Confirm",


"REQUEST_TYP_NM": "STANDARD TEST",


"CUSTOMER_ID": "18910",


"CUSTOMER_NM": "ITSCE TMT ("CO")",


"START_DATE": "09/25/2017",


"START_TIME": "8",


"TIME_ZONE_TEST": "EST",


"EARLIEST_CONFIRM_OR_HELD_DATE": "11/10/2016",


    "ARS_R2C_SR": "n",


"ARS_R2C_SRM": "n",


"ARS_R2C_EVAULT": "n",



}, {


"REQUEST_ID": "94092",


"CCR_TRANS_ID": "null",


"REQUEST_VERSION_ID": "796231",


"RESERVATION_ID": "1079056",


"REQ_STATUS": "Confirm",


"REQUEST_TYP_NM": "STANDARD TEST",


"CUSTOMER_ID": "18910",


"CUSTOMER_NM": "ITSCE TMT ("CO")",


"START_DATE": "09/25/2017",


"START_TIME": "8",


"TIME_ZONE_TEST": "EST",


"EARLIEST_CONFIRM_OR_HELD_DATE": "11/10/2016",


    "ARS_R2C_SR": "n",


"ARS_R2C_SRM": "n",


"ARS_R2C_EVAULT": "n",



}



]}


}



this is my JSON response which is terminated incorrectly as highlighted so at the time of processing it is escaping the records below that.



i want to do some exception handling at our side how can we do that?


Hi Amol,



Following links might help you:


JSON Object validation in Javascript - Stack Overflow


How to check if a string is a valid JSON string in JavaScript without using Try/Catch - Stack Overfl...


javascript - How to test if a string is JSON or not? - Stack Overflow



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

Hi Ankur,



I already gone through these links. it is not applicable in my case.


or if you have any idea how can we parse can you please explain it will be more appreciated.



Regards,


Amol