I am using JQ Play.
Here is my JSON Data –
{
"body": {
"name": "DATATYPES",
"primarykey": [
"db_number"
],
"members": [
{
"name": "db_number",
"type": "NUMBER"
},
{
"name": "db_date",
"type": "DATE"
},
{
"name": "db_timestamp",
"type": "TIMESTAMP(6)"
},
{
"name": "db_timestamp_with_local_time_zone",
"type": "TIMESTAMP(6) WITH LOCAL TIME ZONE"
},
{
"name": "db_char",
"type": "CHAR"
},
{
"name": "db_float",
"type": "FLOAT"
},
{
"name": "db_nchar",
"type": "NCHAR"
},
{
"name": "db_nvarchar2",
"type": "NVARCHAR2"
},
{
"name": "db_varchar2",
"type": "VARCHAR2"
},
{
"name": "db_binary_float",
"type": "BINARY_FLOAT"
},
{
"name": "db_binary_double",
"type": "BINARY_DOUBLE"
},
{
"name": "db_timestamp_with_time_zone",
"type": "TIMESTAMP(6) WITH TIME ZONE"
},
{
"name": "db_interval_year_to_month",
"type": "INTERVAL YEAR(2) TO MONTH"
},
{
"name": "db_interval_day_to_second",
"type": "INTERVAL DAY(2) TO SECOND(6)"
},
{
"name": "db_long",
"type": "LONG"
},
{
"name": "db_blob",
"type": "BLOB"
},
{
"name": "db_clob",
"type": "CLOB"
},
{
"name": "db_boolean",
"type": "CHAR"
}
],
"links": [
{
"rel": "collection",
"href": "https://tdcvm28s16.bcone.com:7002/ords/testords/metadata-catalog/",
"mediaType": "application/json"
},
{
"rel": "canonical",
"href": "https://tdcvm28s16.bcone.com:7002/ords/testords/metadata-catalog/datatypes/",
"mediaType": "application/json"
},
{
"rel": "alternate",
"href": "https://tdcvm28s16.bcone.com:7002/ords/testords/open-api-catalog/datatypes/",
"mediaType": "application/openapi+json"
},
{
"rel": "describes",
"href": "https://tdcvm28s16.bcone.com:7002/ords/testords/datatypes/"
}
]
}
}
My First JQ –
{"parameters":([.body.primarykey[0]] | reduce .[] as $item ({}; .[$item] = ""))}
Output –
{
"parameters": {
"db_number": ""
}
}
Here, my requirement is – I want type of primary also in parameters in output by comparing it with matching record from members.
My expected output is –
{
"parameters": {
"db_number": "number"
}
}
Please help me with correct JQ that will work in jqplay.org
My Second JQ –
.body.primarykey[] as $k | { parameters: {($k): "" }, body: (.body.members | map(
select(.name != $k) | {(.name): .type |ascii_downcase}) | add)}
Here, my requirement is – I want same parameters as above with type of primarykey along with body which eliminates matching primarykey from body.
My expected output is –
{
"parameters": {
"db_number": "number"
},
"body": {
"db_date": "date",
"db_timestamp": "timestamp(6)",
"db_timestamp_with_local_time_zone": "timestamp(6) with local time zone",
"db_char": "char",
"db_float": "float",
"db_nchar": "nchar",
"db_nvarchar2": "nvarchar2",
"db_varchar2": "varchar2",
"db_binary_float": "binary_float",
"db_binary_double": "binary_double",
"db_timestamp_with_time_zone": "timestamp(6) with time zone",
"db_interval_year_to_month": "interval year(2) to month",
"db_interval_day_to_second": "interval day(2) to second(6)",
"db_long": "long",
"db_blob": "blob",
"db_clob": "clob",
"db_boolean": "char"
}
}
Please help me with correct JQ that will work in jqplay.org
2
Answers
You’re losing reference if you cut too early, and still to get the value you need you’d then have to iterate over
.members
again, find the right key, adjust the case again, etc.Here’s a restructured version of your original attempt which just groups the members by containedness in the primary keys array, then assigns each section to to the right keys in the output object.
Demo
For your first result :
then second result :