-1

I am trying to validate a JSON which has Optional keys depending on a condition. Json response is as follows:

{ "hits": [
            {
               
                "_type": "sessions",
                "_source": {
                    "movie_name": "The Greatest Showman (U/A) - English",
                    "session_data": {
                        "freeSeating": false,
                        "mandatoryItems": [],
                        "areas": [
                            {
                                "seatingType": "fixed"
                            },
                            {
                                "seatingType": "free"
                            },
                            {
                                "seatingType": "mixed"
                               
                            }
                        ]
                    }
                }
            },
            { 
                "_type": "sessions",
                "_source": { 
                    "movie_name": "The Greatest Showman (U/A) - English",
                    "session_data": {
                        "freeSeating": false,
                        "mandatoryItems": [],
                        "areas": [
                            {
                                "seatingType": "fixed"   
                            },
                            {
                                "seatingType": "free"
                            }
                        ]
                    }
                }
            },
            {
                
                "_type": "sessions",
                "_source": {
                    
                    "movie_name": "The Greatest Showman 3D (U/A) - English",
                    
                    "session_data": {
                        "freeSeating": false,
                        "mandatoryItems": [
                            {
                                "quantity": 1,
                                "level": "ticket",
                                "price": 30,
                                "type": "3dglasses"    
                            }
                        ],
                        "areas": [
                            {
                                "seatingType": "fixed"
                            }       
                        ]
                    }
                }
            }
        ]
    }

If movie_name contains "3D" in it then mandatoryItems = [{"quantity": 1,"level": "ticket", "price": 30, "type": "3dglasses"}]

If movie_name does not contains "3D" in it then mandatoryItems = []

I want to achieve above assertions in my feature file.

Note: "movie_name" and "mandatoryItems" are present each element of an array. So I want to assert this condition on entire array.

Thanks in advance!!

Priyanka B
  • 101
  • 9

1 Answers1

1

Sorry @Peter for the inconvenience caused by me. I worked on this problem statement by referring all possible sources and wrote following code which is giving me desired output:

    Given url api_url
    When method Get
    And def mandatoryItems_present =
    """
    {
         "quantity": 1,
         "level": "ticket",
         "price": '#number',
         "type": "3dglasses",
    }
    
    """
    Then status 200
    And print response
    And def source_list = karate.jsonPath(response, '$.._source')
    And print source_list
    And match each source_list[*].session_data contains {'freeSeating': '#boolean','mandatoryItems':'##[] mandatoryItems_present'}
    And def movie_names = get source_list[*].movie_name
    And def mandatoryItems_list = get source_list[*].session_data.mandatoryItems 
    And def name_size = names.size();
    And print name_size
    And def threeD_movie_list = new ArrayList()
    And eval for(var i = 0; i < name_size; i++) {if (names[i].match('3D')) threeD_movie_list.add(names[i])} 
    And print threeD_movie_list
    And def threeD_movies_array_size = threeD_movie_list.size();
    And print threeD_movies_array_size
    And print expected
    And def expected = (threeD_movies_array_size == 0 ? {"mandatoryItems" : []} : {'mandatoryItems' : [mandatoryItems_present]} )
    And print expected
    And match each response.hits[*]._source.session_data[*].mandatoryItems == expected

Please let me know whether this approach is correct.

Priyanka B
  • 101
  • 9
  • sorry that is too much to read. my point is ask one small question at a time. so we need to read only 2-3 lines of JSON max. yes you should spend some time writing the question. if you need to split the above into multiple questions that is fine. all the best – Peter Thomas Aug 18 '20 at 14:42