JQ: Nested JSON transformation

强颜欢笑 提交于 2019-12-11 06:06:44

问题


I actually looking for a software to transform json files (i am a complete json noobie) that i got from a web api, for a better load to my SQL database. Some hours Google Research later i found jq, that hit my needs nearly to 100%. I though i found the necessary filter Operation on jq for my needs but runs now in a "little" Problem. My source json is heavly nested and i Need to extract several data (from objects/Arrays) from different layer in the jsons paths. I built following filter on jqplay:

. | {cruise_nid: .nid} + {nights: .nights} + {zone: .zones[].title} +  {sails_nid: .sails[].nid} + {arrival: .sails[].arrival} + {departure: .sails[].departure} + {cabintype: .sails[].cabins[].cabinType.kindName} + {catalogprice: .sails[].cabins[].catalogPrice} + {discountprice: .sails[].cabins[].discountPrice} + {currency: .sails[].cabins[].currency}

source json (shorted):

{
"nid": 434508,
"nights": 121,
"zones": [
    {
        "nid": 35761,
        "title": "Weltreise",
    }
],
"sails": [
    {
        "nid": 434516,
        "arrival": 1525644000,
        "bookingServiceCode": "kreuzfahrt/c8a3/mit-der-columbus-einmal-um-die-ganze-welt-once-in-a-lifetime",
        "departure": 1515193200,
        "optionalFlightPrice": null,
        "cabins": [
            {
                "cabinType": {
                    "nid": 379723,
                    "title": "Glückskabine Innen (Kat. IG): ",
                    "description": "Bei Buchung einer Garantiekabine überlassen Sie der Reederei die Wahl Ihrer Kabinennummer und Deck. Sie entscheiden sich für die Route, einen der zur Auswahl stehenden Termine, sowie die Kabinenkategorie. Für Ihre Flexibilität werden Sie mit attraktiven Vorzugspreisen belohnt.",
                    "bookingServiceCode": "IG",
                    "externalCode": "IG",
                    "kindId": 19,
                    "kindName": "Innenkabine",
                    "kind": "inside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "",
                    "size": "",
                    "bed": "",
                    "windows": "",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": true,
                    "guaranteeCabinInfo": "Bei Buchung einer Garantiekabine überlassen Sie der Reederei die Wahl Ihrer Kabinennummer und Deck. Sie entscheiden sich für die Route, einen der zur Auswahl stehenden Termine, sowie die Kabinenkategorie. Für Ihre Flexibilität werden Sie mit attraktiven Vorzugspreisen belohnt.",
                    "amenities": [],
                    "advantages": []
                },
                "state": 2,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 9519,
                "catalogPrice": 17879,
                "discountPercentage": 0.4675876726886291,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379730,
                    "title": "Innenkabine Standard (Kat. 1): ",
                    "description": "<ul>\r\n<li>Lage: Deck 5</li>\r\n<li>2 Einzelbetten</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>\r\n",
                    "bookingServiceCode": "1",
                    "externalCode": "1",
                    "kindId": 19,
                    "kindName": "Innenkabine",
                    "kind": "inside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 5",
                    "size": "18 m²",
                    "bed": "2 Einzelbetten",
                    "windows": "",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 2,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 10239,
                "catalogPrice": 18599,
                "discountPercentage": 0.44948653153395346,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379731,
                    "title": "Innenkabine Standard Plus (Kat. 2): ",
                    "description": "<ul>\r\n<li>Lage: Deck 6</li>\r\n<li>2 Einzelbetten</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>\r\n",
                    "bookingServiceCode": "2",
                    "externalCode": "2",
                    "kindId": 19,
                    "kindName": "Innenkabine",
                    "kind": "inside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 6",
                    "size": "18 m²",
                    "bed": "2 Einzelbetten",
                    "windows": "",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 2,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 11299,
                "catalogPrice": 20519,
                "discountPercentage": 0.4493396364345241,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379732,
                    "title": "Innenkabine Superior (Kat. 3): ",
                    "description": "<ul>\r\n<li>Lage: Deck 10</li>\r\n<li>2 Einzelbetten</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "3",
                    "externalCode": "3",
                    "kindId": 19,
                    "kindName": "Innenkabine",
                    "kind": "inside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 10",
                    "size": "18 m²",
                    "bed": "2 Einzelbetten",
                    "windows": "",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 1,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 11999,
                "catalogPrice": 21809,
                "discountPercentage": 0.44981429684992436,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379734,
                    "title": "Innenkabine Premium (Kat. 4): ",
                    "description": "<ul>\r\n<li>Lage: Deck 11</li>\r\n<li>2 Einzelbetten</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>\r\n",
                    "bookingServiceCode": "4",
                    "externalCode": "4",
                    "kindId": 19,
                    "kindName": "Innenkabine",
                    "kind": "inside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 11",
                    "size": "18 m²",
                    "bed": "2 Einzelbetten",
                    "windows": "",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 1,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 12709,
                "catalogPrice": 23089,
                "discountPercentage": 0.44956472779245527,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379740,
                    "title": "Glückskabine Außen (Kat. OG): ",
                    "description": "Bei Buchung einer Garantiekabine überlassen Sie der Reederei die Wahl Ihrer Kabinennummer und Deck. Sie entscheiden sich für die Route, einen der zur Auswahl stehenden Termine, sowie die Kabinenkategorie. Für Ihre Flexibilität werden Sie mit attraktiven Vorzugspreisen belohnt.",
                    "bookingServiceCode": "OG",
                    "externalCode": "OG",
                    "kindId": 20,
                    "kindName": "Außenkabine",
                    "kind": "outside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "",
                    "size": "",
                    "bed": "",
                    "windows": "Fenster",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": true,
                    "guaranteeCabinInfo": "Bei Buchung einer Garantiekabine überlassen Sie der Reederei die Wahl Ihrer Kabinennummer und Deck. Sie entscheiden sich für die Route, einen der zur Auswahl stehenden Termine, sowie die Kabinenkategorie. Für Ihre Flexibilität werden Sie mit attraktiven Vorzugspreisen belohnt.",
                    "amenities": [],
                    "advantages": []
                },
                "state": 2,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 12119,
                "catalogPrice": 22739,
                "discountPercentage": 0.4670390078719381,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379748,
                    "title": "Außenkabine Standard mit eingeschränkter Sicht (Kat. 6C): ",
                    "description": "<ul>\r\n<li>Lage: Deck 8</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Fenster</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "6C",
                    "externalCode": "6C",
                    "kindId": 20,
                    "kindName": "Außenkabine",
                    "kind": "outside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 8",
                    "size": "18 m²",
                    "bed": "1 Doppelbett",
                    "windows": "Fenster mit eingeschränkter Sicht",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 1,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 12989,
                "catalogPrice": 23609,
                "discountPercentage": 0.4498284552501165,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379749,
                    "title": "Außenkabine Standard mit eingeschränkter Sicht (Kat. 6B): ",
                    "description": "<ul>\r\n<li>Lage: Deck 9</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Fenster</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "6B",
                    "externalCode": "6B",
                    "kindId": 20,
                    "kindName": "Außenkabine",
                    "kind": "outside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 9",
                    "size": "18 m²",
                    "bed": "1 Doppelbett",
                    "windows": "Fenster mit eingeschränkter Sicht",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 1,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 13409,
                "catalogPrice": 24379,
                "discountPercentage": 0.4499774395996554,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379750,
                    "title": "Außenkabine Standard (Kat. 6): ",
                    "description": "<ul>\r\n<li>Lage: Deck 4</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Bullauge</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "6",
                    "externalCode": "6",
                    "kindId": 20,
                    "kindName": "Außenkabine",
                    "kind": "outside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 4",
                    "size": "18 m²",
                    "bed": "1 Doppelbett",
                    "windows": "Bullauge",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 1,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 14119,
                "catalogPrice": 25659,
                "discountPercentage": 0.4497447289450095,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379751,
                    "title": "Außenkabine Standard (Kat. 7): ",
                    "description": "<ul>\r\n<li>Lage: Deck 6</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Fenster</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "7",
                    "externalCode": "7",
                    "kindId": 20,
                    "kindName": "Außenkabine",
                    "kind": "outside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 6",
                    "size": "18 m²",
                    "bed": "1 Doppelbett",
                    "windows": "Fenster",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 1,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 14459,
                "catalogPrice": 26299,
                "discountPercentage": 0.4502072322141526,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379752,
                    "title": "Außenkabine Standard Plus (Kat. 8): ",
                    "description": "<ul>\r\n<li>Lage: Deck 8 & 9</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Fenster</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "8",
                    "externalCode": "8",
                    "kindId": 20,
                    "kindName": "Außenkabine",
                    "kind": "outside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 8 & 9",
                    "size": "18 m²",
                    "bed": "1 Doppelbett",
                    "windows": "Fenster",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 1,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 15169,
                "catalogPrice": 27579,
                "discountPercentage": 0.44998005728996704,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379753,
                    "title": "Außenkabine Superior (Kat. 9): ",
                    "description": "<ul>\r\n<li>Lage: Deck 10</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Fenster</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "9",
                    "externalCode": "9",
                    "kindId": 20,
                    "kindName": "Außenkabine",
                    "kind": "outside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 10",
                    "size": "18 m²",
                    "bed": "1 Doppelbett",
                    "windows": "Fenster",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 1,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 16939,
                "catalogPrice": 30779,
                "discountPercentage": 0.4496572338282595,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379754,
                    "title": "Außenkabine Superior Plus (Kat. 11): ",
                    "description": "<ul>\r\n<li>Lage: Deck 10</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Fenster</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "11",
                    "externalCode": "11",
                    "kindId": 20,
                    "kindName": "Außenkabine",
                    "kind": "outside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 10",
                    "size": "18 m²",
                    "bed": "1 Doppelbett",
                    "windows": "Fenster",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 1,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 18349,
                "catalogPrice": 33349,
                "discountPercentage": 0.4497885993583016,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379756,
                    "title": "Außenkabine Premium (Kat. 12): ",
                    "description": "<ul>\r\n<li>Lage: Deck 11</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Fenster</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "12",
                    "externalCode": "12",
                    "kindId": 20,
                    "kindName": "Außenkabine",
                    "kind": "outside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 11",
                    "size": "18 m²",
                    "bed": "1 Doppelbett",
                    "windows": "Fenster",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 1,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 19759,
                "catalogPrice": 35909,
                "discountPercentage": 0.44974797404550393,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379758,
                    "title": "Innenkabine Superior zur Einzelbelegung (Kat. 5):",
                    "description": "<ul>\r\n<li>Lage: Deck 10</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "5",
                    "externalCode": "5",
                    "kindId": 19,
                    "kindName": "Innenkabine",
                    "kind": "inside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 1,
                    "maxPassengers": null,
                    "location": "Deck 10",
                    "size": "18 m²",
                    "bed": "1 Doppelbett",
                    "windows": "",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 2,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 14989,
                "catalogPrice": 27259,
                "discountPercentage": 0.45012656370373083,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379759,
                    "title": "Außenkabine zur Einzelbelegung (Kat. 7S): ",
                    "description": "<ul>\r\n<li>Lage: Deck 6</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Fenster</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "7S",
                    "externalCode": "7S",
                    "kindId": 20,
                    "kindName": "Außenkabine",
                    "kind": "outside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 1,
                    "maxPassengers": null,
                    "location": "Deck 6",
                    "size": "18 m²",
                    "bed": "1 Doppelbett",
                    "windows": "Fenster",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 2,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 18089,
                "catalogPrice": 32869,
                "discountPercentage": 0.4496638169703976,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379760,
                    "title": "Außenkabine zur Einzelbelegung (Kat. 8S): ",
                    "description": "<ul>\r\n<li>Lage: Deck 8 & 9</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Fenster</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "8S",
                    "externalCode": "8S",
                    "kindId": 20,
                    "kindName": "Außenkabine",
                    "kind": "outside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 1,
                    "maxPassengers": null,
                    "location": "Deck 8 & 9",
                    "size": "18 m²",
                    "bed": "1 Doppelbett",
                    "windows": "Fenster",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 1,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 18959,
                "catalogPrice": 34469,
                "discountPercentage": 0.44996953784560034,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379761,
                    "title": "Außenkabine Superior zur Einzelbelegung (Kat. 10):",
                    "description": "<ul>\r\n<li>Lage: Deck 10</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Fenster</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "10",
                    "externalCode": "10",
                    "kindId": 20,
                    "kindName": "Außenkabine",
                    "kind": "outside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 1,
                    "maxPassengers": null,
                    "location": "Deck 10",
                    "size": "18 m²",
                    "bed": "1 Doppelbett",
                    "windows": "Fenster",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 2,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 21169,
                "catalogPrice": 38479,
                "discountPercentage": 0.44985576548247097,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379762,
                    "title": "Außenkabine Standard (Kat. 6P): ",
                    "description": "<ul>\r\n<li>Lage: Deck 4</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Bullauge</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "6P",
                    "externalCode": "6P",
                    "kindId": 20,
                    "kindName": "Außenkabine",
                    "kind": "outside",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 4",
                    "size": "18 m²",
                    "bed": "1 Doppelbett",
                    "windows": "Bullauge",
                    "balcony": "",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 1,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 13409,
                "catalogPrice": 24379,
                "discountPercentage": 0.4499774395996554,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379763,
                    "title": "Balkonkabine De Luxe (Kat. DL): ",
                    "description": "<ul>\r\n<li>Lage: Deck 11</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Balkon</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "DL",
                    "externalCode": "DL",
                    "kindId": 21,
                    "kindName": "Balkonkabine",
                    "kind": "balcony",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 11",
                    "size": "24 m²",
                    "bed": "1 Doppelbett",
                    "windows": "Raumhohe Fenster",
                    "balcony": "Balkon",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "advantages": []
                },
                "state": 2,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 28229,
                "catalogPrice": 51299,
                "discountPercentage": 0.44971636874013143,
                "currency": "EUR"
            },
            {
                "cabinType": {
                    "nid": 379764,
                    "title": "Junior Suite (Kat. JS) ",
                    "description": "<ul>\r\n<li>Lage: Deck 11</li>\r\n<li>1 Doppelbett</li>\r\n<li>Bad mit Dusche/WC</li>\r\n<li>Balkon</li>\r\n<li>Wohnbereich</li>\r\n<li>Flatscreen-Fernseher</li>\r\n<li>Radio</li>\r\n<li>Minibar</li>\r\n<li>Direktwahl-Telefon</li>\r\n<li>Safe</li>\r\n<li>Individuell regulierbare Klimaanlage</li>\r\n<li>Bademäntel und Handtücher</li>\r\n<li>Föhn</li>\r\n</ul>",
                    "bookingServiceCode": "JS",
                    "externalCode": "JS",
                    "kindId": 22,
                    "kindName": "Suite",
                    "kind": "suite",
                    "validFrom": null,
                    "validTo": null,
                    "disabledForDirectBooking": false,
                    "bedQuantity": 2,
                    "maxPassengers": null,
                    "location": "Deck 11",
                    "size": "35 m²",
                    "bed": "1 Doppelbett",
                    "windows": "Raumhohe Fenster",
                    "balcony": "Balkon",
                    "information": null,
                    "isGuaranteeCabin": false,
                    "guaranteeCabinInfo": "",
                    "amenities": [
                        "Bad mit Dusche/WC",
                        "Balkon",
                        "Wohnbereich",
                        "Flatscreen-Fernseher",
                        "Radio",
                        "Minibar",
                        "Direktwahl-Telefon",
                        "Safe",
                        "Individuell regulierbare Klimaanlage",
                        "Bademäntel und Handtücher",
                        "Föhn"
                    ],
                    "advantages": []
                },
                "state": 2,
                "fees": "0",
                "gratuities": "0",
                "discountPrice": 31739,
                "catalogPrice": 57709,
                "discountPercentage": 0.45001646190368916,
                "currency": "EUR"
            }
        ],
    }
],

}

The result is like:

{
  "cruise_nid": 434508,
  "nights": 121,
  "zone": "Weltreise",
  "sails_nid": 434516,
  "arrival": 1525644000,
  "departure": 1515193200,
  "cabintype": "Innenkabine",
  "catalogprice": 17879,
  "discountprice": 9519,
  "currency": "EUR"
}

From the json source (100 kB) i expected arround 30 objects like that. But i receive 194481 objects (arround 50 mb file). I actually have no idea why that happens and how i can fix it. Any ideas?

thanks

regards timo


回答1:


  1. The input sample given is not quite valid JSON. The following assumes it has been fixed up.
  2. The following assumes that the typographical case of the output key names may reflect the input keys names more closely.
  3. The following produces the desired result, but is probably not exactly what you want, since it is unclear what you expect if the .zones, .sails and/or .cabins arrays have more than one item.

{nid, nights, zone: .zones[0].title} +
(.sails[0]
 | { sails_nid: .nid, arrival, departure } +
   (.cabins[0] 
    | { cabintype: .cabinType.kindName, catalogPrice, discountPrice, currency } ))

All combinations

If you want "all combinations" of zones, sails, and cabins, simply replace [0] with [] in the above:

{nid, nights, zone: .zones[].title} +
(.sails[]
 | ({ sails_nid: .nid, arrival, departure } ) +
    (.cabins[] 
     | { cabintype: .cabinType.kindName, catalogPrice, discountPrice, currency } ) )

With the sample input, this emits 21 JSON objects.



来源:https://stackoverflow.com/questions/43755787/jq-nested-json-transformation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!