我用jq来重新格式化我的JSON.

JSON字符串:

{"channel": "youtube", "profile_type": "video", "member_key": "hello"}

Wanted output:

{"channel" : "profile_type.youtube"}

My command:

echo '{"channel": "youtube", "profile_type": "video", "member_key": "hello"}' | jq -c '. | {channel: .profile_type + "." + .member_key}'

我知道下面的命令连接字符串.但它的工作逻辑与上述不同:

echo '{"channel": "youtube", "profile_type": "video", "member_key": "hello"}' | jq -c '.profile_type + "." + .member_key'

How can I achieve my result using ONLY jq?

推荐答案

Use parentheses around the string concatenation code:

echo '{"channel": "youtube", "profile_type": "video", "member_key": "hello"}' \
 | jq '{channel: (.profile_type + "." + .channel)}'

Json相关问答推荐

使用SQL查询从SON中查找第n个密钥对值

在scala中将字符串列转换为 struct 的JSON映射

将JSON输入子数组转换为字符串顺序列表输出

Bash和echo命令出现意外结果

在AWS步骤函数中将字符串解析为JSON&S映射状态

使用JQ在数组中 Select 第一个具有匹配项的项

PowerShell:使用JSON原生的Short命令处理JSON?

jq 对特定键进行过滤并将值整理到单个 csv 单元格中

从包含 JSON 对象序列的文件中获取第一个 JSON 对象

如何使用 serde_json 构建有状态的流式解析器?

TSQL FOR JSON 嵌套值

在 json 嵌入的 YAML 文件中 - 使用 Python 仅替换 json 值

Swift:如何从字典中删除空值?

如何一次加载无限滚动中的所有条目以解析python中的HTML

使用 Spring 和 JsonTypeInfo 注释将 JSON 反序列化为多态对象模型

了解 JSON Schema 草稿版本 4 中的additionalProperties关键字

如何创建 JSON 对象 Node.js

杰克逊在通用列表中读取 json

运算符不存在:json = json

Java HashMap 与 JSONObject