Home > Mobile >  How to get the content of a JSON without showing the names of the key values
How to get the content of a JSON without showing the names of the key values

Time:10-25

I am new to Bash and I am currently trying to get the content of a JSON without showing the names of the key values.

This is how the JSON looks like (part of it):

[
  {
    "V1": 65,
    "V2": "Female",
    "V3": 0.7,
    "V4": 0.1,
    "V5": 187,
    "V6": 16,
    "V7": 18,
    "V8": 6.8,
    "V9": 3.3,
    "V10": 0.9,
    "Class": 1
  },
  {
    "V1": 62,
    "V2": "Male",
    "V3": 10.9,
    "V4": 5.5,
    "V5": 699,
    "V6": 64,
    "V7": 100,
    "V8": 7.5,
    "V9": 3.2,
    "V10": 0.74,
    "Class": 1
  },
  {
    "V1": 62,
    "V2": "Male",
    "V3": 7.3,
    "V4": 4.1,
    "V5": 490,
    "V6": 60,
    "V7": 68,
    "V8": 7,
    "V9": 3.3,
    "V10": 0.89,
    "Class": 1
  }
]

This is my script

#!/bin/bash

echo "Albumin =3";
echo "Age Sex Albumin  Proteins";
echo "******";
echo " "
echo "Women";
echo "--------------";
cat csvjson.json | jq -c '.[] | {V1, V2, V8, V9} | select(.V9 ==3) | select(.V2 =="Female")';
echo " "
echo "Men";
echo "-------------";
cat csvjson.json | jq -c '.[] | {V1, V2, V8, V9} | select(.V9 ==3) | select(.V2 =="Male")';

This is what the script shows

Women
--------------
{"V1":38,"V2":"Female","V8":5.6,"V9":3}
{"V1":38,"V2":"Female","V8":5.6,"V9":3}
{"V1":32,"V2":"Female","V8":6,"V9":3}
{"V1":31,"V2":"Female","V8":6,"V9":3}
{"V1":19,"V2":"Female","V8":5.5,"V9":3}
{"V1":38,"V2":"Female","V8":7,"V9":3}
{"V1":20,"V2":"Female","V8":6.1,"V9":3}
{"V1":32,"V2":"Female","V8":7,"V9":3}
{"V1":42,"V2":"Female","V8":6.7,"V9":3}
 
Men
-------------
{"V1":72,"V2":"Male","V8":7.4,"V9":3}
{"V1":60,"V2":"Male","V8":6.3,"V9":3}
{"V1":33,"V2":"Male","V8":5.4,"V9":3}
{"V1":60,"V2":"Male","V8":6.8,"V9":3}
{"V1":60,"V2":"Male","V8":7.4,"V9":3}
{"V1":60,"V2":"Male","V8":7,"V9":3}
{"V1":72,"V2":"Male","V8":6.2,"V9":3}

And this is what I want to show

Women
--------------
38,Female,3, 5.6
38,Female,3, 5.6
32,Female,3, 6
31,Female,3, 6
19,Female,3, 5.5
38,Female,3, 7
20,Female,3, 6.1
32,Female,3, 7
42,Female,3, 6.7

Men
--------------
72,Male,3, 7.4
60,Male,3, 6.3
33,Male,3, 5.4
60,Male,3, 6.8
60,Male,3, 7.4
60,Male,3, 7
72,Male,3, 6.2

So, how can I hide the key values and only show the content of the JSON after doing the filters I did?

CodePudding user response:

This can be accomplished entirely within jq (although some constraints are not all clear, so please comment and I will update the code):

jq --raw-output '

  group_by(.V2)[]
  | if first.V2 == "Male" then "Men" else "Women" end,
    "--------------",
    (
      .[]
      | select(.V9 == 3.3)    # this filters to matching records
      | [.V1, .V2, .V9, .V8]
      | join(",")
    )

' csvjson.json

Demo

CodePudding user response:

Demo stand-alone jq script and code bloc language highlight for use here in stack sites using pmf's answer.

#!/usr/bin/env -S jq --raw-output --from-file

group_by(.V2)[]
| if first.V2 == "Male" then "Men" else "Women" end,
  "--------------",
  (
    .[]
    | select(.V9 == 3.3)    # this filters to matching records
    | [.V1, .V2, .V9, .V8]
    | join(",")
  )
  • Related