首先,我是新的jq
,像1天新,我也是新的JSON,我是一个SQL的家伙,所以我学得很快,但不能让我的头周围…所以请耐心点
我运行Windows,在PowerShell上使用jq v1.5。
我下载了多个JSON文件,它们看起来像这样:
{"Header":{"AssetClass":"Commodities","InstrumentType":"Forward","UseCase":"Forward","Level":"InstRefDataReporting"},"Attributes":{"NotionalCurrency":"EUR","ExpiryDate":"2018-01-01","ReturnorPayoutTrigger":"Contract for Difference (CFD)","DeliveryType":"CASH","BaseProduct":"AGRI","TransactionType":"FUTR","FinalPriceType":"ARGM","ReferenceRate":"10PPM ULTRA LOW SULPHUR DIESEL-CARGOES CIF NWE/BASIS ARA-PLATTS EUROPEAN","SubProduct":"GROS","AdditionalSubProduct":"FWHT"},"ISIN":{"ISIN":"EZX27M86B860","Status":"New"},"TemplateVersion":1,"Derived":{"CommodityDerivativeIndicator":"TRUE","UnderlyingAssetType":"Agriculture","IssuerorOperatoroftheTradingVenueIdentifier":"NA","PriceMultiplier":1,"FullName":"Commodities Forward AGRI GROS FWHT EUR 20180101","ShortName":"NA/Fwd AGRI FWHT EUR 20180101","ClassificationType":"JTAXCC"}}
{"Header":{"AssetClass":"Commodities","InstrumentType":"Swap","UseCase":"Basis_Swap","Level":"InstRefDataReporting"},"Attributes":{"NotionalCurrency":"SOS","ExpiryDate":"2208-12-10","ReturnorPayoutTrigger":"Total Return","DeliveryType":"OPTL","TransactionType":"ORIT","FinalPriceType":"IHSM","ReferenceRate":"NATURAL GAS-MONTHLY INDEX S. TEXAS (TETCO)-GAS DAILY PRICE GUIDE","OtherReferenceRate":"NATURAL GAS-MONTHLY INDEX W. LOUISIANA (TETCO)-GAS DAILY PRICE GUIDE","BaseProduct":"OTHR","OtherBaseProduct":"OTHR","SubProduct":"","AdditionalSubProduct":"","OtherSubProduct":"","OtherAdditionalSubProduct":""},"ISIN":{"ISIN":"EZBBH1XR9GV6","Status":"New"},"TemplateVersion":1,"Derived":{"CommodityDerivativeIndicator":"TRUE","UnderlyingAssetType":"Multi Commodity","IssuerorOperatoroftheTradingVenueIdentifier":"NA","PriceMultiplier":1,"FullName":"Commodities Swap Basis_Swap OTHR OTHR SOS 22081210","ShortName":"NA/Swap OTHR SOS 22081210","ClassificationType":"STQTXE"}}
{"Header":{"AssetClass":"Commodities","InstrumentType":"Swap","UseCase":"Multi_Exotic_Swap","Level":"InstRefDataReporting"},"Attributes":{"NotionalCurrency":"LRD","ExpiryDate":"2200-01-31","ReturnorPayoutTrigger":"Contract for Difference (CFD)","DeliveryType":"CASH","TransactionType":"TAPO","FinalPriceType":"EXOF","UnderlyingInstrumentIndex":["BCOMF6","BCOMNG3"]},"ISIN":{"ISIN":"EZ286HJVY4Q2","Status":"New"},"TemplateVersion":1,"Derived":{"CommodityDerivativeIndicator":"TRUE","IssuerorOperatoroftheTradingVenueIdentifier":"NA","PriceMultiplier":1,"UnderlyingAssetType":"Multi Commodity","BaseProduct":"MCEX","SubProduct":"","AdditionalSubProduct":"","FullName":"Commodities Multi_Exotic_Swap MCEX LRD 22000131","ShortName":"NA/Swap MCEX LRD 22000131","ClassificationType":"STQCXC"}}
{"Header":{"AssetClass":"Commodities","InstrumentType":"Option","UseCase":"Option","Level":"InstRefDataReporting"},"Attributes":{"NotionalCurrency":"TND","ExpiryDate":"2209-10-18","OptionType":"OPTL","OptionExerciseStyle":"AMER","ValuationMethodorTrigger":"Asian","DeliveryType":"CASH","TransactionType":"OTHR","FinalPriceType":"IHSM","ReferenceRate":"NATURAL GAS-NGPL (NICOR, NIPSCO, PGLC CITYGATE), NBPL-NICOR-ICE/10X MONTHLY","BaseProduct":"OTHR","SubProduct":"","AdditionalSubProduct":""},"ISIN":{"ISIN":"EZ2TK5CWL9Y4","Status":"New"},"TemplateVersion":1,"Derived":{"CommodityDerivativeIndicator":"TRUE","UnderlyingAssetType":"Other","IssuerorOperatoroftheTradingVenueIdentifier":"NA","PriceMultiplier":1,"FullName":"Commodities Option OTHR TND 22091018","ShortName":"NA/O OTHR OPTL TND 22091018","ClassificationType":"HTMHAC"}}
{"Header":{"AssetClass":"Commodities","InstrumentType":"Option","UseCase":"Multi_Exotic_Option","Level":"InstRefDataReporting"},"Attributes":{"NotionalCurrency":"SOS","ExpiryDate":"2209-10-18","UnderlyingInstrumentIndex":["BCOMSI2","BCOMPR3T"],"OptionType":"CALL","OptionExerciseStyle":"AMER","ValuationMethodorTrigger":"Other Path Dependent","DeliveryType":"CASH","TransactionType":"ORIT","FinalPriceType":"BLTC"},"ISIN":{"ISIN":"EZ82L36B6225","Status":"New"},"TemplateVersion":1,"Derived":{"CommodityDerivativeIndicator":"TRUE","IssuerorOperatoroftheTradingVenueIdentifier":"NA","PriceMultiplier":1,"UnderlyingAssetType":"Multi Commodity","BaseProduct":"MCEX","SubProduct":"","AdditionalSubProduct":"","FullName":"Commodities Multi_Exotic_Option MCEX SOS 22091018","ShortName":"NA/O MCEX Call SOS 22091018","ClassificationType":"HTQBPC"}}
文件的大小可以超过1 GB。
为了有效地使用这些,我需要将JSON行转换为JSON数组, Package 文件,前置“[”并追加“]”,并为每行使用逗号(,)分隔。
使文件看起来像这样(理论上):
[
{
"Header": {
"AssetClass": "Commodities",
"InstrumentType": "Swap",
"UseCase": "Basis_Swap",
"Level": "InstRefDataReporting"
},
"Attributes": {
"NotionalCurrency": "EUR",
"ExpiryDate": "2017-08-31",
"ReturnorPayoutTrigger": "Contract for Difference (CFD)",
"DeliveryType": "CASH",
"BaseProduct": "AGRI",
"OtherBaseProduct": "AGRI",
"TransactionType": "FUTR",
"FinalPriceType": "ARGM",
"ReferenceRate": "10PPM ULTRA LOW SULPHUR DIESEL-CARGOES CIF NWE/BASIS ARA-PLATTS EUROPEAN",
"OtherReferenceRate": "10PPM ULTRA LOW SULPHUR DIESEL-CARGOES CIF NWE/BASIS ARA-PLATTS EUROPEAN",
"SubProduct": "GROS",
"AdditionalSubProduct": "FWHT",
"OtherSubProduct": "GROS",
"OtherAdditionalSubProduct": "FWHT"
},
"ISIN": {
"ISIN": "EZ68CZDRFYY7",
"Status": "New"
},
"TemplateVersion": 1,
"Derived": {
"CommodityDerivativeIndicator": "TRUE",
"UnderlyingAssetType": "Multi Commodity",
"IssuerorOperatoroftheTradingVenueIdentifier": "NA",
"PriceMultiplier": 1,
"FullName": "Commodities Swap Basis_Swap AGRI GROS FWHT AGRI GROS FWHT EUR 20170831",
"ShortName": "NA/Swap AGRI FWHT FWHT EUR 20170831",
"ClassificationType": "STQCXC"
}
},
{
"Header": {
"AssetClass": "Commodities",
"InstrumentType": "Swap",
"UseCase": "Basis_Swap",
"Level": "InstRefDataReporting"
},
"Attributes": {
"NotionalCurrency": "EUR",
"ExpiryDate": "2017-08-31",
"ReturnorPayoutTrigger": "Contract for Difference (CFD)",
"DeliveryType": "CASH",
"BaseProduct": "AGRI",
"OtherBaseProduct": "AGRI",
"TransactionType": "FUTR",
"FinalPriceType": "ARGM",
"ReferenceRate": "10PPM ULTRA LOW SULPHUR DIESEL-CARGOES CIF NWE/BASIS ARA-PLATTS EUROPEAN",
"OtherReferenceRate": "10PPM ULTRA LOW SULPHUR DIESEL-CARGOES CIF NWE/BASIS ARA-PLATTS EUROPEAN",
"SubProduct": "GROS",
"AdditionalSubProduct": "FWHT",
"OtherSubProduct": "GROS",
"OtherAdditionalSubProduct": "FWHT"
},
"ISIN": {
"ISIN": "EZ68CZDRFYY7",
"Status": "New"
},
"TemplateVersion": 1,
"Derived": {
"CommodityDerivativeIndicator": "TRUE",
"UnderlyingAssetType": "Multi Commodity",
"IssuerorOperatoroftheTradingVenueIdentifier": "NA",
"PriceMultiplier": 1,
"FullName": "Commodities Swap Basis_Swap AGRI GROS FWHT AGRI GROS FWHT EUR 20170831",
"ShortName": "NA/Swap AGRI FWHT FWHT EUR 20170831",
"ClassificationType": "STQCXC"
}
}
]
所以我找到了jq,根据我的理解,我可以运行这个
jq --slurp 'map(select(. >= 2))' Inputfile.json > OutputFile.json
这是可行的,但当运行大于200 MB的文件时,我会出现“系统内存不足”错误(如果我使用伊势),如果我使用标准的Powershell或CMD,则需要很长时间。分钟(5+)
如果我把--slurp
从命令中去掉,它会工作,它会更快,但结果看起来像这样:
[
{
"Header": {
"AssetClass": "Commodities",
"InstrumentType": "Swap",
"UseCase": "Basis_Swap",
"Level": "InstRefDataReporting"
},
"Attributes": {
"NotionalCurrency": "EUR",
"ExpiryDate": "2017-08-31",
"ReturnorPayoutTrigger": "Contract for Difference (CFD)",
"DeliveryType": "CASH",
"BaseProduct": "AGRI",
"OtherBaseProduct": "AGRI",
"TransactionType": "FUTR",
"FinalPriceType": "ARGM",
"ReferenceRate": "10PPM ULTRA LOW SULPHUR DIESEL-CARGOES CIF NWE/BASIS ARA-PLATTS EUROPEAN",
"OtherReferenceRate": "10PPM ULTRA LOW SULPHUR DIESEL-CARGOES CIF NWE/BASIS ARA-PLATTS EUROPEAN",
"SubProduct": "GROS",
"AdditionalSubProduct": "FWHT",
"OtherSubProduct": "GROS",
"OtherAdditionalSubProduct": "FWHT"
},
"ISIN": {
"ISIN": "EZ68CZDRFYY7",
"Status": "New"
},
"TemplateVersion": 1,
"Derived": {
"CommodityDerivativeIndicator": "TRUE",
"UnderlyingAssetType": "Multi Commodity",
"IssuerorOperatoroftheTradingVenueIdentifier": "NA",
"PriceMultiplier": 1,
"FullName": "Commodities Swap Basis_Swap AGRI GROS FWHT AGRI GROS FWHT EUR 20170831",
"ShortName": "NA/Swap AGRI FWHT FWHT EUR 20170831",
"ClassificationType": "STQCXC"
}
}]
[{
"Header": {
"AssetClass": "Commodities",
"InstrumentType": "Swap",
"UseCase": "Basis_Swap",
"Level": "InstRefDataReporting"
},
"Attributes": {
"NotionalCurrency": "EUR",
"ExpiryDate": "2017-08-31",
"ReturnorPayoutTrigger": "Contract for Difference (CFD)",
"DeliveryType": "CASH",
"BaseProduct": "AGRI",
"OtherBaseProduct": "AGRI",
"TransactionType": "FUTR",
"FinalPriceType": "ARGM",
"ReferenceRate": "10PPM ULTRA LOW SULPHUR DIESEL-CARGOES CIF NWE/BASIS ARA-PLATTS EUROPEAN",
"OtherReferenceRate": "10PPM ULTRA LOW SULPHUR DIESEL-CARGOES CIF NWE/BASIS ARA-PLATTS EUROPEAN",
"SubProduct": "GROS",
"AdditionalSubProduct": "FWHT",
"OtherSubProduct": "GROS",
"OtherAdditionalSubProduct": "FWHT"
},
"ISIN": {
"ISIN": "EZ68CZDRFYY7",
"Status": "New"
},
"TemplateVersion": 1,
"Derived": {
"CommodityDerivativeIndicator": "TRUE",
"UnderlyingAssetType": "Multi Commodity",
"IssuerorOperatoroftheTradingVenueIdentifier": "NA",
"PriceMultiplier": 1,
"FullName": "Commodities Swap Basis_Swap AGRI GROS FWHT AGRI GROS FWHT EUR 20170831",
"ShortName": "NA/Swap AGRI FWHT FWHT EUR 20170831",
"ClassificationType": "STQCXC"
}
}
]
它为每一行创建一个数组,但数组不是逗号分隔的,这不是我想要的。
那么,如何处理包含多个JSON行的大文件,并为作为逗号分隔的单个数组生成的输入文件创建一个文件,而不使用slurp呢?
我读过有关输入的内容,但不确定这是否与我需要做的事情相关?
4条答案
按热度按时间e1xvtsh31#
也许我迟到了,但这就是你要找的!
r8uurelv2#
看起来
inputs
和slurp有同样的问题。我不知道如何在大文件上使用jq
来实现这一点,但sed
可以做到:输出:
说明
sed脚本由三个独立的替换组成。下面是它们在不同的行上:
tpgth1q73#
也许awk对眼睛更容易:
为了记录在案,这里有一个使用
input
的jq版本的无slurp解决方案:s4chpxco4#
我使用了一行node调用:
说明:
Node的
readline
是一个很好的逐行读取流的方法,它是asyncIterable
,可以使用(await... of)获取每一行,然后调用JSON.parse
以确保验证每一行是有效的json,然后调用JSON.stringify
将每一行转换回缩小的JSON;当然,你可以改为本地的
const arr = [];
,并将每行的obj推送到t中,然后在最后使用JSON.stringify,以获得最小化的JSON,但我喜欢这种几乎缩小的格式,即每一行的obj都缩小了,但仍逐行保留外部数组,以便我可以轻松地通过
wc -l
计数其行数来获得其总数