Mandragor
Mandragor

Reputation: 5544

convert simple concatenated json to shell variables

Some simple JSON arrives and looks like this. I need to convert it to shell variables, the keys should be uppercase, and also "-" should be converted "_", and finally all key names should be put in shell list to generate the output which you can see below. I managed to do this with a complicated pipe of jq queries, I am wondering if there is a more simple way to do this I jq? Note that the __INDEX0,__INDEX1,... variables in the final output would not be required.

JSON INPUT (input.json)

{
  "msg-version": "1.0",
  "msg-type":  "app-config",
  "content": "aaa"
}
{
  "msg-version": "1.0",
  "msg-type":  "app-config",
  "content": "bbb"
}
{
  "msg-version": "1.0",
  "msg-type":  "app-config",
  "content": "ddd",
  "breakit": { "a":"b"}
}

SHELL OUTPUT

__INDEX0='0'
MSG_VERSION0='1.0'
MSG_TYPE0='app-config'
CONTENT0='aaa'
__INDEX_NAMES0='CONTENT MSG_TYPE MSG_VERSION'
__INDEX1='1'
MSG_VERSION1='1.0'
MSG_TYPE1='app-config'
CONTENT1='bbb'
__INDEX_NAMES1='CONTENT MSG_TYPE MSG_VERSION'
__INDEX2='2'
MSG_VERSION2='1.0'
MSG_TYPE2='app-config'
CONTENT2='ddd'
BREAKIT2='{"a":"b"}'
__INDEX_NAMES2='BREAKIT CONTENT MSG_TYPE MSG_VERSION'

bash jq quer(ies)

cat input.json|\
jq  'keys as $keys|. + {"__index_names": ($keys|join(" ")|split("-")|join("_")|ascii_upcase)}'|\
jq --slurp 'keys[] as $_k|{ __index: $_k|tostring}*  .[$_k]  '|\
jq -r '.__index as $i|to_entries| .[]| { key: ((.key + $i)|split("-")|join("_")|ascii_upcase), value: (.value) }|.key + "='"'"'" + (.value|tostring) + "'"'"'"')

Upvotes: 2

Views: 72

Answers (1)

peak
peak

Reputation: 116650

Using the jq command-line options -n and -r, the following produces the output as required (that is, without the superfluous __INDEX keys):

def q: if type=="string" then @sh else tojson|@sh end;

foreach inputs as $in (-1;.+1;
  . as $n
  | $in
  | to_entries[]
  | "\(.key|gsub("-";"_")|ascii_upcase + ($n|tostring) )=\(.value|q)"
  )

The tricky bit here, of course, is adding the integer suffixes to the key names.

Upvotes: 1

Related Questions