Home > database >  Looking for a way to capitalize/decapitalize a particular object or values of particular objects in
Looking for a way to capitalize/decapitalize a particular object or values of particular objects in

Time:02-28

I have been trying to find a way to somehow convert the values of a particular object in my json file to Capital.lower case. The json is something like this

"objects": {
    "1": {
      "type": "GeometryCollection",
      "geometries": [
        {
          "arcs": [
            [
              0,
              1,
              2
            ]
          ],
          "type": "Polygon",
          "properties": {
            "state_name": "ARUNACHAL PRADESH"
          }
        }
      ]
    },
    "2": {
      "type": "GeometryCollection",
      "geometries": [
        {
          "arcs": [
            [
              3,
              4,
              5,
              6,
              7,
              8
            ],
            [
              9
            ]
          ],
          "type": "Polygon",
          "properties": {
            "state_name": "ANDHRA PRADESH"
          }
        }
      ]
    },

I would like the state_name values to be converted to lower case in the json file, and type values to be capitalized so that the final output looks like

"objects": {
    "1": {
      "type": "GeometryCollection",
      "geometries": [
        {
          "arcs": [
            [
              0,
              1,
              2
            ]
          ],
          "type": "POLYGON",
          "properties": {
            "state_name": "arunachal pradesh"
          }
        }
      ]
    },
    "2": {
      "type": "GeometryCollection",
      "geometries": [
        {
          "arcs": [
            [
              3,
              4,
              5,
              6,
              7,
              8
            ],
            [
              9
            ]
          ],
          "type": "POLYGON",
          "properties": {
            "state_name": "andhra pradesh"
          }
        }
      ]
    },

The json is complex and is very big, I am wondering if ther's a way with jq to do it or any other command line tool like sed or awk or tr.

CodePudding user response:

You should get jour JSON right. Then, it's easy using jq's ascii_upcase and ascii_downcase functions:

jq '.objects[].geometries[] |= (
  .properties.state_name |= ascii_downcase | .type |= ascii_upcase
)'
{
  "objects": {
    "1": {
      "type": "GeometryCollection",
      "geometries": [
        {
          "arcs": [
            [
              0,
              1,
              2
            ]
          ],
          "type": "POLYGON",
          "properties": {
            "state_name": "arunachal pradesh"
          }
        }
      ]
    },
    "2": {
      "type": "GeometryCollection",
      "geometries": [
        {
          "arcs": [
            [
              3,
              4,
              5,
              6,
              7,
              8
            ],
            [
              9
            ]
          ],
          "type": "POLYGON",
          "properties": {
            "state_name": "andhra pradesh"
          }
        }
      ]
    }
  }
}

Demo

CodePudding user response:

Suggesting awk script:

  awk '/ {10}"type":/ {$NF = tolower($NF);}
       / {12}"state_name":/ {$NF = toupper($NF);}
       1' FS=: OFS=: input.json

Assuming the indentation is consistent!

  • Related