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..."
 
Line 69: Line 69:


=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">

Revision as of 03:37, 30 October 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 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'