import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
def someJSONString = '{"films":[{"5":[5,3,4,5,6]}, {"4":[1,2]}]}}'
def slurper = new JsonSlurper()
def doc = slurper.parseText(someJSONString)
for(films in doc.films){
if(setFilm || setSec)
break
def secs = films.get(cfid)
if(secs != null){
setFilm = true
setSec = true
for(sec in secs){
if(sec == csec)
setSec = false
}
if(setSec)
secs.add(csec)
}
}
if(!setFilm){
def lhm = new LinkedHashMap();
def al = new ArrayList();
al.add(csec)
lhm.put(cfid,al)
doc.films += lhm;
}
def out= new File('ex.txt'); out.createNewFile(); out << doc;
println doc