Home > Net >  How can I change a json structure into an object and rename keys with jq
How can I change a json structure into an object and rename keys with jq

Time:05-19

Using jq I am trying to convert the rawe json below into the desired json outcome. Objectives: name renamed to pathParameterName type renamed to datasetParameter

Raw Json I'm trying to convert

{
"pathOptions": {
        "parameters": {
            "raw_date": {
                "name": "raw_date",
                "type": "Datetime",
                "datetimeOptions": {
                    "localeCode": "en-GB"
                },
                "createColumn": true,
                "filter": {
                    "expression": "(after :date1)",
                    "valuesMap": {
                        ":date1": "2022-03-08T00:00:00.000Z"
                    }
                }
            }
        }
    }
  }

Json desired outcome:

{
  "pathOptions": {
    "parameters": [
      {
        "pathParameterName": "raw_date",
        "datasetParameter": {
          "name": "raw_date",
          "type": "Datetime",
          "datetimeOptions": {
            "localeCode": "en-GB"
          },
          "createColumn": true,
          "filter": {
            "expression": "(after :date1)",
            "valuesMap": [
              {
                "valueReference": ":date1",
                "value": "2022-03-08T00:00:00.000Z"
              }
            ]
          }
        }
      }
    ]
  }
 }


This is what I have so far:

map_values(if type == "object" then to_entries else . end)

This is what my code above currently produces. -I'm struggling with the key renaming.

{
  "pathOptions": [
    {
      "key": "parameters",
      "value": [
        {
          "pathParameterName": "raw_date",
          "datasetParameter": {
            "name": "raw_date",
            "type": "Datetime",
            "datetimeOptions": {
              "localeCode": "en-GB"
            },
            "createColumn": true,
            "filter": {
              "expression": "(after :date1)",
              "valuesMap": [
                {
                  "valueReference": ":date1",
                  "value": "2022-03-08T00:00:00.000Z"
                }
              ]
            }
          }
        }
      ]
    }
  ]
}

CodePudding user response:

The function to_entries, "converts between an object and an array of key-value pairs" (see the manual). To rename the preset key and value fields, just reassign them to a new name with a new object as in {valueReference: .key, value}.

jq '
  .pathOptions.parameters |= (
    to_entries | map({
      pathParameterName: .key,
      datasetParameter: (
        .value | .filter.valuesMap |= (
          to_entries | map({valueReference: .key, value})
        )
      )
    })
  )
'
{
  "pathOptions": {
    "parameters": [
      {
        "pathParameterName": "raw_date",
        "datasetParameter": {
          "name": "raw_date",
          "type": "Datetime",
          "datetimeOptions": {
            "localeCode": "en-GB"
          },
          "createColumn": true,
          "filter": {
            "expression": "(after :date1)",
            "valuesMap": [
              {
                "valueReference": ":date1",
                "value": "2022-03-08T00:00:00.000Z"
              }
            ]
          }
        }
      }
    ]
  }
}

Demo

  • Related