1

I have following object:

{
  "base": "/templates",
  "title": "Templates",
  "categories": {
    "category1": {
      "propertiesResources": {
        "resources": {
          "cliTemplate": {
            "type": "CLIENT",
            "path": "properties/cli.json",
            "roles": [
              "ROLE_SYSTEM_READ",
              "ROLE_SYSTEM_WRITE"
            ]
          },
          "viewerTemplate": {
            "type": "SERVER",
            "path": "properties/server.json",
            "roles": [
              "ROLE_CONFIGURATOR_READ",
              "ROLE_CONFIGURATOR_WRITE"
            ]
          }
        }
      }
    }
  }
}

As you can see the field roles is an array which contains list of roles. The roles are guaranteed to be in this order. What I need is to replace the field roles with two new fields: roleRead and roleWrite where this new fields will have corresponding values from the array.

{
  "base": "/templates",
  "title": "Templates",
  "categories": {
    "category1": {
      "propertiesResources": {
        "resources": {
          "cliTemplate": {
            "type": "CLIENT",
            "path": "properties/cli.json",
            "readRole": "ROLE_SYSTEM_READ",
            "writeRole": "ROLE_SYSTEM_WRITE"
          },
          "viewerTemplate": {
            "type": "SERVER",
            "path": "properties/server.json",
            "readRole": "ROLE_CONFIGURATOR_READ",
            "writeRole": "ROLE_CONFIGURATOR_WRITE"
          }
        }
      }
    }
  }
}

Using this command I was able to get half way there

.categories[][].resources[].roles|={"readRole": .[0], "writeRole": .[1]}

https://jqplay.org/s/QPRMdXR0Os

1 Answer 1

2

One way would be to add the objects together with the .roles array and delete it after

.categories[][].resources[] |= ( 
  . + { readRole: .roles[0], writeRole: .roles[1] } | del(.roles)
)

jqplay demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.