3

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
  • 440
  • 2
  • 10
  • 28
  • What is expected output? – Rao Jan 05 '18 at 11:01
  • In expected Output, I am getting only this "Key" = "max" removed that too without quotes.. Like this :- "": "12".. But the solution @cfrick has given is correct.. But it is not removing these "goBus": { "first": 12800, "second": 11900, "third": 12800 }, with the same approach. – avidCoder Jan 05 '18 at 11:11
  • It would help if you can put exact desired json than explanation. – Rao Jan 05 '18 at 11:50
  • I have added it @rao – avidCoder Jan 05 '18 at 12:11
  • No, I just want to remove this goBus object from the desiredJson. @nitin – avidCoder Jan 05 '18 at 12:16
  • I do not see the requested info though you say added it. Asking the expected result explicitly because, you mentioned to remove '['. So, appreciate it if you can show the expected json. – Rao Jan 05 '18 at 12:27

2 Answers2

3

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)

cfrick
  • 35,203
  • 6
  • 56
  • 68
  • 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 Jan 05 '18 at 10:50
  • @Rao - it is working here with cfrick code.. But the goBus object is not getting removed with the same approach. – avidCoder Jan 05 '18 at 12:23
  • I missed the array in question. You have to use an explicit spread operator do your work on each element. – cfrick Jan 05 '18 at 13:26
  • 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? – avidCoder Jan 05 '18 at 13:52
  • 1
    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()` – cfrick Jan 05 '18 at 13:54
  • Sorry, I am not getting.. Is this what you are suggesting to remove square bracket "[ ]" @cfrick – avidCoder Jan 05 '18 at 14:02
  • 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`. – cfrick Jan 05 '18 at 14:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162604/discussion-between-avidcoder-and-cfrick). – avidCoder Jan 05 '18 at 14:06
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) ​
Nitin Dhomse
  • 2,524
  • 1
  • 12
  • 24
  • 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. – avidCoder Jan 05 '18 at 12:34
  • 1
    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. – Nitin Dhomse Jan 05 '18 at 12:39