示例json文件payload.json.tpl
:
{
"foo": "bar",
"x": {
"y": "${array}"
}
}
我在bash中有一个数组
array=("one" "two" "three")
如何运行jq命令将键.x.y
替换为["one", "two", "three"]
所以最终的json将是:
{
"foo": "bar",
"x": {
"y": ["one", "two", "three"]
}
}
答案 0 :(得分:2)
使用import xlsxwriter
import pandas as pd
df= pd.DataFrame({'Time':['2019/01/03 15:02:07', '2019/01/03 15:16:55', '2019/01/03 15:17:20', '2019/01/03 15:28:58','2019/01/03 15:32:28','2019/01/03 15:38:54'],
'Payload':['[0]->[1]', '[1]->[0]','[0]->[1]','[0]->[1]','[1]->[0]','[0]->[1]']})
workbook = xlsxwriter.Workbook('Results.xlsx')
ws = workbook.add_worksheet("Rapport détaillé")
# wsNavco = workbook.add_worksheet("Délai reconnexion NAVCO")
ws.set_column(0, 1, 30)
ws.set_column(1, 2, 25)
# Add a format. Light yellow fill with dark red text.
format1 = workbook.add_format({'bg_color': '#fffcad',
'font_color': '#0a0a0a'})
# Add a format. Green fill with dark green text.
format2 = workbook.add_format({'bg_color': '#e7fabe',
'font_color': '#0a0a0a'})
# Write a conditional format over a range.
ws.conditional_format('A1:A24', {'type': 'cell',
'criteria': '>=',
'value': 50,
'format': format1})
ws.conditional_format('B1:B24', {'type': 'cell',
'criteria': '>=',
'value': 50,
'format': format2})
parametres = (
['Parametres', 'Valeurs'],
['1ere date ', str(df['Time'].iloc[0])],
['Derniere date ', str(df['Time'].iloc[len(df)-1])],
)
# Start from the first cell. Rows and
# columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for name, parametres in (parametres):
ws.write(row, col, name)
ws.write(row, col + 1, parametres)
row += 1
workbook.close()
df= df.sort_values(by='Time')
df.Time = pd.to_datetime(df.Time, format='%Y/%m/%d %H:%M:%S')
print('df\n',df)
diff = []
for i in range(len(df) - 1):
if df.iloc[i, 1] == '[1]->[0]' and df.iloc[i + 1, 1] == '[0]->[1]':
time_diff = df.iloc[i + 1, 0] - df.iloc[i, 0]
else:
time_diff = 0
diff.append(time_diff)
diff.append(0) # to fill the last value
df['Difference'] = diff
print(df['Difference'])
print('df1\n',df)
workbook = xlsxwriter.Workbook('Results.xlsx')
wsNavco = workbook.add_worksheet('Délai reconnexion NAVCO')
# wsNavco = wb.worksheets[1]
wsNavco.set_column(0, 1, 25)
wsNavco.set_column(1, 2, 55)
# Add a format. Light yellow fill with dark red text.
format1 = workbook.add_format({'bg_color': '#fffcad',
'font_color': '#0a0a0a'})
# Add a format. Green fill with dark green text.
format2 = workbook.add_format({'bg_color': '#e7fabe',
'font_color': '#0a0a0a'})
# Write a conditional format over a range.
wsNavco.conditional_format('A1:A24', {'type': 'cell',
'criteria': '>=',
'value': 50,
'format': format1})
wsNavco.conditional_format('B1:B24', {'type': 'cell',
'criteria': '>=',
'value': 50,
'format': format2})
for i in range (len(df)-1):
wsNavco.set_column(1, 1, 15)
wsNavco.write('A'+str(3),'Payload')
wsNavco.write('A'+str(i+4), str((df.iloc[i,1])))
wsNavco.write('B'+str(3),'Délai reconnexion NAVCO')
wsNavco.write('B'+str(i+4), str((df.iloc[i,2])))
workbook.close()
(需要jq 1.6)
$ARGS.positional
答案 1 :(得分:1)
就像这样,也可以与jq <1.6一起使用:
< payload.json.tpl jq --arg a "${array[*]}" '.x.y=($a|split(" "))'
请注意使用${array[*]}
而不是${array[@]}
。使用*
时,${array}
的元素将作为单个字符串而不是多个字符串传递。
https://www.gnu.org/software/bash/manual/html_node/Arrays.html