json 格式化一行中的每个对象

标签 json shell jsonlines

cat 2.txt | ./jq '{(.id): .custom}'

以上命令输出
{
  "1": {
    "results": "Success"
  }
}
{
  "2": {

    "input method": "touch",
    "from": "Prescription Center",

  }
}
{
  "3": {

    "entry point": "|All"
  }

}

预期输出:

我想在一行中打印/保存每个对象。
cat 2.txt | ./jq '{(.id): .custom}'

{ "1": {  "results": "Success" }  }
{ "2": {  "input method": "touch",  "from": "Prescription Center"  }  }
{ "3": {  "entry point": "|All" } }

在shell脚本中可以吗?

最佳答案

jq manual

  • --compact-output / -c:

    By default, jq pretty-prints JSON output. Using this option will result in more compact output by instead putting each JSON object on a single line.



因此,以下应该起作用:
cat 2.txt | ./jq -c '{(.id): .custom}'

关于json 格式化一行中的每个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25467344/

相关文章:

php - 从 jQuery GET 请求返回 HTML

shell - 如何在 ubuntu 中删除交换文件?

openai-api - 字符 '\u201c' 在哪里?

ios - 从 JSON 数据 Swift 4 创建 TableView 部分

javascript - 如何将json日期格式转换为普通日期格式。例如 :'/Date(1388412591038)/' 到 '12-30-2013' ?

linux - shell脚本中mv命令的问题

shell - "history -p"有什么作用?

python - 如何从元素不断变化的 JSONL 文件中提取元素?

json - 使用 jq 过滤空值和/或空值

java - 如何使用 Retrofit 传递动态 JSON 文件名