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
Related
I'm trying to transform a record json into individual json records but have a hard time getting the values rather than the names. I expect the keys to change on occasion and there to be more than 4 records on occasion so I wanted it to be dynamic. It seems the current transform will only give me the keys and not the values broken out into there own records.
Input
[
{
"Owner": {
"0": "CIMections",
"1": "CIMections",
"2": "CIMections",
"3": "CIMections"
},
"Name": {
"0": "AFE 20NSF044",
"1": "AFE 20NSF044",
"2": "AFE 20NSF044",
"3": "AFE 20NSF044"
},
"Producer": {
"0": "Produtream",
"1": "Produtream",
"2": "Produtream",
"3": "Produtream"
},
"Producers ID": {
"0": "NTI XR 001-004",
"1": "NTI XR 001-004",
"2": "NTI XR 001-004",
"3": "NTI XR 001-004"
},
"Weld - Real Time Count": {
"0": "",
"1": "",
"2": "",
"3": ""
},
"Character Set": {
"0": "ISO_IR 192",
"1": "ISO_IR 192",
"2": "ISO_IR 192",
"3": "ISO_IR 192"
},
"inv# Welds": {
"0": "Accepted 001",
"1": "Accepted 002",
"2": "Accepted 003",
"3": "Accepted 004"
},
"invoice": {
"0": 893300361,
"1": 411904740,
"2": 673190473,
"3": 1426231494
},
"status": {
"0": "Done",
"1": "Done",
"2": "Done",
"3": "Done"
},
"Date Completed": {
"0": "20210301 163500.000000",
"1": "20210301 163500.000000",
"2": "20210301 163500.000000",
"3": "20210301 163500.000000"
},
"Institution Name": {
"0": "NXXT Digital",
"1": "NXXT Digital",
"2": "NXXT Digital",
"3": "NXXT Digital"
},
"file_id": {
"0": "00001",
"1": "00002",
"2": "00003",
"3": "00004"
}
}
]
Current Transformation
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"Owner": "Owner.[].#(1,0)",
"$": "&",
"#": "#clientId"
}
}
}
}
]
Im Getting
{
"Owner" : "Owner",
"Name" : "Name",
"Producer" : "Producer",
"Producers ID" : "Producers ID",
"Weld - Real Time Count" : "Weld - Real Time Count",
"Character Set" : "Character Set",
"inv# Welds" : "inv# Welds",
"invoice" : "invoice",
"status" : "status",
"Date Completed" : "Date Completed",
"Institution Name" : "Institution Name",
"file_id" : "file_id"
}
But wanting
{
"Owner" : "CIMections",
"Name" : "AFE 20NSF044",
"Producer" : "Produtream",
"Producers ID" : "NTI XR 001-004",
"Weld - Real Time Count" : "",
"Character Set" : "ISO_IR 192",
"inv# Welds" : "Accepted 001",
"invoice" : "893300361",
"status" : "Done",
"Date Completed" : "20210301 163500.000000",
"Institution Name" : "NXXT Digital",
"file_id" : "00001"
},
{
"Owner" : "CIMections",
"Name" : "AFE 20NSF044",
"Producer" : "Produtream",
"Producers ID" : "NTI XR 001-004",
"Weld - Real Time Count" : "",
"Character Set" : "ISO_I 192",
"inv# Welds" : "Accepted 002",
"invoice" : "411904740",
"status" : "Done",
"Date Completed" : "20210301 163500.000000",
"Institution Name" : "NXXT Digital",
"file_id" : "00002"
}...
$ wildcard is not needed, but only using # wildcard is enough, along with filtering by indices with 0 and 1 (0|1) such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"0|1": {
"#": "[&].&2"
}
}
}
}
}
]
where prepending the value with [&]. converts each individual array to seperate objects within a single array.
If you need to get all the objects within the array, then replace "0|1" with "*" wildcard.
I want to create a nested JSON for using it as a Post method parameter. I tried NSDictionary, Array, [String:Any], etc. But failed to get the accurate JSON structure.
I want a JSON format, like this
{
"Command": [
{
"commandData": {
"name": "name",
"description": "description",
"title": "title",
"actions": [
{
"ERR": [
{
"ERR_LINE": {
"Value": {
"1": 1.0,
"2": 1.0,
"3": 1.0,
"4": 1.0,
"5": 1.0,
"6": 1.0
}
}
}
]
}
]
},
"errID": "id",
"issueID": "id"
}
]
}
You can do like this:
var postData = Dictionary<String,Any>()
postData = [
"Command": [
"commandData": [
"name": "name",
"description": "description",
"title": "title",
"actions": [
"ERR": [
"ERR_LINE": [
"Value": [
"1": 1.0,
"2": 1.0,
"3": 1.0,
"4": 1.0,
"5": 1.0,
"6": 1.0
]
]
]
]
],
"errID": "id",
"issueID": "id"
]
]
Hope it works.
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"
}
]
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 have a JSON object below and there are multiple nested divisions, the first without any identifiers. I am trying to get to the teams within the last divisions using a for loop, but I am not able to access them. Need some guidance.
{
"division": {
"division": [
{
"team": {
"id": "229525",
"name": "MyTeam",
"photo": "",
"visible": "True",
"RosterView": "True",
"PublicResults": "True",
"Statistics": "False",
"privilege": [
"False",
"True",
"True",
"True",
"True",
"True",
"True"
]
},
"name": "Boys 9-10",
"id": "12897",
"sort": "0",
"open": "0"
},
{
"team": [
{
"id": "229523",
"name": "Cougars",
"photo": "",
"visible": "True",
"RosterView": "True",
"PublicResults": "True",
"Statistics": "False",
"privilege": [
"False",
"True",
"True",
"True",
"True",
"True",
"True"
]
},
Update: Hmmm, after reading your JSON carefully, I notice that the 1st division has only 1 team since the team has only one object. But seems like the 2nd division has more than 1 team since there is an array of objects in team. I guess you have to handle the case where there only one team in a division.
Your for loop was almost right. Try this:
var divisions = JSONObject.division.division;
for (var i=0; i < divisions.length; i++) {
// handle division of one team here,
// don't need for loop if there only one team
// just do division[i].team.id
for (var j=0; j < divisions[i].length; j++) {
var teamId = divisions[i].team[j].id;
alert(teamId);
}
}
Your loop was
for (i=0; i <= JSONObject.division.division.teams[i].length; i++) { }
It has 2 problems, first you are skipping the last element by doing i <= ...length; i++
second, JSONObject.division.division.teams[i] is not an array, but this one JSONObject.division.division[i].team will give you an array of all the teams in a division. Then you can just get the team you want by its index in the array :)
First you should fix your parenthesis in json.
var a = {
"division": {
"division": [
{
"team": {
"id": "229525",
"name": "MyTeam",
"photo": "",
"visible": "True",
"RosterView": "True",
"PublicResults": "True",
"Statistics": "False",
"privilege": [
"False",
"True",
"True",
"True",
"True",
"True",
"True"
]
},
"name": "Boys 9-10",
"id": "12897",
"sort": "0",
"open": "0"
},
{
"team": [
{
"id": "229523",
"name": "Cougars",
"photo": "",
"visible": "True",
"RosterView": "True",
"PublicResults": "True",
"Statistics": "False",
"privilege": [
"False",
"True",
"True",
"True",
"True",
"True",
"True"
]
}
]
}
]
}
}
Then you can reach second 'team' object with:
a['division']['division'][1]['team'][0]['id'];
This will give you id. You can select anything you want. It is not so complicated. Just read it like a puzzle.
Here is live example: JSFiddle
here are some for loops :
var json1 = a['division']['division'][1]['team'][0]; // this is for second team array,
json2 = a['division']['division'][0]['team']; // this is for first team object,
for (obj in json1){
return json[obj];
};
According to your json, first and second teams are not in same data type. first team is array but second one is object. Thats why there are two different variables.