Ask Your Question
1

Convert a string to hash

asked 2013-12-10 16:09:48 -0500

TwistedTech gravatar image

I have a string in the variable $user_sid which is the string result of a hash. e.g. '{"Administrator"=>"S-1-5-21-689180083-2307631502-233028039-500", "Ghost"=>"S-1-5-21-689180083-2307631502-233028039-1000", "Guest"=>"S-1-5-21-689180083-2307631502-233028039-501"}'

How can I convert this back to a hash? Is there a str2hash function? I found the hash function in the stdlib but that takes an array.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2013-12-11 00:14:58 -0500

llowder gravatar image

Since that string looks like it is likely JSON, I would recommend using the parsejson() function that is in recent versions of stdlib.

This function takes a JSON formatted string and turns it into an array or hash (or other puppet construct).

There is a similar function for YAML - parseyaml().

edit flag offensive delete link more

Comments

`parsejson` throws an error "expected ':' in object at '=>"S-1-5-21-68..." which can be solved by a simple `regsubst` call.

TwistedTech gravatar imageTwistedTech ( 2013-12-11 08:01:12 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-12-10 16:09:48 -0500

Seen: 1,641 times

Last updated: Dec 11 '13