LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   JSON Help (https://www.linuxquestions.org/questions/programming-9/json-help-4175659018/)

bradvan 08-12-2019 09:08 AM

JSON Help
 
I have a JSON that I am trying to edit. An array within the JSON has multiple elements with the same name and same sub-values. I want to edit only one of them in a bash script. Here is the JSON:
Code:

{
  "created": null,
  "category": "server-profiles",
  "serialNumberType": "Physical",
  "wwnType": "Physical",
  "macType": "Physical",
  "firmware": {
    "reapplyState": "NotApplying",
    "firmwareActivationType": "Scheduled",
    "firmwareScheduleDateTime": null,
    "firmwareInstallType": "FirmwareAndOSDrivers",
    "forceInstallFirmware": false,
    "manageFirmware": true,
    "firmwareBaselineUri": "/rest/firmware-drivers/spp-2019_03_0_85"
  },
  "hideUnusedFlexNics": null,
  "associatedServer": null,
  "affinity": null,
  "enclosureBay": null,
  "enclosureUri": null,
  "enclosureGroupUri": null,
  "serverHardwareTypeUri": "/rest/server-hardware-types/C309ABA9-3DFE-464E-9930-F4D61225C417",
  "serverHardwareUri": "/rest/server-hardware/30393137-3436-4536-34323939394C",
  "templateCompliance": "Unknown",
  "serverProfileTemplateUri": "/rest/server-profile-templates/28fa126e-bd5d-44ff-9b4c-1710555f8dcc",
  "bootMode": {
    "secureBoot": "Unmanaged",
    "pxeBootPolicy": "IPv4",
    "mode": "UEFIOptimized",
    "manageMode": true
  },
  "connectionSettings": {
    "connections": [],
    "reaplyState": "NotApplying"
  },
  "taskUri": null,
  "inProgress": false,
  "serverHardwareReapplyState": "NotApplying",
  "state": "Creating",
  "status": "OK",
  "modified": null,
  "refreshState": "NotRefreshing",
  "eTag": null,
  "osDeploymentSettings": null,
  "sanStorage": {
    "reaplyState": "NotApplying",
    "sanSystemCredentials": [],
    "volumeAttachments": [],
    "manageSanStorage": false
  },
  "localStorage": {
    "reaplyState": "NotApplying",
    "controllers": [],
    "sasLogicalJBODs": []
  },
  "managementProcessor": {
    "mpSettings": [
      {
        "args": {
          "password": null,
          "deleteAdministratorAccount": false
        },
        "settingType": "AdministratorAccount"
      },
      {
        "args": {
          "kerberosAuthentication": false,
          "directoryUserContext": [
            "dc=my,dc=com"
          ],
          "directoryServerPort": 636,
          "directoryServerAddress": "direct-serve.my.com",
          "password": null,
          "iloObjectDistinguishedName": "mysid",
          "directoryGenericLDAP": true,
          "directoryAuthentication": "defaultSchema"
        },
        "settingType": "Directory"
      },
      {
        "args": {
          "directoryGroupAccounts": [
            {
              "iLOConfigPriv": true,
              "virtualPowerAndResetPriv": true,
              "virtualMediaPriv": true,
              "remoteConsolePriv": true,
              "userConfigPriv": true,
              "groupSID": "S-1-5-22-2822724604-3899611472-1493707958-52883",
              "groupDN": "mygroup_OneView"
            },
            {
              "iLOConfigPriv": false,
              "virtualPowerAndResetPriv": true,
              "virtualMediaPriv": true,
              "remoteConsolePriv": true,
              "userConfigPriv": false,
              "groupSID": "S-1-5-22-2822724604-3899611472-1493707958-40755",
              "groupDN": "mygroup_OneView_RO"
            }
          ]
        },
        "settingType": "DirectoryGroups"
      }
    ],
    "manageMP": true,
    "reapplyState": "NotApplying"
  },
  "bios": {
    "reapplyState": "NotApplying",
    "overriddenSettings": [
      {
        "value": "NoState",
        "id": "MinProcIdlePkgState"
      },
      {
        "value": "MaxPerf",
        "id": "EnergyPerBios"
      }
    ],
    "manageBios": true
  },
  "boot": {
    "order": [],
    "manageBoot": false
  },
  "type": "ServerProfileV10",
  "uri": null,
  "name": "my-server",
  "description": "DL380 G9",
  "serialNumber": null,
  "uuid": null,
  "iscsiInitiatorName": null,
  "iscsiInitiatorNameType": "AutoGenerated"
}

I need to edit the password in the same section where kerberosAuthtication is. When I ran:
Code:

echo ${NEW_PROF} | jq -r '.managementProcessor.mpSettings[].args.password="mypassword"'
it says it cannot iterate over null. If I put in an index of 0, it just adds a new section. Does anyone know the correct method to edit this value?

NevemTeve 08-12-2019 11:21 AM

Maybe PHP would be a better choice than bash for processing JSON data.

Contrapak 08-12-2019 12:47 PM

Better yet, just use JavaScript. It is JavaScript Object Notation after all.

individual 08-12-2019 09:18 PM

It can be done with jq, but the solution is somewhat complex.
Code:

jq -r '.managementProcessor.mpSettings |= (map(if .args?.kerberosAuthentication? != null then .args.password="newPassword" else . end))'
Here we update .managementProcessor.mpSettings with the result of the operation on the right-hand side. In this case, we iterate over mpSettings and update the password to "newPassword" if the key kerberosAuthentication exists. Otherwise just print the current object.

Here is a test run. Notice that the only thing changed is the password.
Code:

jq -r '.managementProcessor.mpSettings |= (map(if .args?.kerberosAuthentication? != null then .args.password="newPassword" end))' < data.json | diff - data.json
73c73
<          "password": "newPassword",
---
>          "password": null,


bradvan 08-13-2019 03:43 AM

PHP or Javascript might be better, but I don't know either of those. Individual, thanks for that! I'll give it a whirl! :)
Follow-up: My version of jq did not seem to like the '?' character. It flagged it as invalid each time. When I simply removed it, the update worked. My version of jq is 1.3. Thanks again!

individual 08-13-2019 06:25 AM

Quote:

Originally Posted by bradvan (Post 6024602)
Follow-up: My version of jq did not seem to like the '?' character. It flagged it as invalid each time. When I simply removed it, the update worked. My version of jq is 1.3. Thanks again!

I should have asked what version you had installed. If you are going to be using jq a lot, I would recommend upgrading to the latest version.

syg00 08-13-2019 06:33 AM

I can't spell JSON, but I keep seeing references to jq here - looks like even I can get the general gist. Looks like a great tool.

Contrapak 08-13-2019 08:01 PM

Quote:

Originally Posted by syg00 (Post 6024630)
I can't spell JSON, but I keep seeing references to jq here - looks like even I can get the general gist. Looks like a great tool.

jq is fantastic. The syntax is a little intimidating, but once you get over it, it's truly great.


All times are GMT -5. The time now is 07:36 AM.