Description

The ListJSONGen parses a JSON file and retrieves data from specified fields for an Attribute. The retrieved value can be used by another Domain or included in the generated output for that Attribute. 


For example, for a single API, you can use the ListJSONGen to retrieve a field value within an API Response and pass that value to another output Domain.  


In This Article


Generator Parameters

The following parameters are available for this Generator. Items with an asterisk* are required. 

  • filePath* - Defines the path where the JSON file is located. 
  • fileSubDir - Defines an optional subdirectory under the path where the JSON file exists.
  • fileName* - Defines the name of the JSON file to open. 
  • columnHierarchy* - Defines which column within the JSON file to read data from. Use a period to separate each item in the hierarchy. Note: A detailed example is shown later in this article. 
  • setLoop* - Defines whether the size of the list should be used to set the Scenario loop value. The default value is "false".
  • sortOrder* - Defines whether the list should be sorted, and if so, in ascending, descending, or random order. 
  • seed - Defines an optional seed for randomization. Using the seed will ensure that the same random data is generated each time data is generated. 
  • list - Stores one value on each line in the list. To add values, type them in and then press ENTER. The listed value is only for simulation (preview) mode. When running a true Scenario, the data will be loaded from the specific resource (i.e., JSON file).


Example 1 - Retrieve Fruit from a Flat JSON File

A user wants to retrieve the fruit field value from a flat JSON file containing three objects (shown below).


JSON File Contents

[
    {
        "fruit": "Apple",
        "size": "Large",
        "color": "Red"
    },
    {
        "fruit": "Mango",
        "size": "Large",
        "color": "Yellow"
    },
    {
        "fruit": "Orange",
        "size": "Medium",
        "color": "Yellow"
    }
]


Project Setup

They have set up a Project with the following: 

  1. Domain - SampleDomain
  2. Attribute - fruits
  3. Receiver -  DelimitedFileReceiver
  4. Scenario - SampleDomainScenario



Parameter Configuration

  • columnHierarchy = fruit
  • setLoop = true 
    • The Scenario loop count will be '3' since the file has three objects.
  • sortOrder = noSorting 
    • The fruits will appear in the same order as listed in the JSON file. 


Sample Output


Note: If setLoop is 'false,' the loop count will be what has been set at the Scenario or Test Data Case level (a case overrides the Scenario loop count value). So, if the loop count is '12,' the three values will repeat for 12 records.



Example 2 - Retrieve Products from a Nested JSON File

For this example, the user is retrieving values from a complex, nested JSON file that contains users, orders, and products. They want to retrieve the products for each user order. 


JSON File Contents

{
  "users": [
    {
      "id": 1,
      "name": "John Doe",
      "email": "john.doe@example.com",
      "address": {
        "street": "123 Main St",
        "city": "Anytown",
        "state": "CA",
        "zip": "12345"
      },
      "orders": [
        {
          "order_id": 101,
          "date": "2023-06-25",
          "total": 250.75,
          "status": "shipped",
          "products": [
            {
              "product_id": 501,
              "name": "Laptop",
              "quantity": 1,
              "price": 999.99
            },
            {
              "product_id": 502,
              "name": "Mouse",
              "quantity": 2,
              "price": 25.50
            }
          ]
        },
        {
          "order_id": 102,
          "date": "2023-07-01",
          "total": 89.99,
          "status": "delivered",
          "products": [
            {
              "product_id": 503,
              "name": "Keyboard",
              "quantity": 1,
              "price": 89.99
            }
          ]
        }
      ]
    },
    {
      "id": 2,
      "name": "Jane Smith",
      "email": "jane.smith@example.com",
      "address": {
        "street": "456 Oak St",
        "city": "Othertown",
        "state": "NY",
        "zip": "67890"
      },
      "orders": [
        {
          "order_id": 103,
          "date": "2023-06-20",
          "total": 150.00,
          "status": "processing",
          "products": [
            {
              "product_id": 504,
              "name": "Tablet",
              "quantity": 1,
              "price": 150.00
            }
          ]
        }
      ]
    }
  ],
  "products": [
    {
      "id": 501,
      "name": "Laptop",
      "description": "A high-performance laptop for all your computing needs.",
      "price": 999.99,
      "category": "Electronics",
      "reviews": [
        {
          "review_id": 201,
          "user_id": 1,
          "rating": 5,
          "comment": "Excellent laptop with great performance!",
          "date": "2023-06-26"
        }
      ]
    },
    {
      "id": 502,
      "name": "Mouse",
      "description": "A comfortable and responsive mouse.",
      "price": 25.50,
      "category": "Accessories",
      "reviews": [
        {
          "review_id": 202,
          "user_id": 1,
          "rating": 4,
          "comment": "Good mouse, but a bit pricey.",
          "date": "2023-06-27"
        }
      ]
    },
    {
      "id": 503,
      "name": "Keyboard",
      "description": "A mechanical keyboard with customizable keys.",
      "price": 89.99,
      "category": "Accessories",
      "reviews": []
    },
    {
      "id": 504,
      "name": "Tablet",
      "description": "A lightweight and powerful tablet for everyday use.",
      "price": 150.00,
      "category": "Electronics",
      "reviews": [
        {
          "review_id": 203,
          "user_id": 2,
          "rating": 4,
          "comment": "Great tablet for the price.",
          "date": "2023-06-21"
        }
      ]
    }
  ]
}


Project Setup

They have set up a Project with the following: 

  1. Domain - SampleDomain
  2. Attributes - products
  3. Receiver -  DelimitedFileReceiver
  4. Scenario - SampleDomainScenario



Parameter Configuration

  • columnHierarchy = users.orders.products.name
    • You must use a period (.) between each item in the hierarchy for complex files.
    • In this case, the user wants to retrieve the product name from each user order. 
  • setLoop = true
  • sortOrder = noSorting


Sample Output

The JSON file contained two users. The first user had two orders containing a total of 3 products. The second user had one order containing 1 product. Four products are included in the output file.  


 

User Id = 1

  • Order 101 - Laptop and Mouse
  • Order 102 - Keyword


User Id = 2 

  • Order 103 - Tablet