none
Json child element value not removed RRS feed

  • General discussion

  • I want to remove cities Uthah,NewYork  from main.json file below, the function is unable to remove it 

    {
        "quiz": {
            "sport": {
                "q1": {
                    "question": "Which one is correct team name in NBA?",
                    "options": [
                        "NewYork",
                        "LosAngeles",
                        "Uthah",
                        "Dallas"
                    ],
                    "answer": "Huston Rocket"
                }
            },
            "maths": {
                "q1": {
                    "question": "5 + 7 = ?",
                    "options": [
                        "Newack",
                        "LosAngeles",
                        "Charlston",
                        "Barbados"
                    ],
                    "answer": "12"
                },
                "q2": {
                    "question": "12 - 8 = ?",
                    "options": [
                        "NewOrlance",
                        "LosAngeles",
                        "Uthah",
                        "Dallas"
                    ],
                    "answer": "4"
                }
            }
        }
    }

    $getjson=gc '.\main.json' | Convertfom-json
    $options=$getjson.quiz.sport.q1.options
    $city=@('Uthah','NewYork')
    
    foreach ($o in $options) 
    {
        foreach ($c in $city)
        {
            if($o -match $c) 
            {
                    $options = $options | Where-Object { $_ –ne $o }
                    $getjson | ConvertTo-Json -Depth 100 
            }
                
        }
     }
    $getjson.quiz.sport.q1.options=$options
    $getjson | Set-Content -Path '.\main.json' -Force



    Wednesday, September 18, 2019 6:40 PM

All replies

  • "Utah" is spelled wrong.

    This is tricky for non-programmers but here is how to do this the easy way.

    $obj = @'
    {
        "quiz": {
            "sport": {
                "q1": {
                    "question": "Which one is correct team name in NBA?",
                    "options": [
                        "NewYork",
                        "LosAngeles",
                        "Uthah",
                        "Dallas"
                    ],
                    "answer": "Huston Rocket"
                }
            },
            "maths": {
                "q1": {
                    "question": "5 + 7 = ?",
                    "options": [
                        "Newack",
                        "LosAngeles",
                        "Charlston",
                        "Barbados"
                    ],
                    "answer": "12"
                },
                "q2": {
                    "question": "12 - 8 = ?",
                    "options": [
                        "NewOrlance",
                        "LosAngeles",
                        "Uthah",
                        "Dallas"
                    ],
                    "answer": "4"
                }
            }
        }
    }
    '@ | ConvertFrom-Json
    
    $city = 'Uthah', 'NewYork'
    $obj.quiz.maths.q2.options = $options | where{ $_ -notin $city }
    $obj.quiz.sport.q1.options = $options | where{ $_ -notin $city } $newjson = $obj | ConvertTo-Json



    \_(ツ)_/


    • Edited by jrv Wednesday, September 18, 2019 8:11 PM
    Wednesday, September 18, 2019 8:09 PM
  • $obj= @'
    {
        "quiz" : [
                    {
                        "game": "NFS",
                        "computer": "domain-1",
                        "producers" : "INTL-1",
                        "options": [
                                     {
                                        "name": "USA-A",
                                        "city": [
                                                 "NewYork",
                                                 "Raleigh",
                                                 "Charlston"
                                                ]
                                     },
                                     {
                                        "name": "USA-B",
                                        "city": [
                                                 "LosAngles",
                                                 "Uthah",
                                                 "Dortmond"
                                                ]
                                     }
                                  ]
                              
                    },
                    {
                        "game": "FIFA",
                        "computer": "domain-2",
                        "producers": "INTL-2",
                        "options": [
                                     {
                                        "name": "USA-C",
                                        "city": [
                                                 "Newack",
                                                 "Uthah",
                                                 "Charlston"
                                                ]
                                     },
                                     {
                                        "name": "USA-D",
                                        "city": [
                                                 "NewYork",
                                                 "Uthah",
                                                 "Carolina"
                                                ]
                                     }
                                  ]
                    
                    }
            ]
    }
    '@ | ConvertFrom-Json
    
    $city = 'Uthah', 'NewYork'
    $obj.quiz.options = $options | where{ $_ -notin $city }
    $obj.quiz.options = $options | where{ $_ -notin $city }
    $newjson = $obj | ConvertTo-Json
      
            I want to remove NewYork and Uthah in json using powershell, the code is not working and 
            throwing an error $options is not exists in current context, is there any way to make it work, I 
            am using powershell 4.0
             
    Friday, September 20, 2019 7:10 PM
  • DUPLICATE POST: https://social.technet.microsoft.com/Forums/scriptcenter/en-US/f45c0846-e05a-4ed8-87cc-ae228b1d3452/json-child-element-value-not-removed?forum=ITCG#f5c7823a-48d4-4e26-9c87-d66393da762c

    I gave you the answer of how to do this in your first post. Please read and try to understand the instructions.  Changing the request does not change the solution.


    \_(ツ)_/

    Friday, September 20, 2019 7:23 PM
  • "Utah" is spelled wrong.

    This is tricky for non-programmers but here is how to do this the easy way.

    $obj = @'
    {
        "quiz": {
            "sport": {
                "q1": {
                    "question": "Which one is correct team name in NBA?",
                    "options": [
                        "NewYork",
                        "LosAngeles",
                        "Uthah",
                        "Dallas"
                    ],
                    "answer": "Huston Rocket"
                }
            },
            "maths": {
                "q1": {
                    "question": "5 + 7 = ?",
                    "options": [
                        "Newack",
                        "LosAngeles",
                        "Charlston",
                        "Barbados"
                    ],
                    "answer": "12"
                },
                "q2": {
                    "question": "12 - 8 = ?",
                    "options": [
                        "NewOrlance",
                        "LosAngeles",
                        "Uthah",
                        "Dallas"
                    ],
                    "answer": "4"
                }
            }
        }
    }
    '@ | ConvertFrom-Json
    
    $city = 'Uthah', 'NewYork'
    $obj.quiz.maths.q2.options = $options | where{ $_ -notin $city }
    $obj.quiz.sport.q1.options = $options | where{ $_ -notin $city } $newjson = $obj | ConvertTo-Json



    \_(ツ)_/


    for slightly different json the code is not working also I have done all tweeks still there is no any specific solution I have found to work on it 

    $obj= @'
    {
        "quiz" : [
                    {
                        "game": "NFS",
                        "computer": "domain-1",
                        "producers" : "INTL-1",
                        "options": [
                                     {
                                        "name": "USA-A",
                                        "city": [
                                                 "NewYork",
                                                 "Raleigh",
                                                 "Charlston"
                                                ]
                                     },
                                     {
                                        "name": "USA-B",
                                        "city": [
                                                 "LosAngles",
                                                 "Uthah",
                                                 "Dortmond"
                                                ]
                                     }
                                  ]
                              
                    },
                    {
                        "game": "FIFA",
                        "computer": "domain-2",
                        "producers": "INTL-2",
                        "options": [
                                     {
                                        "name": "USA-C",
                                        "city": [
                                                 "Newack",
                                                 "Uthah",
                                                 "Charlston"
                                                ]
                                     },
                                     {
                                        "name": "USA-D",
                                        "city": [
                                                 "NewYork",
                                                 "Uthah",
                                                 "Carolina"
                                                ]
                                     }
                                  ]
                    
                    }
            ]
    }
    '@ | ConvertFrom-Json
    
    $city = 'Uthah', 'NewYork'
    $obj.quiz.options = $options | where{ $_ -notin $city }
    $obj.quiz.options = $options | where{ $_ -notin $city }
    $newjson = $obj | ConvertTo-Json

    Saturday, September 21, 2019 6:19 AM
  • I understand, you can please remove current thread, feeling sorry for giving you extra work, I have post same question on previous post now please give me answer there because I have done all possible tweeks (basically on the msdn forums I don't understand posting discussion and posting a thread now I got it)
    • Edited by himanshu_kulkarni Saturday, September 21, 2019 6:36 AM grammar mistake remove
    Saturday, September 21, 2019 6:24 AM
  • Your new json has "city" as the target and options as the parent.


    \_(ツ)_/

    Saturday, September 21, 2019 6:35 AM
  • $obj.quiz.options[0].city


    \_(ツ)_/

    Saturday, September 21, 2019 7:11 AM
  • I was already tried this one but convert to json at last step with removed value is not successful
    Sunday, September 22, 2019 12:47 AM