Home > Mobile >  Remove/delete json value (multiple array)
Remove/delete json value (multiple array)

Time:04-08

I want to remove 'Software 3' in the array. I'm quite new with json data and I do not know what is the correct way to do it. I have tried the unset method but its not working. Can anyone show me the example. I already search the solution but most of it not using multiple array.

json data:

"system_info": {
"hardware": {
  "model": "PowerEdge R710",
  "serialno": "FG6MC2S",
  "warranty": {
    "warranty_start_date": "2022-03-14",
    "warranty_end_date": "2025-03-14"
  }
},
"operating_env": {
  "os": "PowerEdge R710",
  "os_version": "FG6MC2S"
},
"software_installed": [
  {
    "name": "Software 1",
    "version": "4.5"
  },
  {
    "name": "Software 2",
    "version": "5.5"
  },
  {
    "name": "Software 3",
    "version": "5.5"
  }
]},

CodePudding user response:

Using python. I will assume that this data lives in data.json

remove_script.py

import json

# Opening JSON file
with open('data.json') as json_file:
    data = json.load(json_file)

    # Remove software 3
    data['system_info'].update(
        {"software_installed": data['system_info']['software_installed'][:2]})

    print(data)

    # write to another file
    with open("output.json", "w") as write_file:
        json.dump(data, write_file, indent=4)

printed output

>>> {'system_info': {'hardware': {'model': 'PowerEdge R710', 'serialno': 'FG6MC2S', 'warranty': {'warranty_start_date': '2022-03-14', 'warranty_end_date': '2025-03-14'}}, 'operating_env': {'os': 'PowerEdge R710', 'os_version': 'FG6MC2S'}, 'software_installed': [{'name': 'Software 1', 'version': '4.5'}, {'name': 'Software 2', 'version': '5.5'}]}}

output.json

{
    "system_info": {
        "hardware": {
            "model": "PowerEdge R710",
            "serialno": "FG6MC2S",
            "warranty": {
                "warranty_start_date": "2022-03-14",
                "warranty_end_date": "2025-03-14"
            }
        },
        "operating_env": {
            "os": "PowerEdge R710",
            "os_version": "FG6MC2S"
        },
        "software_installed": [
            {
                "name": "Software 1",
                "version": "4.5"
            },
            {
                "name": "Software 2",
                "version": "5.5"
            }
        ]
    }
}

CodePudding user response:

Using javascript

const data = {
  system_info: {
    hardware: {
      model: 'PowerEdge R710',
      serialno: 'FG6MC2S',
      warranty: {
        warranty_start_date: '2022-03-14',
        warranty_end_date: '2025-03-14',
      },
    },
    operating_env: {
      os: 'PowerEdge R710',
      os_version: 'FG6MC2S',
    },
    software_installed: [
      {
        name: 'Software 1',
        version: '4.5',
      },
      {
        name: 'Software 2',
        version: '5.5',
      },
      {
        name: 'Software 3',
        version: '5.5',
      },
    ],
  },
};
const { system_info: { hardware, operating_env, software_installed } } = data;
const newData = {
  system_info: {
    hardware,
    operating_env,
    software_installed: software_installed.filter(s => s.name !== 'Software 3')
  }
}
console.log(newData);
  • Related