New answers tagged

4

To get the name of the block device and each of its non-null child mount-points as a tab-delimited list: jq -r ' .blockdevices[] | select(.fstype == "crypto_LUKS") as $dev | $dev.children[]? | select(.mountpoint | type == "string") as $mp | [ $dev.name, $mp.name ] | @tsv' Since a "null mount-point" is not actually ...


0

You can mix quotes in the same shell string, so if one part of the string is easier to write with single quotes and another part requires a substitution, you can use single quotes for the literal part and double quotes around the substitution. However, note that the resulting JSON will not be well-formed if $line contains a double quote, a backslash or ...


2

It seems you want to pass a nested JSON object as an argument to some command. The shell won't expand a variable that is inside single quotes. The solution here is not to switch to using double quotes, though, as the data you are injecting into the JSON document might need encoding for it to be valid JSON (if it contains tabs, quotes, or other characters ...


0

Just put your string in double quotes and not in simple quotes. Don't forget to backslashes internal double quotes. while read line; do command "{'\"id\"':{\"S\":\"$line\"}}" done <output.txt


0

I like jq, but it's not the tool for transforming JSON into CSV. For doing anything with CSV, consider: csvkit: a decent general-purpose CSV toolkit, has in2csv which can transform JSON to CSV GoCSV: like csvkit, but I'd say "more well-rounded"; has transpose, which is what's needed for this problem That trailing comma after the final ("...


0

@roaima's answer explains it well, alternatively you can write the solution like this: jq '[ .[] | {IP:.ip, STATUS:.status, SOFTWARE:.info.software.version, KERNEL:.info.kernel} ]' json.data Here, the .[] part iterates over all elements of input, and the outer [] part, that wraps the whole result, converts it to an array.


2

Your command iterates across each of the array elements, for each of the four elements you've specified, so instead of getting three objects, you're getting 34 = 81 objects. To iterate across each object you can use a map() function, like this: jq 'map({IP:.ip, STATUS:.status, SOFTWARE:.info.software.version, KERNEL:.info.kernel})' json.data [ { "...


1

I'm answering with what is essentially the same solution as to what thanasisp posted, but reformulated to use a helper function, groupmap, to do the grouping and mapping of the contents of the clientSettings and productSettings arrays. def groupmap(key): group_by(.[key]) | map( { (key): .[0][key], entries: del(.[][key]) } ); .clientSettings ...


2

An empty string is still a string, so .abc_severity? would give you an empty string, not null (or false). Also, note that the question mark means approximately "replace with null if this key does not exist". In the example, all three keys exist, and their values are not null. If you had been using null for empty values, your jq expression would ...


1

If jq not available, sed can be used. ELAP=$(sed 's/.*"elapsedTime":\([0-9]*\).*/\1/' filename) Try it online!


8

The data is JSON format so it's best to use a parser that understands this format. This will pick out the elapsedTime value from the JSON in the file /tmp/data jq .elapsedTime /tmp/data 17 This will pick out only those values larger than 10000 jq '.elapsedTime | select(. > 10000)' /tmp/data If you really cannot use jq then a sed|awk construct can be ...


3

Assuming you don't REALLY want to convert DATE to Date and Region to REGION (if you do it's an easy tweak once you explain the logic for selecting which tags to change) and that you really do want a , at the end of every output line except the last (again, an easy tweak if you don't), then using any awk in any shell on every Unix box: $ cat tst.awk NR==1 { ...


4

Assuming that the fields are always in the given order, that there is a header line on the first line of input, and that multiple space characters delimit fields, you may compress the runs of consecutive spaces with tr and parse the data with jq. database-client-command | tr -s ' ' | jq -c -Rn ' input | split(" ") as $head | inputs ...


2

Here's one way. Save the following as foo.awk: { if(NR==1){ for(i=1;i<=NF;i++){ head[i]=$i } } else{ printf "{\"%s\":\"%s\",\"%s\":\"%s\",\"DATA\":{\"%s\":%s,\"%s\":%s,\"%s\":%s,\"%s\":\"%s\",\"%s\":\"%s\"}}\...


0

I've created a jq inspired tool in python, which has access to all python functions. $ pip install jf With your data in test.jsonl $ jf -c --import hashlib '{data_1: .data_1, target_number: hashlib.md5(str(.target_number).encode()).hexdigest(), extra_data: .extra_data}' test.jsonl {"data_1": 210, "target_number": "...


1

Assuming this is part of a JSON file, the following jq command would find all objects with a trait_type key and a value key and delete all of those objects where the value key has the value None. jq 'del( .. | select( type == "object" and has("trait_type") and has("value") and .value == "None" ...


Top 50 recent answers are included