json nested array value parsing to object - json

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]]
}
}
}

Related

Creating dynamic JSON objects using groovy

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 }])
]}

read hyperopt parameters from json

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.

jq transform - json child array to delimited string

i want to transform the following input with jq
{
"count": 1000,
"rows": [{
"id": 1,
"child_id": 11,
"childarray": ["value1", "value2"]
},
{
"id": 2,
"child_id": 12,
"childarray": ["value3", "value4"]
}
]
}
Output should be:
[
{
"id": 1,
"child_id": 11,
"childarray": "value1, value2"
},
{
"id": 2,
"child_id": 12,
"childarray": "value3, value4"
}
]
I use this jq filter-> "[.rows[]]" but don't know howto "join" the array -> "childarray": ["value1", "value2"] to a comma delimited string -> "value1, value2"
https://jqplay.org/s/5TlSDgJnkc
map + join approach:
jq '.rows | map(.childarray |= join(", "))' input.json
The output:
[
{
"id": 1,
"child_id": 11,
"childarray": "value1, value2"
},
{
"id": 2,
"child_id": 12,
"childarray": "value3, value4"
}
]
Another alternative is:
jq '.rows[].childarray |= join(", ") | .rows'

How we can add Name of a restful response result List

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.

Getting error while parsing json response from a dynamic {System.RuntimeType} variable

I'm working on some code in which uses dynamic variables jsonResponse .
dynamic jsonResponse = JsonConvert.DeserializeObject(response);
This variable contains collection of hotel list in json format. From this collection I am getting roomlist collection in a new variable roomResponseList :
var roomResponseList = jsonResponse["hotels"]["hotels"][rooms].roomResponseList;
I am getting first room detail into **JObject responseRateKeys **:
foreach (var roomByResponse in roomResponseList)
{
JObject responseRateKeys = JObject.Parse(roomByResponse.ToString());
var boardNameListByResponse = responseRateKeys.AsJEnumerable().AsEnumerable()
.Select(t => t["rates"]["boardName"].ToString().Trim())
.Distinct()
.ToList();
}
But when I am trying to get any item list from JObject by using linq lambda, I am getting error,
"Cannot access child value on Newtonsoft.Json.Linq.JProperty."
Value of roomByResponse=
{ "code": "DBL.KG-NM", "name": "DOUBLE KING BED NON SMOKING", "rates": [ { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|RO|IWH25|1~1~0||N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NRF", "rateType": "RECHECK", "net": "186.04", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "RO", "boardName": "ROOM ONLY", "cancellationPolicies": [ { "amount": "149.63", "from": "2017-07-14T03:29:00+05:30" } ], "rooms": 1, "adults": 1, "children": 0, "dailyRates": [ { "offset": 1, "dailyNet": "93.02" }, { "offset": 2, "dailyNet": "93.02" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|BB|IWB25|1~1~0||N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NOR", "rateType": "RECHECK", "net": "238.92", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "BB", "boardName": "BED AND BREAKFAST", "rooms": 1, "adults": 1, "children": 0, "dailyRates": [ { "offset": 1, "dailyNet": "119.46" }, { "offset": 2, "dailyNet": "119.46" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|RO|IWH25|2~2~1|2|N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NRF", "rateType": "RECHECK", "net": "372.06", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "RO", "boardName": "ROOM ONLY", "cancellationPolicies": [ { "amount": "299.25", "from": "2017-07-14T03:29:00+05:30" } ], "rooms": 2, "adults": 2, "children": 1, "childrenAges": "2", "dailyRates": [ { "offset": 1, "dailyNet": "186.03" }, { "offset": 2, "dailyNet": "186.03" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|BB|IWB25|2~2~1|2|N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NOR", "rateType": "RECHECK", "net": "477.84", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "BB", "boardName": "BED AND BREAKFAST", "rooms": 2, "adults": 2, "children": 1, "childrenAges": "2", "dailyRates": [ { "offset": 1, "dailyNet": "238.92" }, { "offset": 2, "dailyNet": "238.92" } ] } ] }
Thank you
Pravesh Singh
change linq to
responseRateKeys["rates"].AsJEnumerable().Select(t=>t["boardName"]).Distinct().ToList()