I have this json response from db.
{"StudentId":"1","SubjectId":"1","Mark":"61"}{"StudentId":"1","SubjectId":"2","Mark":"75"}{"StudentId":"1","SubjectId":"3","Mark":"87"}{"StudentId":"2","SubjectId":"1","Mark":"82"}{"StudentId":"2","SubjectId":"2","Mark":"64"}{"StudentId":"2","SubjectId":"3","Mark":"77"}
I want convert as
{"StudentId":"1",
"Mark":[ "1":"61", "2":"75", "3":"87" ]
}
{"StudentId":"2",
"Mark":[ "1":"82", "2":"64", "3":"77" ]
}
By using this I want to generate a html table.
Are you by any chance using Couchbase? This transformation would be quite easy in N1QL. Here's the query:
SELECT data.StudentId, OBJECT v.SubjectId:v.Mark FOR v IN ARRAY_AGG(data) END as Mark
FROM [{"StudentId":"1","SubjectId":"1","Mark":"61"},
{"StudentId":"1","SubjectId":"2","Mark":"75"},
{"StudentId":"1","SubjectId":"3","Mark":"87"},
{"StudentId":"2","SubjectId":"1","Mark":"82"},
{"StudentId":"2","SubjectId":"2","Mark":"64"},
{"StudentId":"2","SubjectId":"3","Mark":"77"}] data
GROUP BY data.StudentId
Presumably you would be replacing the hard-coded array in the FROM clause with a query to get the data in the first place.
Here's the output:
[
{
"Mark": {
"1": "61",
"2": "75",
"3": "87"
},
"StudentId": "1"
},
{
"Mark": {
"1": "82",
"2": "64",
"3": "77"
},
"StudentId": "2"
}
]
{"StudentId":"1","SubjectId":"1","Mark":"61"},
{"StudentId":"1","SubjectId":"2","Mark":"75"},
{"StudentId":"1","SubjectId":"3","Mark":"87"},
{"StudentId":"2","SubjectId":"1","Mark":"82"},
{"StudentId":"2","SubjectId":"2","Mark":"64"},
{"StudentId":"2","SubjectId":"3","Mark":"77"}
In DB I have results table with attributes StudentId, SubjectId, Mark
I fetched the data and converted to JSON.
I finally want data in this format.
[
{
"Mark": {
"1": "61",
"2": "75",
"3": "87"
},
"StudentId": "1"
},
{
"Mark": {
"1": "82",
"2": "64",
"3": "77"
},
"StudentId": "2"
}
]
Related
Given JSON:
[
{
"1": "false"
"2": "true",
"3": "true"
},
{
"1": "false"
"2": "false",
"3": "false"
},
{
"1": "true"
"2": "true",
"3": "true"
}
]
Given array as an argument passed to jq:
["1","2","3"]
Need to remove JSON element using jq tool if at least one key is not "true".
Desired output:
[
{
"1": "false"
"2": "true",
"3": "true"
},
{
"1": "true"
"2": "true",
"3": "true"
}
]
Given JSON:
[
{
"1": "false"
"2": "true",
"3": "true"
},
{
"1": "false"
"2": "false",
"3": "false"
},
{
"1": "true"
"2": "true",
"3": "true"
}
]
Given array as an argument passed to jq:
["1","2","3"]
Need to remove JSON element using jq tool if at least one key is not "true".
Desired output:
[
{
"1": "false"
"2": "true",
"3": "true"
},
{
"1": "true"
"2": "true",
"3": "true"
}
]
I don't know what role the argument ["1","2","3"] should play, but apart from that, you can convert the object items from string to boolean using fromjson, then aggregate them appropriately, and use this result in a map(select(…)) clause to filter the input array.
Now, if you want to remove items
if at least one key is not "true"
meaning you want to keep only those that are entirely true, use the all aggregator:
jq 'map(select(map(fromjson) | all))'
[
{
"1": "true",
"2": "true",
"3": "true"
}
]
Demo
But if, as your desired output suggests, you want to remove those items that are entirely false, meaning to keep only those where there is at least one value which is true, use the any aggregator:
jq 'map(select(map(fromjson) | any))'
[
{
"1": "false",
"2": "true",
"3": "true"
},
{
"1": "true",
"2": "true",
"3": "true"
}
]
Demo
I'm reading a JSON in pyspark and seeing the below issue.
Column name "change(me)" contains invalid characters, please use alias to rename it
I have tried to use withColumnRenamed but that does not seem to help
df = spark.read.option("multiline","true").json("json_file")
df = df.withColumnRenamed("change(me)", "change_me")
Here is my sample json
{
"1": {
"task": [
"wakeup",
"getready"
]
},
"2": {
"task": [
"brush",
"shower"
]
},
"3": {
"task": [
"brush",
"shower"
]
},
"activites": ["standup", "play", "sitdown"],
"statuscheck": {
"time": 60,
"color": 1002,
"change(me)": 9898
},
"action": ["1", "2", "3", "4"]
}
when I check for columns in my dataframe, I do not see change(me) but it still complains of invalid character
I have a JSON object which has 100000 records. I want a select a record which has specific value to the one of the key
Eg:
[{
"name": "bindu",
"age": "24",
"qualification": "b.tech"
},
{
"name": "naveen",
"age": "23",
"qualification": "b.tech"
},
{
"name": "parvathi",
"age": "23",
"qualification": "m.tech"
},
{
"name": "bindu s",
"status": "married"
},
{
"name": "naveen k",
"status": "unmarried"
}]
now I want to combine the records which are having the name with 'bindu' and 'bindu s. We can achieve this by iterating on the JSON object but since the size is more it is taking more time. Is there any way to make this easy.
I want the output like
[{
"name": "bindu",
"age": "24",
"qualification": "b.tech",
"status": "married"
},
{
"name": "naveen",
"age": "23",
"qualification": "b.tech",
"status": "unmarried"
},
{
"name": "parvathi",
"age": "23",
"qualification": "m.tech"
"status": ""
},
This will rename and merge your objects by first name.
jq 'map(.name |= split(" ")[0]) | group_by(.name) | map(add)'
I am trying to utilize the JSON result of a GET request to my Li3 app, but I would like the result to be an array of the returned JSON objects, rather than an object of the JSON objects.
I have the following code in my view file (index.html.php):
print($todos->to('json'));
Which results in each row becoming a JSON object (good), but within an over-arching JSON object.
{
"1": {
"id": "1",
"title": "One",
"done": "0"
},
"2": {
"id": "2",
"title": "Two",
"done": "0"
},
"3": {
"id": "3",
"title": "Three",
"done": "0"
},
"4": {
"id": "4",
"title": "Four",
"done": "0"
}
}
I would like to get:
[
{
"id": "1",
"title": "One",
"done": "0"
},
{
"id": "2",
"title": "Two",
"done": "0"
},
{
"id": "3",
"title": "Three",
"done": "0"
},
{
"id": "4",
"title": "Four",
"done": "0"
}
]
Note: I've found that this was the case (array of objects) in commit "974469cf25db5cbab61f3e1ff172405f4635032e" of the lithium github project, but with anything after that commit, the result is an object of objects.
Try $todos->to('json', ['indexed' => false]), or, refer to the Media class for direct serialization of JSON without the template.
Todos::all(['return' => 'array'))->to('json'); works perfect with RecordSet too
I wish to generate a DataTable with ajax response as the source. I convert an ArrayList of employees to JSON string using GSON libraries. The JSON string I get is
{
"sEcho": 3,
"iTotalRecords": 2,
"iTotalDisplayRecords": 2,
"aaData": [
{
"id": 1,
"firstName": "darsheet",
"lastName": "shah",
"city": "san jose",
"state": "ca",
"zip": 95112
},
{
"id": 2,
"firstName": "akshat",
"lastName": "shah",
"city": "ahmedabad",
"state": "gj",
"zip": 380061
}
]
}
But the sAjaxSource attribute requires JSON string in following format
{
"sEcho": 3,
"iTotalRecords": 2,
"iTotalDisplayRecords": 2,
"aaData": [
{
"0": 1,
"1": "darsheet",
"2": "shah",
"3": "san jose",
"4": "ca",
"5": 95112
},
{
"0": 2,
"1": "akshat",
"2": "shah",
"3": "ahmedabad",
"4": "gj",
"5": 380061
}
]
}
The Datatable code I use is
$(document).ready(function(){
$('#refresh').click(function(){
$('#emp').dataTable({
"sAjaxSource":".../ExploreDatatable/loadTableAjax"
});
});
});
How to remove this mismatch in JSON structure ?
Thanks.
As I know it's that you option of datatable is wrong. it should do it like this:
$('#emp').dataTable({
"sAjaxSource":".../ExploreDatatable/loadTableAjax",
"aoColumns": [{"mDataProp": "id"},
{"mDataProp": "firstName"},
.....the other column names, that should be mapped to the json key......
]
});