Jq oneliners: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Created page with "=Introduction= This page is to remind me of jq one liners =Sample Data= <syntaxhighlight lang="json"> { "id": "123", "code": "123", "headers": [ { "id": "1", "testDimension": "AB", "testLines": [ { "id": "1", "test1Dimension": "1", "test2Dimension": "2", "test3Dimension": "3" } ], "shortcutCode1": "Code1", "shortcutCode2": "Code2" }, { "id": "2", "tes..."
 
 
(One intermediate revision by the same user not shown)
Line 52: Line 52:
}
}
</syntaxhighlight>
</syntaxhighlight>
=Show the records which are are BC=
To show AB records
<syntaxhighlight lang="bash">
cat test4.json | jq '.headers[] | select(.testDimension == "AB")'
</syntaxhighlight>
=Show the records which are not BC=  
=Show the records which are not BC=  
To remove the AB you can do this
To remove the AB you can do this
Line 57: Line 63:
cat test4.json | jq '.headers[] | select(.testDimension != "BC")'
cat test4.json | jq '.headers[] | select(.testDimension != "BC")'
</syntaxhighlight>
</syntaxhighlight>
=Delete the records all records accept AB=  
=Delete the records all records accept AB=  
To remove the AB you can do this
To remove the AB you can do this
Line 69: Line 76:


=Count the Records to be deleted=  
=Count the Records to be deleted=  
For me I guess if I was to use jq to manipulate data it is handy to count stuff.<br>
<br>
First, list the current count of all records with no -s
<syntaxhighlight lang="bash">
cat test4.json | jq '. | length'
</syntaxhighlight>
First list them by removing the del( ... )
First list them by removing the del( ... )
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">

Latest revision as of 02:26, 7 November 2024

Introduction

This page is to remind me of jq one liners

Sample Data

{
  "id": "123",
  "code": "123",
  "headers": [
    {
      "id": "1",
      "testDimension": "AB",
      "testLines": [
        {
          "id": "1",
          "test1Dimension": "1",
          "test2Dimension": "2",
          "test3Dimension": "3"
        }
      ],
      "shortcutCode1": "Code1",
      "shortcutCode2": "Code2"
    },
    {
      "id": "2",
      "testDimension": "AB",
      "testLines": [
        {
          "id": "1",
          "test1Dimension": "1",
          "test2Dimension": "2",
          "test3Dimension": "3"
        }
      ],
      "shortcutCode1": "Code1",
      "shortcutCode2": "Code2"
    },
    {
      "id": "3",
      "testDimension": "BC",
      "testLines": [
        {
          "id": "1",
          "test1Dimension": "1",
          "test2Dimension": "2",
          "test3Dimension": "3"
        }
      ],
      "shortcutCode1": "Code1",
      "shortcutCode2": "Code2"
    }
  ]
}

Show the records which are are BC

To show AB records

cat test4.json | jq '.headers[] | select(.testDimension == "AB")'

Show the records which are not BC

To remove the AB you can do this

cat test4.json | jq '.headers[] | select(.testDimension != "BC")'

Delete the records all records accept AB

To remove the AB you can do this

cat test4.json | jq 'del(.headers[] | select(.testDimension != "AB"))'

Count the AB Records

cat test4.json | jq 'del(.headers[] | select(.testDimension != "AB"))' | jq '.headers | length'

Count the Records to be deleted

For me I guess if I was to use jq to manipulate data it is handy to count stuff.

First, list the current count of all records with no -s

cat test4.json | jq '. | length'

First list them by removing the del( ... )

cat test4.json | jq '.headers[] | select(.testDimension != "AB")'

Then use can count them with the -s option

cat test4.json | jq '.headers[] | select(.testDimension != "AB")' | jq -s '. | length'