Skip to content

Create a Function to Generate a JSON File from a List of Names and Values #108

@hbisneto

Description

@hbisneto

Develop a Python function that takes a list of item names and their corresponding values as parameters, iterates over the names, and creates a JSON file with the resulting structure. The function should associate each name with its respective value, producing a well-formatted JSON file.

Task Details:

  • Objective: Create a function that:
    • Takes two parameters:
      • names: a list of strings representing the item names.
      • values: a list of values (can be any JSON-compatible type, such as strings, numbers, booleans, etc.).
    • Iterates over the list of names and associates each name with its corresponding value.
    • Generates a JSON file with the resulting structure.
  • Requirements:
    • The function must validate that the names and values lists have the same length. If not, it should raise an exception with a clear message.
    • The generated JSON file must be saved to a specified path (either as an additional parameter or a default path).
    • The JSON file should be well-formatted (e.g., using indent=4 with the json library).
    • Ensure that the value types are JSON-compatible.
  • Example Usage:
    names = ["item1", "item2", "item3"]
    values = [10, "text", True]
    create_json(names, values, "output.json")
    Expected Output (output.json):
    {
        "item1": 10,
        "item2": "text",
        "item3": true
    }
  • Acceptance Criteria:
    • The function must correctly create the JSON file based on the provided parameters.
    • It should handle errors such as lists of different lengths or invalid data types.
    • The generated JSON file must be valid and readable.
    • Unit tests must be implemented to verify:
      • Correct creation of the JSON file.
      • Error handling for lists of different lengths.
      • Validation of JSON-compatible data types.
  • Additional Notes:
    • Use Python's standard json library for JSON file manipulation.
    • Consider adding an optional parameter to specify the output file name/path.
    • Document the function with a docstring explaining parameters, return value, and possible exceptions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    File ModuleRelated to the "File" module inside FileSystemProWrapper ModuleRelated to the "Wrapper" module inside FileSystemPro

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions