Reduce json bean descriptor to text - json

I wish to reduce with jq
[ {
"context" : "app:swagger,dev:8080",
"parent" : null,
"beans" : [ {
"bean" : "app",
"aliases" : [ ],
"scope" : "singleton",
"type" : "com.example.App",
"resource" : "null",
"dependencies" : [ "environment" ]
}, {
"bean" : "environment",
"aliases" : [ ],
"scope" : "singleton",
"type" : "com.example.Environment",
"resource" : "null",
"dependencies" : [ ]
}
},
...
}]
to
app --> environment
...

The problem statement appears to be under-specified, but the following seems to be either a solution or very close to one:
jq -r '.[] | .beans[] | "\(.bean) --> \(.dependencies[])"' input.json

Related

jq - how can I flat my list of lists into one level list

how can I have transformed my json
{
"clients": [
{
"id" : "qwerty",
"accounts" : [{"number" : "6666"}, {"number" : "7777"}]
},
{
"id" : "zxcvb",
"accounts" : [{"number" : "1111"}, {"number" : "2222"}]
}
]
}
into following type of json? using JQ
{
"items": [
{
"id" : "qwerty",
"number" : "6666"
},{
"id" : "qwerty",
"number" : "7777"
},{
"id" : "zxcvb",
"number" : "1111"
},{
"id" : "zxcvb",
"number" : "2222"
}]
}
What kind of tools from JQ can help me? I can't choose any possible way to do it
Something like this should do the trick:
{items: [.clients[] | {id} + .accounts[]]}
Online demo

Parse and Map 2 Arrays with jq

I am working with a JSON file similar to the one below:
{ "Response" : {
"TimeUnit" : [ 1576126800000 ],
"metaData" : {
"errors" : [ ],
"notices" : [ "query served by:1"]
},
"stats" : {
"data" : [ {
"identifier" : {
"names" : [ "apiproxy", "response_status_code", "target_response_code", "target_ip" ],
"values" : [ "IO", "502", "502", "7.1.143.6" ]
},
"metric" : [ {
"env" : "dev",
"name" : "sum(message_count)",
"values" : [ 0.0]
} ]
} ]
} } }
My object is to display a mapping of the identifier and values like :
apiproxy=IO
response_status_code=502
target_response_code=502
target_ip=7.1.143.6
I have been able to parse both names and values with
.[].stats.data[] | (.identifier.names[]) and .[].stats.data[] | (.identifier.values[])
but I need help with the jq way to map the values.
The whole thing can be done in jq using the -r command-line option:
.[].stats.data[]
| [.identifier.names, .identifier.values]
| transpose[]
| "\(.[0])=\(.[1])"

How to get entire parent node using jq json parser?

I am trying to find a value in the json file and based on that I need to get the entire json data instead of that particular block.
Here is my sample json
[{
"name" : "Redirect to Website 1",
"behaviors" : [ {
"name" : "redirect",
"options" : {
"mobileDefaultChoice" : "DEFAULT",
"destinationProtocol" : "HTTPS",
"destinationHostname" : "SAME_AS_REQUEST",
"responseCode" : 302
}
} ],
"criteria" : [ {
"name" : "requestProtocol",
"options" : {
"value" : "HTTP"
}
} ],
"criteriaMustSatisfy" : "all"
},
{
"name" : "Redirect to Website 2",
"behaviors" : [ {
"name" : "redirect",
"options" : {
"mobileDefaultChoice" : "DEFAULT",
"destinationProtocol" : "HTTPS",
"destinationHostname" : "SAME_AS_REQUEST",
"responseCode" : 301
}
} ],
"criteria" : [ {
"name" : "contentType",
"options" : {
"matchOperator" : "IS_ONE_OF",
"values" : [ "text/html*", "text/css*", "application/x-javascript*" ],
}
} ],
"criteriaMustSatisfy" : "all"
}]
I am trying to match for "name" : "redirect" inside each behaviors array and if it matches then I need the entire block including the "criteria" section, as you can see its under same block {}
I managed to find the values using select methods but not able to get the parent section.
https://jqplay.org/s/BWJwVdO3Zv
Any help is much appreciated!
To avoid unwanted duplication:
.[]
| first(select(.behaviors[].name == "redirect"))
Equivalently:
.[]
| select(any(.behaviors[]; .name == "redirect"))
You can try this jq command:
<file jq 'select(.[].behaviors[].name=="redirect")'

Query multiple elements in nested JSON Document

I have the following sample data in MongoDB:
{
"_id" : ObjectId("54833e93ade1a1521a2a2fe8"),
"fname" : "yumi",
"mname" : "sakura",
"lname" : "kirisaki",
"consultations" : [
{
"medications" : [
"paracetamol",
"ibuprofen",
"carbocisteine"
],
"diagnosis" : [
"sore throat",
"fever",
"cough"
],
"date" : ISODate("2014-12-01T16:00:00Z")
},
{
"medications" : [
"paracetamol",
"carbocisteine",
"afrin"
],
"diagnosis" : [
"cough",
"colds",
"fever"
],
"date" : ISODate("2014-12-11T16:00:00Z")
}
]
}
{
"_id" : ObjectId("54833e93ade1a1521a2a2fe9"),
"fname" : "james",
"mname" : "legaspi",
"lname" : "reyes",
"consultations" : [
{
"medications" : [
"zanamivir",
"ibuprofen",
"paracetamol"
],
"diagnosis" : [
"influenza",
"body aches",
"headache"
],
"date" : ISODate("2014-10-22T16:00:00Z")
},
{
"medications" : [
"carbocisteine",
"albuterol",
"ibuprofen"
],
"diagnosis" : [
"asthma",
"cough",
"headache"
],
"date" : ISODate("2014-11-13T16:00:00Z")
}
]
}
I am trying to query patients with zanamivir AND ibuprofen AND cough:
db.patient.find({
$and:
[
{"consultations.medications":["zanamivir", "ibuprofen"]},
{"consultations.diagnosis":"cough"}
]
}).pretty()
So, in the short sample data, I was hoping james would be returned since he is the only one with zanamivir medication.
Nothing is happening when I enter the above query in cmd. It just goes to the next line (no syntax errors, etc.)
How must I go about the query?
You need the use the $all operator.
db.patient.find({
"consultations.medications": { "$all" : [ "zanamivir", "ibuprofen" ]},
"consultations.diagnosis": "cough"
})
Pretty simple, it's just your first part of the query.
db.patient.find({
$and:[
{"consultations.medications":["zanamivir", "ibuprofen"]},
{"consultations.diagnosis":"cough"}]})
Asking Mongodb to find consultations.medications against ["zanamivir", "ibuprofen"] is asking it to find someone whose medications are equal to ['zanamivir', 'ibuprofen'].
If you want to find people who have had zanamivir and ibuprofen medicated you need to tweak the query to this:
db.patient.find({
$and:[
{"consultations.medications":"zanamivir"},
{"consultations.medications":"ibuprofen"},
{"consultations.diagnosis":"cough"}]})
Enjoy!

Corinis/JSFORM : nested collections are ignored

I'm using Corinis/jsForm.js (jquery.jsForm-1.0.rc.2.js) to map JSON to/from html fields
I have trouble when my collection is deeply nested
(i.e. the field seems to get ignored)
any suggestions ?
My json object looks like this
{ "#class" : "CmsNode",
"#fieldTypes" : "createdOn=t,updatedOn=t",
"#rid" : "#13:16",
"#type" : "d",
"#version" : 18,
"children" : [ ],
"cmsRecord" : { "#class" : "CmsRecord",
"#type" : "d",
"#version" : 0,
"active" : [ { "#class" : "Record",
"#type" : "d",
"#version" : 0,
"properties" : { "alias" : "blah" }
} ],
"archive" : [ ],
"classifications" : [ ],
"keywords" : [ ],
"pending" : [ ]
},
"createdOn" : "2013-07-05 12:38:59:057",
"data" : { "name" : "test3en" },
"history" : [ ],
"isMenu" : true,
"isVisible" : false,
"mvcModel" : "dd",
"mvcView" : "aaa",
"pageViews" : [ ],
"parents" : [ "#13:9" ],
"related" : [ ],
"updatedOn" : "2013-07-08 09:06:47:610",
"uuid" : "933a10da-b9a8-44d1-9a65-adc189c740b2",
"viewClasses" : [ ]
}
I need to have an input field attached to property data.cmsRecord.active.properties.alias
I'm trying to achieve this, by using the following html code:
<div class="collection" data-field="data.cmsRecord.active">
<div>
<input type="text" name="active.properties.alias" />
</div>
</div>
Issue has been resolved by the script author.
fixed Release: jsForm-1.0.3.js