1

I am parsing the incoming JSON and checking for a surname value. If it starts with 'C' then response should be sent back, else reject it.

{

"KeyPersons": [{
    "Person": {
        "KeyPersonType": "D",
        "Forename": "user502",
        "Surname": "C",
        "DateOfBirth": "1995-09-14",
        "Gender": "M"
    }

How do I parse this JSON using datapower gateway script?

I tried the below and it didnt work.

session.input.readAsJSON(function (error, json)


if(json.KeyPersons.Person.Surname == 'C')

 session.output.write(json);

 
abhishek p
  • 11
  • 1

1 Answers1

0

KeyPersons is an array so you'd need an index for it and also in case of checking multiple instances you'd need to loop them.

Also if you want to check the first letter only your code won't work:

if(json.KeyPersons[0].Person.Surname.slice(0, 1) === 'C')
  session.output.write(json);

It might also be a good idea to check for existence of the object, e.g.:

if(json.KeyPersons && json.KeyPersons.length
    && json.KeyPersons[0].Person
    && json.KeyPersons[0].Person.Surname
    && json.KeyPersons[0].Person.Surname.slice(0, 1) === 'C')
  session.output.write(json);
Anders
  • 3,198
  • 1
  • 20
  • 43