How to remove the JSON array, brackets, key and value using replaceAll method?

10,510

Solution 1

There usually is no reason to that with string replacements -- it has just to much potential to mess something up. You can just modify the map before writing it back as JSON. E.g.:

import groovy.json.*

def jsonStr = '{"a": 1, "b": [{"c": 3, "d": 4}]}}'
def json = new JsonSlurper().parseText(jsonStr)
// XXX: first "de-array" `b`
json.b = json.b.first()
// next remove `c` from it
json.b.remove('c')
println JsonOutput.toJson(json)
// => {"a":1,"b":{"d":4}}

edit:

OP also wants to get rid of the array, altough this messes with the naming and only works if there is at least one element (see comments)

Solution 2

This is the working solution with your desired output

Working code here Working example

import groovy.json.* 
def jsonStr = '''{
"count": 4,
"max": "12",
"min": 0,
"details": [{
    "goBus": {
        "first": 12800,
        "second": 11900,
        "third": 12800
    },
    "goAir": {
        "first": 12800,
        "second": 11900,
        "third": 12800
    },
    "gotTrain": {
        "first": 12800,
        "second": 11900,
        "third": 12800,
        "fourth": 13000
    },
    "sell": true,
    "darn": 2,
    "rate": [{
        "busRate": 11900,
        "flag": false,
        "percent": 0
        }]
    }]
}'''

def json = new JsonSlurper().parseText(jsonStr) 
json.details[0].remove('goBus') 
println JsonOutput.toJson(json) ​
Share:
10,510
avidCoder
Author by

avidCoder

Passionate coder, Always ready to learn new things and a debugger.

Updated on June 05, 2022

Comments

  • avidCoder
    avidCoder almost 2 years

    I have following JSON as an output:-

    def desiredJson = '{"count": 4, "max": "12", "min": 0, "details": [{"goBus": {"first": 12800, "second": 11900, "third": 12800},"goAir": {"first": 12800, "second": 11900, "third": 12800}, "gotTrain": {"first": 12800, "second": 11900},"sell": true, "darn": 2,"rate": [{ "busRate": 11900, "flag": false, "percent": 0}],}],}'
    

    I want to remove "count" key and its value, remove

    "goBus": {
        "first": 12800,
        "second": 11900,
        "third": 12800
    },
    

    And remove square brackets of "details" node.

    I have tried below code to remove and replace as null:-

    def slurper = new JsonSlurper();
    def json = slurper.parse(file)
    
    def newjson = JsonOutput.toJson(json).toString()
    
    String j = "max"
    newjson = newjson.replaceAll(""+ j +"", "")
    
    log.info newjson
    

    As an output, the max value is not getting removed. Or Is there any other way we can remove these all things from JSON.

    Can anybody help me on this?

    I have tried this also:-

    def json = new JsonSlurper().parseText(desiredJson)
    def njson =  json.details.goBus
    
    def pjson = njson.remove()
    
    log.info JsonOutput.toJson(pjson)
    

    It is returning false.

  • avidCoder
    avidCoder over 6 years
    This is fine. but in my case, I want to remove "goBus": { "first": 12800, "second": 11900, "third": 12800 }, which is inside "details" array node.. How should I process for this?
  • avidCoder
    avidCoder over 6 years
    @Rao - it is working here with cfrick code.. But the goBus object is not getting removed with the same approach.
  • avidCoder
    avidCoder over 6 years
    That's perfect @Nitin, Is there anyway, I could make it dynamic.. Say, If you have goBus sometimes at other position.. In that case, what would you suggest.
  • Nitin Dhomse
    Nitin Dhomse over 6 years
    Here, the 0th position will be fine if you don't have the nested objects (which you want to remove) inside "details", keep json structure consistent if possible.
  • cfrick
    cfrick over 6 years
    I missed the array in question. You have to use an explicit spread operator do your work on each element.
  • avidCoder
    avidCoder over 6 years
    Thanks, I had resolved it.. I have asked about removing the square bracket of "rate" array from the desiredJson.. else removing the square bracket for "b" array would suffice. Can we remove it?
  • cfrick
    cfrick over 6 years
    This makes only sense, if there only ever to be just one details, which does not sound like it (plural). So you can do with a rule like "always the first". e.g. json.details = json.details.first()
  • avidCoder
    avidCoder over 6 years
    Sorry, I am not getting.. Is this what you are suggesting to remove square bracket "[ ]" @cfrick
  • cfrick
    cfrick over 6 years
    Yes, this will replace the array on details with just the first element in details. But I find this very misleading. I'd rather add detail and and then delete details.
  • avidCoder
    avidCoder over 6 years