我在弄清楚我需要在jq中使用什么过滤器和方法以将json数据转换为我想要的内容时遇到问题。我尝试过的每种组合都根本无法使用,或者针对每个对象的每个子数组的每个值进行迭代。
我在此上花费了太长时间,并且没有取得更多进展。需要有人将另一只眼睛放在上面。我敢肯定,这比我想像的要简单得多,但我只是无法弄清楚!
这是我正在使用的json数据:
[
{
"location": "locationa",
"services": [
{
"name": "serviceA",
"version": "5.2.0.2",
"updatedAt": "2018-04-17"
},
{
"name": "serviceB",
"version": "4.19.0.5",
"updatedAt": "2018-04-17"
}
]
},
{
"location": "locationb",
"services": [
{
"name": "serviceA",
"version": "5.2.0.2",
"updatedAt": "2018-04-17"
},
{
"name": "serviceB",
"version": "4.19.0.5",
"updatedAt": "2018-04-17"
},
{
"name": "serviceC",
"version": "1.0.0.1",
"updatedAt": "2018-04-17"
}
]
}
]
这是我尝试退出的格式:
locationa serviceA 5.2.0.2
locationa serviceB 4.19.0.5
locationb serviceA 5.2.0.2
locationb serviceB 4.19.0.5
locationb serviceC 1.0.0.1
答案 0 :(得分:1)
您可以使用library(ggplot2)
library(reshape2)
library(scales)
df<-data.frame(row.names=c("AcroMetrix","PV1_PV2","CHIPv2","TSACP","TSTP"),Germline=c(34,33,14,22,12),Somatic_5_15=c(341,331,281,249,147),Somatic_15_30=c(180,176,129,124,108))
df$name<-row.names(df)
df_molten<-melt(df)
df_molten$name<-factor(df_molten$name,levels=c('AcroMetrix','PV1_PV2','CHIPv2','TSACP','TSTP'))
df_molten$Percent_targeted <- unlist(lapply(1:length(levels(df_molten$variable)), function(i){c(100,100,77,73,49)}))
gg <- ggplot(df_molten,aes(x=name,y=value,fill=variable))+
geom_bar(stat='identity', width=.6)+
scale_fill_discrete(labels=c("Germline","Somatic 5-15% VAF","Somatic 15-30% VAF"))+
geom_text(aes(label=value),size=3,fontface='bold',position=position_stack(vjust=.5))+
xlab("Panel")+ylab("Counts")+
theme_bw()+
theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank(),panel.background=element_blank(),axis.line=element_line(colour="black"),panel.border=element_blank(),legend.title=element_blank())
gg <- gg + scale_y_continuous(expand = expand_scale(mult=c(0, 0.0)))
# get the sacle values of the current y-axis
gb <- ggplot_build(gg)
y.range <- gb$layout$panel_params[[1]]$y.range
y2.range <- range(df_molten$Percent_targeted)# extendrange(, f=0.01)
scale_factor <- (diff(y.range)/max(y2.range))
trans <- ~ ((. -y.range[1])/scale_factor)
df_molten$Percent_targeted_scaled <- rescale(df_molten$Percent_targeted, y.range, c(0, y2.range[2]))
df_molten$x <- which(levels(df_molten$name)%in%df_molten$name)-.3
gg <- gg + geom_segment(aes(x=x, xend=x, yend=Percent_targeted_scaled), y=0, size=2, data=df_molten)
gg <- gg + geom_label(aes(label=paste0(Percent_targeted, '%'), x=x, y=Percent_targeted_scaled), fill='white', data=df_molten)
gg <- gg + scale_y_continuous(expand=expand_scale(mult=c(.05, .05)), sec.axis = sec_axis(trans, name = "Percent targeted", labels = scales::percent(seq(0, 1, length.out = 5), scale=100)))
gg
运算符连接字符串:
+
答案 1 :(得分:1)
通常,在尝试输出csv / tsv时,您将希望生成要输出的行的数组,然后传递到@csv
或@tsv
或利用join/1
以以下格式输出您选择的。
jq -r '.[] | [.location] + (.services[] | [.name, .version]) | @tsv' input.json
答案 2 :(得分:0)
作为替代方案,您可以考虑使用基于步行路径的Unix实用程序 jtc
:
bash $ <file.json jtc -x'<name>l:' -y'[-3][location]' -y' ' -y'[-1][version]' | xargs -L3
locationa serviceA 5.2.0.2
locationa serviceB 4.19.0.5
locationb serviceA 5.2.0.2
locationb serviceB 4.19.0.5
locationb serviceC 1.0.0.1
bash $