jq

How to avoid generating all combinations of selected data while constructing an object?

僤鯓⒐⒋嵵緔 提交于 2021-01-28 21:01:23
问题 My original JSON is given below. [ { "id": "1", "name": "AA_1", "total": "100002", "files": [ { "filename": "8665b987ab48511eda9e458046fbc42e.csv", "filename_original": "some.csv", "status": "3", "total": "100002", "time": "2020-08-24 23:25:49" } ], "status": "3", "created": "2020-08-24 23:25:49", "filenames": "8665b987ab48511eda9e458046fbc42e.csv", "is_append": "0", "is_deleted": "0", "comment": null }, { "id": "4", "name": "AA_2", "total": "43806503", "files": [ { "filename":

Use jq to merge keys with common id

独自空忆成欢 提交于 2021-01-28 11:45:46
问题 consider a file 'b.json': [ { "id": 3, "foo": "cannot be replaced, id isn't in a.json, stay untouched", "baz": "do not touch3" }, { "id": 2, "foo": "should be replaced with 'foo new2'", "baz": "do not touch2" } ] and 'a.json': [ { "id": 2, "foo": "foo new2", "baz": "don't care" } ] I want to update the key "foo" in b.json using jq with the matching value from a.json. It should also work with more than one entry in a.json. Thus the desired output is: [ { "id": 3, "foo": "cannot be replaced, id

jq query returning too many records (unwanted permutations)

余生长醉 提交于 2021-01-28 11:31:40
问题 I have a complex JSON file and I am trying to get the below result using JQ. Expected Result: { "Host": "Test.example.com", "Title": "Ensure message of the day is configured properly", "Status": "passed" } { "Host": "Test.example.com", "Title": "Ensure bond0 is present", "Status": "passed" } { "Host": "Test.example.com", "Title": "Ensure the SELinux state is disabled", "Status": "passed" } Below is the JSON file that I get as a result of running an Chef Inspec profile. JSON FILE: { "platform"

group objects by a field and sum another, then produce a CSV report

不羁岁月 提交于 2021-01-28 10:56:01
问题 How can I create a csv from this json? I have: [ { "name": "John", "cash": 5 }, { "name": "Anna", "cash": 4 }, { "name": "Anna", "cash": 3 }, { "name": "John", "cash": 8 } ] I need group by name and sum the cash and send the result a .csv like: John,13 Anna,7 Thanks! 回答1: JQ has group_by as a builtin, use that and do map(.cash) | add to sum cash values for each group. group_by(.name)[] | [.[0].name, (map(.cash) | add)] | @csv Online demo 来源: https://stackoverflow.com/questions/61953604/group

jq: Remove keys with empty string values

为君一笑 提交于 2021-01-28 10:28:27
问题 I have the following JSON: { "data": [ { "{#NAME}": "Test 1", "{#ID}": "1", "{#IP}": "192.168.1.2:80" }, { "{#NAME}": "Test 2", "{#ID}": "2", "{#IP}": "" }, { "{#NAME}": "Test 3", "{#ID}": "3", "{#IP}": "192.168.1.3:80" }, { "{#NAME}": "Test 4", "{#ID}": "4", "{#IP}": "192.168.1.4:80" }, { "{#NAME}": "Test 5", "{#ID}": "5", "{#IP}": "" } ] } But I want to return: { "data": [ { "{#NAME}": "Test 1", "{#ID}": "1", "{#IP}": "192.168.1.2" }, { "{#NAME}": "Test 2", "{#ID}": "2", }, { "{#NAME}":

unexpected loop cycles with jq

懵懂的女人 提交于 2021-01-28 08:47:18
问题 I'm trying to print a table in bash to stdout from json with jq : [ { "key": "name", "doc_count": 1000, "values_over_time": { "buckets": [ { "key_as_string": "2019-05-01 11:00:00.000", "key": 1556708400000, "doc_count": 50 }, { "key_as_string": "2019-05-02 12:00:00.000", "key": 1556798400000, "doc_count": 40 }, { "key_as_string": "2019-05-02 13:00:00.000", "key": 1556802000000, "doc_count": 30 } ] } } ] With jq -r '(.[].key + " " + .[].values_over_time[][].key_as_string) + " " + (.[].values

Using wildcard for files with jq on Windows

北战南征 提交于 2021-01-28 05:31:01
问题 I am using jq 1.6 on Windows 8.1 and facing same issue as reported here https://github.com/stedolan/jq/issues/1644 Command as simple as jq . *.json is failing with following error: Assertion failed! Program: jq.exe File: src/main.c, Line 256 Expression: wargc == argc This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. Does anyone have solution to this? What is the correct way to use jq with all the

Java复习整理 01

[亡魂溺海] 提交于 2021-01-28 05:21:26
练习代码: 练习代码: 1 //这条语句说明这个Java文件在demo的包下 2 package demo1; 3 /** 4 * 5 * @author 王兴平 6 * 这个是第一个hello world 案例 7 * 这个注释是文本注释 8 * 注释内容是不会编译的,编译器会忽略其存在 9 * 10 */ 11 // 这是一个单行注释 现在用它描述个语句的功能 12 /* 13 * 这是多行注释 14 * 用它来描述内的工能的详细介绍 15 * 或者实现步骤 16 */ 17 /* 18 * 实现步骤: 19 * 1.定义一个类 class 20 * 2.编写程序执行的入口方法,main主方法 21 * 3.通过输出语句System.out.println()将信息”HelloWorld!”打印在控制台上 22 */ 23 24 //这条语句说明这个类的名字注意要和文件明相同 25 public class Helloworld { 26 //这是一个主方法程序执行的入口 27 public static void main(String[] args) { 28 //这是一条控制台输出语句 29 System.out.println("Hello World!"); 30 } 31 } 1 package demo2; 2 /** 3 * 4 * @author 王兴平 5

Using jq to change json object and field value from variable

北城以北 提交于 2021-01-28 04:42:09
问题 I want to modify the following json using variables specified in a Linux bash shell using jq. var1="red" var2="european.flowers" var3="european_vegetables" var4="20" My json: { "plants": { "flowers.small": { "colour": "", "age": "", "vegetables": { "root": "", "height": "" } } } } I want to modify the json using variables in jq: { "plants": { "${var2}": { "colour": "${var1}", "age": "", "${var3}": { "root": "", "height": "${var4}" } } } } I am attempting to just set a field value from a

Memory usage of jq's --slurp option

对着背影说爱祢 提交于 2021-01-28 03:42:24
问题 Does the --slurp option load the entire input in memory before processing it or has it been optimized somehow in order to avoid that? 回答1: The answer to the question is essentially "yes". Commands such as "jq --slurp . FILE ...." store the parsed input as an array in memory. This will often require more memory than the size of the input itself -- consider for example that JSON objects are stored as hash tables. With jq 1.5 there are often better alternatives than "slurping" the input. Most