With brackets: error Data Bag Items must contain a Hash or Mash


#1

hello. i was just struggling a big getting a data bag to upload. i
made a small modification to the json, and now it works, and i’m
wondering why this is.

i grabbed the example json from a blog post,
http://www.themomorohoax.com/2010/07/31/ruby-chef-tutorial

the json fails for me:

zahadum:~/sbox/ops/chef-repo$ knife data bag from file users37 data_bags/users37/ops.json
ERROR: Chef::Exceptions::ValidationFailed: Data Bag Items must contain a Hash or Mash!

zahadum:~/sbox/ops/chef-repo$ cat data_bags/users37/ops.json
[
{
“id”: “kallen”,
“groups”: [
“admin”
],
“uid”: 666,
“local_files”: true,
“full_name”: “kallen”,
“shell”: “/bin/bash”,
“ssh_key”: “ssh-rsa AAAA[snip for brevity]4drMA”,
“password”: “$1$8dootdootdootdoot”
}
]

i remove the surrounding brackets in that json file for shiggles.
and then the data bag uploads fine:

zahadum:~/sbox/ops/chef-repo$ knife data bag from file users37 data_bags/users37/ops.json
Updated data_bag_item[users37::kallen]

why is that?

thanks in advance,
kallen


#2

Because the square brackets mean that it’s an array with a single element - so the deserialized JSON is an array, rather than a hash or mash.


Opscode, Inc.
Adam Jacob, Chief Product Officer
T: (206) 619-7151 E: adam@opscode.com

On Thursday, August 25, 2011 at 12:46 PM, kallen@groknaut.net wrote:

hello. i was just struggling a big getting a data bag to upload. i
made a small modification to the json, and now it works, and i’m
wondering why this is.

i grabbed the example json from a blog post,
http://www.themomorohoax.com/2010/07/31/ruby-chef-tutorial

the json fails for me:

zahadum:~/sbox/ops/chef-repo$ knife data bag from file users37 data_bags/users37/ops.json
ERROR: Chef::Exceptions::ValidationFailed: Data Bag Items must contain a Hash or Mash!

zahadum:~/sbox/ops/chef-repo$ cat data_bags/users37/ops.json
[
{
“id”: “kallen”,
“groups”: [
“admin”
],
“uid”: 666,
“local_files”: true,
“full_name”: “kallen”,
“shell”: “/bin/bash”,
“ssh_key”: “ssh-rsa AAAA[snip for brevity]4drMA”,
“password”: “$1$8dootdootdootdoot”
}
]

i remove the surrounding brackets in that json file for shiggles.
and then the data bag uploads fine:

zahadum:~/sbox/ops/chef-repo$ knife data bag from file users37 data_bags/users37/ops.json
Updated data_bag_item[users37::kallen]

why is that?

thanks in advance,
kallen