2017-06-29 6 views
0

これは私のjsonです。jqネストされたjson bashで特定の値を持つオブジェクトを選択します。

[ 
{ 
    "time": "2017-06-10 00:00:48-0400,317", 
    "UserInfo": { 
    "AppId": "ONE_SEARCH", 
    "UsageGroupId": "92600", 
    }, 
    "Items": [ 
    { 
    "PublicationCode": "", 
    "OpenUrlRefId": "", 
    "ReferringUrl": "N", 
    "OpenAccess": "0", 
    "ItmId": "1328515516" 
    } 
    ] 
}, 
{ 
    "time": "2017-06-10 00:00:48-0400,548", 
    "UserInfo": { 
    "AppId": "DIALOG", 
    "UsageGroupId": "1195735", 
    }, 
    "Items": [ 
    { 
    "Origin": "Alert", 
    "PublicationCode": "", 
    "NumberOfCopies": 1, 
    "ItmId": "1907446549" 
    }, 
    { 
    "Origin": "Alert", 
    "PublicationCode": "", 
    "NumberOfCopies": 1, 
    "ItmId": "1907446950", 
    } 
    ] 
} 
] 

jqを使用して、要素 "Items"に "Origin": "Alert"を持つオブジェクトを抽出します。 JQを使用することによってそれを行うにはどのように

{ 
"Items": [ 
    { 
    "Origin": "Alert", 
    "PublicationCode": "", 
    "NumberOfCopies": 1, 
    "ItmId": "1907446549", 
    "ReasonCode": "" 
    }, 
    { 
    "Origin": "Alert", 
    "PublicationCode": "", 
    "NumberOfCopies": 1, 
    "ItmId": "1907446950", 
    } 
] 
} 

{ 
"time": "2017-06-10 00:00:48-0400,548", 
"UserInfo": { 
    "AppId": "DIALOG", 
    "UsageGroupId": "1195735", 
}, 
"Items": [ 
    { 
    "Origin": "Alert", 
    "PublicationCode": "", 
    "NumberOfCopies": 1, 
    "ItmId": "1907446549" 
    }, 
    { 
    "Origin": "Alert", 
    "PublicationCode": "", 
    "NumberOfCopies": 1, 
    "ItmId": "1907446950", 
    } 
] 
} 

またはこの:そして、結果はこのようになりますでしょうか?私はいくつかの方法を試しましたが、それらのほとんどは "Origin"を含むすべての子オブジェクト: "Alert"を含む配列を返します。私はこれらの子どもたちがまだ構造を維持している必要があります。なぜなら、それらのうちのどれが一緒に起こったのか、別々に起こったのかを知る必要があるからです。

ところで、 "Origin"の唯一の値は "Alert"です。したがって、指定されたキー名を持つオブジェクトを選択する方法があれば、それも機能するはずです。

ありがとうございました! :)

答えて

0

フィルタ:

.[] | select(any(.Items[]; .Origin == "Alert")) 

は、最初に述べた許容結果を生じます。あなたのjqにany/2がない場合は、アップグレードをお勧めします。それはオプションではありません場合は、代わりに次のような単純なのではなく非効率的なフィルタを使用することができます。

.[] | select(.Items | map(.Origin) | index("Alert")) 

または:

.[] | select(reduce .Items[] as $item (false; . or ($item | .Origin == "Alert"))) 
+0

それは非常にうまく機能します!ありがとうございました。 :) – Eleanor

関連する問題