Description

An Attribute can be hidden (not generated) within Flat or Nested JSON output based on a specified condition. For an Attribute, the user must select an output condition within the Attribute Dashboard and specify the condition using an assigned Generator. 


Note: This feature is supported for the JSONSegmentMergeReciever (Nested JSON) and the JSONFILEReceiver (Flat JSON). The example in this article works the same when generating Flat JSON. 

Note: To see another example that hides an Attribute in the generated output when the value is NULL or Blank, click here


Story

A tester wants to generate nested JSON but has an Attribute that should not appear in the output based on the following condition: 


If Voter.age > 17
then allowedToVote = true
else allowedToVote = null


When "true," the allowedToVote Attribute will be displayed in the JSON output file. However, it will not appear in the JSON output file when the value of the Attribute is "null." 



Step 1: Import a JSON file

A JSON file will need to be imported to create the Domains and Attributes for generating test data. For step-by-step instructions, please see this article: How to import Domains from JSON.



The import will create the following within the selected Project Version of a Project: 

  • Domains (including Merge Domain)
  • Domain Attributes
  • Domain Scenarios
  • Scenario Chain
  • Configuration File


Additionally, it will automatically add the required Receiver to each created Domain. 



Step 2: Select the Domain and set the loopCount

  • Select the Domain within the Domains Pane. 



  • The loopCount will be set to "5", meaning five JSON Files will be generated. For step-by-step instructions on how to change a Domain's loopCount, click here.


    Note: The loopCount of the Parent Domain determines how many JSON files will be generated within the output directory. In this example, the Voter Domain is the Parent Domain.


Step 3: Select the Attribute and set up the Condition

  • Select the Attribute that will be hidden (not generated) in the output based on the defined condition.



  • Select an Output Condition: Exclude on NULL, Exclude on Blank, or Exclude on Blank or NULL.

  • For this example, Exclude on Null has been selected.



  • Assign a Generator to define the condition that will determine when the Attribute is not generated in the output. For this example, the EvalCaseGen Generator has been assigned.

  • The following parameters have been configured:
    • equation = var1 > 17
    • var1 = references the age Attribute in the Voter Domain
    • caseTrue = true
    • caseFalse = null


When the voter's age is above 17, the allowedToVote Attribute will be generated. It will be hidden (not generated) in the output when the voter's age is 17 or less.


Note: Other Generators can be used to determine when an Attribute is not generated within nested JSON, such as the SwitchGen Generator.


Step 4: Download the Scenario Chain


Click on the Cloud Icon within the Scenario Chains Pane to download the Scenario Chain. 



Step 5: Download the Configuration File


Click on the Cloud Icon within the Configuration Management Tab to download the Configuration File. 



The Config.xml file will need to be placed in a Config folder within the path defined for the user's resource.output.directory. This is defined in the Organization Resources Pane within the GenRocket web platform.



Step 6: Run the Scenario to Generate Data

Open a Command Prompt or Terminal window and use the following command to generate nested JSON output. Replace <ScenarioChain.grs> with the actual name of the Scenario Chain.


genrocket -r <ScenarioChain.grs>


The Scenario Chain Name in this example is JSON1Scenario Chain: 


genrocket - r JSON1ScenarioChain.grs


Sample Output

This example will create five JSON files. The voter's age will determine if the allowedToVote Attribute is included within each generated  JSON file. 



Output File 1

The voter's age is "14," and the allowedToVote Attribute is hidden (not generated) within the output file.


Output File 2

The voter's age is "3," and the allowedToVote Attribute is hidden (not generated) in the output file. 



Output File 3

The voter's age is "26," and the allowedToVote Attribute is included in the output file.

 


Output File 4

The voter's age is "6," and the allowedToVote Attribute is hidden within the output file. 



Output File 5

The voter's age is "29," and the allowedToVote Attribute is included in the output file.