LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2019, 09:08 AM   #1
bradvan
Member
 
Registered: Mar 2009
Posts: 322

Rep: Reputation: 59
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?
 
Old 08-12-2019, 11:21 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,803

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
Maybe PHP would be a better choice than bash for processing JSON data.
 
1 members found this post helpful.
Old 08-12-2019, 12:47 PM   #3
Contrapak
Member
 
Registered: May 2019
Location: /home/
Distribution: Arch Linux
Posts: 126

Rep: Reputation: 28
Better yet, just use JavaScript. It is JavaScript Object Notation after all.
 
1 members found this post helpful.
Old 08-12-2019, 09:18 PM   #4
individual
Member
 
Registered: Jul 2018
Posts: 231

Rep: Reputation: 175Reputation: 175
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,

Last edited by individual; 08-12-2019 at 09:28 PM. Reason: Updated to use diff output instead of pasting the full result JSON.
 
2 members found this post helpful.
Old 08-13-2019, 03:43 AM   #5
bradvan
Member
 
Registered: Mar 2009
Posts: 322

Original Poster
Rep: Reputation: 59
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!

Last edited by bradvan; 08-13-2019 at 04:33 AM. Reason: update after trying the suggested fix
 
Old 08-13-2019, 06:25 AM   #6
individual
Member
 
Registered: Jul 2018
Posts: 231

Rep: Reputation: 175Reputation: 175
Quote:
Originally Posted by bradvan View Post
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.
 
Old 08-13-2019, 06:33 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,001

Rep: Reputation: 2873Reputation: 2873Reputation: 2873Reputation: 2873Reputation: 2873Reputation: 2873Reputation: 2873Reputation: 2873Reputation: 2873Reputation: 2873Reputation: 2873
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.
 
Old 08-13-2019, 08:01 PM   #8
Contrapak
Member
 
Registered: May 2019
Location: /home/
Distribution: Arch Linux
Posts: 126

Rep: Reputation: 28
Quote:
Originally Posted by syg00 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Help with php, JSON and array that has arrays Basher52 Programming 9 10-23-2015 02:21 PM
Help with optimizing a PHP script that processes large amounts of JSON data? Vinter Programming 2 07-13-2014 10:02 PM
json help sajith Programming 1 02-27-2008 06:38 AM
LXer: How to bridge the gap between XML and Ajax with JSON LXer Syndicated Linux News 0 09-14-2006 08:21 PM
php-json fedora 5 hammer65 Fedora 0 07-15-2006 02:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration