I am trying to convert a JSON array into a JSON object, keys of the JSON object are dynamic in nature, please find the example below.
"Section" field in the source array is getting converted to key of object in target JSON
Source:
[
{
"a": 0,
"section": 1.0
},
{
"a": 1,
"section": 1.0
},
{
"a": 2,
"section": 2.0
},
{
"a": 3,
"section": 2.0
},
{
"a": 4,
"section": 3.0
}
]
Target:
{
"1": {
"total": 1,
"data": [
{
"a": 0
},
{
"a": 1
}
]
},
"2": {
"total": 5,
"data": [
{
"a": 2
},
{
"a": 3
}
]
},
"3": {
"total": 4,
"data": [
{
"a": 4
}
]
}
}
You can try this
def list = [
[
"a" : 0,
"section": 1.0
],
[
"a" : 1,
"section": 1.0
],
[
"a" : 2,
"section": 2.0
],
[
"a" : 3,
"section": 2.0
],
[
"a" : 4,
"section": 3.0
]
]
def modified = list.groupBy { it -> (it.section).intValue() }
.collectEntries {[
(it.key.toString()) : ([
data: it.value.collect { it.findAll {it.key != 'section'}},
total: it.value.sum { val -> val.a }])
]}
Related
I want to read hyperopt parameters from a JSON file.
My JSON file would be like:
[
{
"id": "121",
"model": [
{
"model_name": "power",
"estimator_type": [
{
"type": "Polynomial",
"degree": [2, 3, 4]
},
{
"type": "svm",
"C": [0, 1],
"kernel": [
{
"ktype": "linear"
},
{
"ktype": "RBF",
"width": [0, 1]
}
]
}
],
"cut_values": {
"qids": ["1234"]
}
},
{
"model_name": "speed",
"estimator_type": [
{
"type": "Polynomial",
"degree": ["quniform", 2, 3]
}
],
"cut_values": null
}
]
},
{
"id": "123",
"model": [
{
"model_name": "power",
"estimator_type": [
{
"type": "LinearRegression"
}
],
"cut_values": null
}
]
}
]
I have checked this post but with no success for more complex JSON like the one above.
I want to be able to create a space like 2.2 A Search Space Example: scikit-learn.
I am new to python, using python3. I have json data like:
{
"message": {
"count": 46,
"limit": 1000,
"schools": [
{
"class": "1",
"class_id": "1c8***",
"charges": [
{
"cost": 10,
"breakdown": [
{
"books": "1",
"unitQuantity": "10"
}
]
}
],
"area": "maccau"
},
{
"class": "2",
"class_id": "1c3***",
"charges": [
{
"cost": 100,
"breakdown": [
{
"books": "1",
"unitQuantity": "100"
}
]
}
],
"area": "maccau"
},
{
"class": "1",
"class_id": "1c3***",
"charges": [
{
"cost": 10,
"breakdown": [
{
"books": "1",
"unitQuantity": "10"
}
]
}
],
"area": "maccau"
},
{
"class": "2",
"class_id": "1c8***",
"charges": [
{
"cost": 50,
"breakdown": [
{
"books": "1",
"unitQuantity": "50"
}
]
}
],
"area": "maccau"
}
],
"url": {
"link": "/"
}
}
}
I was able to use json.loads to load data and I am trying to get results like:
class Cost
1 20
2 150
I tried converting json to a dictionary:
item_dict = json.load(json_data)
Tried to get data out using for loop and checking if class = 1 and then summing up the cost. But I feel like that is not the best approach. Can someone please tell me what would be the best way of doing this?
I have a service which is producing List of results I want to give the name of each list,find the Example.
Current Result:
[
[
{
"marketname": "East",
"totalmarketcount": 22
},
{
"marketname": "Midwest",
"totalmarketcount": 9275
},
{
"marketname": "West",
"totalmarketcount": 25
}
],
[
{
"discarded": 0,
"received": 1268,
"coded": 6031,
"completed": 5951
}
],
But I want :
{
"EncoderCodedlist": [
{
"totalcount": 6001,
"encountercoder": "name1"
},
{
"totalcount": 41,
"encountercoder": "name8"
},
{
"totalcount": 6,
"encountercoder": "name6"
},
{
"totalcount": 3,
"encountercoder": "name4"
},
{
"totalcount": 3,
"encountercoder": "name2"
}
]
}
I am using Spring Boot and Spring rest, which we produce the json object.
json nested array value parsing. how to retrive the following json to a .obj values
> { "data": {
"stuff": {
"onetype": [
{ "id": 1, "name": "" },
{ "id": 2, "name": "" }
],
"othertype": [
{ "id": 2, "xyz": [-2, 0, 2], "n": "Crab Nebula", "t": 0, "c": 0, "d": 5 }
]
},
"otherstuff": {
"thing":
[[1, 42], [2, 2]]
}
}
}
Am new to Groovy and am having trouble converting an array to JSON. The JSON computed should have all the values from my array list, but it is storing only the last one. Here is the code:
def arraylist = [["0",2],["1",8],["2",6],["3",8],["4",3]]
def arraysize = arraylist.size()
def builder = new groovy.json.JsonBuilder()
builder ({
cols([
{
"id" "hours"
"label" "Hours"
"type" "string"
},
{
"id" "visitor"
"label" "Visitors"
"type" "number"
}
])
rows([
{
for( i in 0..< arraysize )
{
c([
{
"v" arraylist[i][0]
},
{
"v" arraylist[i][1]
}
])
}//for
}
])
})
println builder.toPrettyString()
Can try running the code here:
http://groovyconsole.appspot.com/
Expected output is here:
{
"cols": [
{
"id": "hours",
"label": "Hours",
"type": "string"
},
{
"id": "visitor",
"label": "Visitors",
"type": "number"
}
],
"rows": [
{
"c": [
{
"v": "0"
},
{
"v": 2
}
]
},
{
"c": [
{
"v": "1"
},
{
"v": 8
}
]
},
{
"c": [
{
"v": "2"
},
{
"v": 6
}
]
},
{
"c": [
{
"v": "3"
},
{
"v": 8
}
]
},
{
"c": [
{
"v": "4"
},
{
"v": 3
}
]
}
]
}
Something like this seems to give the result you wanted:
def arraylist = [["0",2],["1",8],["2",6],["3",8],["4",3]]
def builder = new groovy.json.JsonBuilder()
builder {
cols( [
[ id: "hours", label: "Hours", type: "string" ],
[ id: "visitor", label: "Visitors", type: "number" ] ] )
rows( arraylist.collect { pair -> [ c: pair.collect { item -> [ v: item ] } ] } )
}
println builder.toPrettyString()