Is there a rule of thumb for when to use JSON objects instead of arrays with a 'name' key?

4/2/2019

What reasons might one prefer the with_object or the with_array options below?

{
  "with_array": [
    {
      "name": "foo",
      "key": "value"
    },
    {
      "name": "bar",
      "key": "value"
    }
  ],
  "with_object": {
    "foo": {
      "key": "value"
    },
    "bar": {
      "key": "value"
    }
  }
}

Real world example: Why is the environment variable spec in this Kubernetes manifest using an array of name and value keys?

pods/inject/envars.yaml 

apiVersion: v1
kind: Pod
metadata:
  name: envar-demo
  labels:
    purpose: demonstrate-envars
spec:
  containers:
  - name: envar-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: DEMO_GREETING
      value: "Hello from the environment"
    - name: DEMO_FAREWELL
      value: "Such a sweet sorrow"

From Define an environment variable for a container

-- jdb
arrays
json
kubernetes
object

1 Answer

4/2/2019

The answer to three questions can help finding which of the two is more appropriate:

questions to determine structure

-- trincot
Source: StackOverflow