Hi,
At a high level, I am working on building a utility where-in the overall operation and granular tasks to be performed is driven by a JSON file. A group of tasks is maintained as a JSON array and it is critical that tasks be performed in the order in which
they are defined in the array.
The JSON file is read from disk in power-shell using API ConvertFrom-Json
I have an two options
- Assume that sequence of elements of array are maintained by ConvertFrom-Json
Here one can define the tasks as an array of strings, holding the task files
NOTE: This is a random example, used only for the purpose of this question
"tasks": [
"C:\FirstTask.bat",
"C:\SecondTask_DependsOnFirstTaskBeingPerformed.bat"
]
The execution script will list the elements in the array and perform them in sequence.
$converted_json_object.tasks| Foreach-Object{
# Perform task
}
- Assume that sequence of elements of array are NOT maintained by ConvertFrom-Json
Here one can define the tasks as an array of objects, holding the task files and the sequence number
NOTE: This is a random example, used only for the purpose of this question
"tasks": [
{"task_number": 1, "task_name": "C:\FirstTask.bat"},
{"task_number": 2, "task_name": "C:\SecondTask_DependsOnFirstTaskBeingPerformed.bat"}
]
The execution script will list the elements in the array, sort them on task number and perform them in sequence.
$converted_json_object.tasks | Sort-Object -Property task_number
| Foreach-Object {
# Perform task
}
The JSON RFC states that - "An array is an ordered sequence of zero or more values." I am however not sure if ConvertFrom-Json parser is guaranteed to maintain this sequence.
I would appreciate any information on whether it is safe to assume that the current (and future) versions of ConvertFrom-Json are guaranteed to maintain the order or array elements. This will enable me to not require to maintain a "task_number"
member in the array of tasks in JSON.
Thanks and Regards,
Nilesh