私のJSONファイルには1行に何千ものレコードが含まれており、特定の姓またはカード番号に対応する部分をコピーしたいと思います。
3つのデータセットを含む例が添付されています。各固有セクションには開始{"card
と終了があります[]}
。
各セクションで検索できる唯一の詳細は姓"38BA1234"
またはカード番号です。"2132312312333344"
JSONファイルから約100個の固有の詳細を検索したいのですが、セクションごとに実行してコンマで区切る必要があります。私はgrepとsedを試しましたが、成功しませんでした。
{"card_serial_number": 58913, "household_uuid": "2133d032f4fb45d88198d39753f50635", "interventions": [903, 905], "qr_code": "{\"version\": 1, \"members\": [{\"firstName\": \"MUSA\", \"middleName\": null, \"lastName\": \"MODU\", \"dateOfBirth\": \"1985-02-14\", \"gender\": \"Male\", \"recipient\": \"Principal\", \"id\": 17327891}, {\"firstName\": \"SADIQ\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"2014-02-14\", \"gender\": \"Male\", \"recipient\": \"\", \"id\": 17327886}, {\"firstName\": \"MUSTAPHA\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"2012-02-14\", \"gender\": \"Male\", \"recipient\": \"\", \"id\": 17327887}, {\"firstName\": \"MARYAM\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"2007-02-14\", \"gender\": \"Female\", \"recipient\": \"\", \"id\": 17327889}, {\"firstName\": \"FALMATA\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"1993-02-14\", \"gender\": \"Female\", \"recipient\": \"\", \"id\": 17327890}, {\"firstName\": \"ABUR\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"2010-02-14\", \"gender\": \"Male\", \"recipient\": \"\", \"id\": 17327888}], \"card\": \"2132312312333344\", \"householdID\": 4203050}", "household_name": "38BA1234", "compressQR": 1, "pin_number": "8119", "location": "Bidu llari", "recipient": {"picture": "/9j/4AAQSkZJRgA9LaWBQrkEj60UcjlqTa5//Z", "first_name": "MUSA", "last_name": "MODU", "gender": "Male", "document_number": "38BA1234_01", "fingerprints": [{"code": "Rk1S1AICTAVbKAAAA", "finger": "Forefinger", "hand": "Right hand"}, {"code": "RIBXAJHAAAAA", "finger": "Ringfinger", "hand": "Right hand"}, {"code": "bAECqARTmAAAA", "finger": "Middlefinger", "hand": "Right hand"}, {"code": "Rk1SACDyATbOAIARAUIzAAAA", "finger": "Thumb", "hand": "Right hand"}, {"code": "IEPAWrzAICEAX/sAAAA", "finger": "Littlefinger", "hand": "Right hand"}, {"code": "RAIBPARu6AEC/Ab/1AIBoAQhNAEEMARnJAEDGAWKSAED+AYu3AAAA", "finger": "Forefinger", "hand": "Left hand"}, {"code": "Rk1vAED5AOi9AIDuAFN6AAAA", "finger": "Ringfinger", "hand": "Left hand"}, {"code": "Rk1SACAyMAAAAAGkAEElASa6AICwAdD6AECZADaQAAAA", "finger": "Middlefinger", "hand": "Left hand"}, {"code": "Rk1SDaAXS3AECXAa8VAECzAFHkAAAA", "finger": "Thumb", "hand": "Left hand"}, {"code": "RkQGAEBRAdCOAEAVAYT9AAAA", "finger": "Littlefinger", "hand": "Left hand"}], "birthdate": "19850731", "document_type": "IOM ID"}, "card_number": "2132312312333344", "alternates": []},{"card_serial_number": 58923, "household_uuid": "6db84a1c50614b53b84fc67c581dbe7f", "interventions": [903, 905], "qr_code": "{\"version\": 1, \"members\": [{\"firstName\": \"HAJJA\", \"middleName\": \"FATIMA\", \"lastName\": \"MUSA\", \"dateOfBirth\": \"1978-02-14\", \"gender\": \"Female\", \"recipient\": \"Principal\", \"id\": 17327577}, {\"firstName\": \"TOMA\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"2007-02-14\", \"gender\": \"Female\", \"recipient\": \"\", \"id\": 17327575}, {\"firstName\": \"FANNE\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"2002-02-14\", \"gender\": \"Female\", \"recipient\": \"\", \"id\": 17327576}, {\"firstName\": \"ADAMA\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"2010-02-14\", \"gender\": \"Female\", \"recipient\": \"\", \"id\": 17327574}], \"card\": \"3424567891123242\", \"householdID\": 4202992}", "household_name": "2134A213", "compressQR": 1, "pin_number": "9438", "location": "Bidu llari", "recipient": {"picture": "/9j/4AAQIrq9NI21yNj25rp9Mb5V/KvOrHQi5c+tZdyvJ71q3A+XNZ9yMjIqabEZjjmnRgdaWVajU4Ix0rpJNC1YAitOF8isaJsGr9vJyM1EogaSPyOaKjTJOaKxegH/2Q==", "first_name": "HAJJA", "last_name": "MUSA", "gender": "Female", "document_number": "2134A213_01", "fingerprints": [{"code": "RkDWART3AECeATdrAAAA", "finger": "Forefinger", "hand": "Right hand"}, {"code": "dAEP/AICmAEaJAEBtAKChAAAA", "finger": "Ringfinger", "hand": "Right hand"}, {"code": "Rk1XAPWNAECbAJPGAEDUARAfAICwAKjLAEBxAN/fAIBXAIooAICrAHSAAIDGAVZwAAAA", "finger": "Middlefinger", "hand": "Right hand"}, {"code": "Rk1CwAYRwAAAA", "finger": "Thumb", "hand": "Right hand"}, {"code": "DxAcaQAIBbAF0DAEB9AQ4hAICEAQbLAICbARrcAECbAS/XAAAA", "finger": "Littlefinger", "hand": "Right hand"}, {"code": "RkAasHAIDyAIrpAIDFAbcHAIDnAJdoAIBgAQDAAAAA", "finger": "Forefinger", "hand": "Left hand"}, {"code": "OFVAIDHAQDGAIBLALCRAICVALdpAAAA", "finger": "Ringfinger", "hand": "Left hand"}, {"code": "Rk1SApSAEDRAE16AIDxAJvfAECvAYsJAEEMAQZGAICdAar9AAAA", "finger": "Middlefinger", "hand": "Left hand"}, {"code": "MASBrAAAA", "finger": "Thumb", "hand": "Left hand"}, {"code": "ID4ARTGAIB6AY8DAIC6AIHmAIDXAKTLAAAA", "finger": "Littlefinger", "hand": "Left hand"}], "birthdate": "19780731", "document_type": "IOM ID"}, "card_number": "3424567891123242", "alternates": []},{"card_serial_number": 579001, "household_uuid": "0444ed63908646a898148abcea00f2a9", "interventions": [903, 905], "qr_code": "{\"version\": 1, \"members\": [{\"firstName\": \"HAJJA\", \"middleName\": null, \"lastName\": \"DANLADI\", \"dateOfBirth\": \"1990-02-14\", \"gender\": \"Female\", \"recipient\": \"Principal\", \"id\": 17321251}, {\"firstName\": \"FATIMA\", \"middleName\": null, \"lastName\": \"MOHAMMED\", \"dateOfBirth\": \"2014-02-14\", \"gender\": \"Female\", \"recipient\": \"\", \"id\": 17321250}], \"card\": \"212323311232A213\", \"householdID\": 4201878}", "household_name": "08A00598", "compressQR": 1, "pin_number": "8141", "location": "Bidu llari", "recipient": {"picture": "/9j/4AAQSkrDLgXB5opIJBIMN1HeikB/9k=", "first_name": "HAJJA", "last_name": "DANLADI", "gender": "Female", "document_number": "08A00598_01", "fingerprints": [{"code": "XmwAIDHAYJLAIBuAMyNAAAA", "finger": "Forefinger", "hand": "Right hand"}, {"code": "Rk1SAEDoATxSAED5AYQNAAAA", "finger": "Ringfinger", "hand": "Right hand"}, {"code": "Rk1SACAMz6AEA2APSyAEBhAP69AAAA", "finger": "Middlefinger", "hand": "Right hand"}, {"code": "Rk1SACAyECPAG95AIC2AIXuAEEBAUbcAEAgAaDNAEAwAaXQAIDKAL5yAEAOAVurAAAA", "finger": "Thumb", "hand": "Right hand"}, {"code": "HAICwAXFDAIDNASDSAECzAavpAEDtAY/aAEBuAVE3AAAA", "finger": "Littlefinger", "hand": "Right hand"}, {"code": "AEETAXIhAEAsAV5mAAAA", "finger": "Forefinger", "hand": "Left hand"}, {"code": "AAAA", "finger": "Ringfinger", "hand": "Left hand"}, {"code": "ICYAXd3AECwAXcDAAAA", "finger": "Middlefinger", "hand": "Left hand"}, {"code": "RAEBPAcNQAICYAM2QAEE1AY7AAAAA", "finger": "Thumb", "hand": "Left hand"}, {"code": "Rk1S9AIDLATUuAAAA", "finger": "Littlefinger", "hand": "Left hand"}], "birthdate": "19900731", "document_type": "IOM ID"}, "card_number": "212323311232A213", "alternates": []}
答え1
まず、jsonが無効です。コンマで区切られた複数の有効なjsonオブジェクトがあります。したがって、各オブジェクトを別々のjson文字列として扱うか、各オブジェクトを外部オブジェクトのキーとしてインストールする必要があります。ここでは、jsonを取得し、キー、 、a
にb
各オブジェクトをインストールしますc
。
jsonをコピーして貼り付けてエラーを確認できます。http://jsonlint.com/
これでjsonは次のようになります。
文書:data.json
{
"a": {
"card_serial_number": 58913,
"household_uuid": "2133d032f4fb45d88198d39753f50635",
"interventions": [903, 905],
"qr_code": "{\"version\": 1, \"members\": [{\"firstName\": \"MUSA\", \"middleName\": null, \"lastName\": \"MODU\", \"dateOfBirth\": \"1985-02-14\", \"gender\": \"Male\", \"recipient\": \"Principal\", \"id\": 17327891}, {\"firstName\": \"SADIQ\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"2014-02-14\", \"gender\": \"Male\", \"recipient\": \"\", \"id\": 17327886}, {\"firstName\": \"MUSTAPHA\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"2012-02-14\", \"gender\": \"Male\", \"recipient\": \"\", \"id\": 17327887}, {\"firstName\": \"MARYAM\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"2007-02-14\", \"gender\": \"Female\", \"recipient\": \"\", \"id\": 17327889}, {\"firstName\": \"FALMATA\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"1993-02-14\", \"gender\": \"Female\", \"recipient\": \"\", \"id\": 17327890}, {\"firstName\": \"ABUR\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"2010-02-14\", \"gender\": \"Male\", \"recipient\": \"\", \"id\": 17327888}], \"card\": \"2132312312333344\", \"householdID\": 4203050}",
"household_name": "38BA1234",
"compressQR": 1,
"pin_number": "8119",
"location": "Bidu llari",
"recipient": {
"picture": "/9j/4AAQSkZJRgA9LaWBQrkEj60UcjlqTa5//Z",
"first_name": "MUSA",
"last_name": "MODU",
"gender": "Male",
"document_number": "38BA1234_01",
"fingerprints": [{
"code": "Rk1S1AICTAVbKAAAA",
"finger": "Forefinger",
"hand": "Right hand"
}, {
"code": "RIBXAJHAAAAA",
"finger": "Ringfinger",
"hand": "Right hand"
}, {
"code": "bAECqARTmAAAA",
"finger": "Middlefinger",
"hand": "Right hand"
}, {
"code": "Rk1SACDyATbOAIARAUIzAAAA",
"finger": "Thumb",
"hand": "Right hand"
}, {
"code": "IEPAWrzAICEAX/sAAAA",
"finger": "Littlefinger",
"hand": "Right hand"
}, {
"code": "RAIBPARu6AEC/Ab/1AIBoAQhNAEEMARnJAEDGAWKSAED+AYu3AAAA",
"finger": "Forefinger",
"hand": "Left hand"
}, {
"code": "Rk1vAED5AOi9AIDuAFN6AAAA",
"finger": "Ringfinger",
"hand": "Left hand"
}, {
"code": "Rk1SACAyMAAAAAGkAEElASa6AICwAdD6AECZADaQAAAA",
"finger": "Middlefinger",
"hand": "Left hand"
}, {
"code": "Rk1SDaAXS3AECXAa8VAECzAFHkAAAA",
"finger": "Thumb",
"hand": "Left hand"
}, {
"code": "RkQGAEBRAdCOAEAVAYT9AAAA",
"finger": "Littlefinger",
"hand": "Left hand"
}],
"birthdate": "19850731",
"document_type": "IOM ID"
},
"card_number": "2132312312333344",
"alternates": []
},
"b": {
"card_serial_number": 58923,
"household_uuid": "6db84a1c50614b53b84fc67c581dbe7f",
"interventions": [903, 905],
"qr_code": "{\"version\": 1, \"members\": [{\"firstName\": \"HAJJA\", \"middleName\": \"FATIMA\", \"lastName\": \"MUSA\", \"dateOfBirth\": \"1978-02-14\", \"gender\": \"Female\", \"recipient\": \"Principal\", \"id\": 17327577}, {\"firstName\": \"TOMA\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"2007-02-14\", \"gender\": \"Female\", \"recipient\": \"\", \"id\": 17327575}, {\"firstName\": \"FANNE\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"2002-02-14\", \"gender\": \"Female\", \"recipient\": \"\", \"id\": 17327576}, {\"firstName\": \"ADAMA\", \"middleName\": null, \"lastName\": \"MUSA\", \"dateOfBirth\": \"2010-02-14\", \"gender\": \"Female\", \"recipient\": \"\", \"id\": 17327574}], \"card\": \"3424567891123242\", \"householdID\": 4202992}",
"household_name": "2134A213",
"compressQR": 1,
"pin_number": "9438",
"location": "Bidu llari",
"recipient": {
"picture": "/9j/4AAQIrq9NI21yNj25rp9Mb5V/KvOrHQi5c+tZdyvJ71q3A+XNZ9yMjIqabEZjjmnRgdaWVajU4Ix0rpJNC1YAitOF8isaJsGr9vJyM1EogaSPyOaKjTJOaKxegH/2Q==",
"first_name": "HAJJA",
"last_name": "MUSA",
"gender": "Female",
"document_number": "2134A213_01",
"fingerprints": [{
"code": "RkDWART3AECeATdrAAAA",
"finger": "Forefinger",
"hand": "Right hand"
}, {
"code": "dAEP/AICmAEaJAEBtAKChAAAA",
"finger": "Ringfinger",
"hand": "Right hand"
}, {
"code": "Rk1XAPWNAECbAJPGAEDUARAfAICwAKjLAEBxAN/fAIBXAIooAICrAHSAAIDGAVZwAAAA",
"finger": "Middlefinger",
"hand": "Right hand"
}, {
"code": "Rk1CwAYRwAAAA",
"finger": "Thumb",
"hand": "Right hand"
}, {
"code": "DxAcaQAIBbAF0DAEB9AQ4hAICEAQbLAICbARrcAECbAS/XAAAA",
"finger": "Littlefinger",
"hand": "Right hand"
}, {
"code": "RkAasHAIDyAIrpAIDFAbcHAIDnAJdoAIBgAQDAAAAA",
"finger": "Forefinger",
"hand": "Left hand"
}, {
"code": "OFVAIDHAQDGAIBLALCRAICVALdpAAAA",
"finger": "Ringfinger",
"hand": "Left hand"
}, {
"code": "Rk1SApSAEDRAE16AIDxAJvfAECvAYsJAEEMAQZGAICdAar9AAAA",
"finger": "Middlefinger",
"hand": "Left hand"
}, {
"code": "MASBrAAAA",
"finger": "Thumb",
"hand": "Left hand"
}, {
"code": "ID4ARTGAIB6AY8DAIC6AIHmAIDXAKTLAAAA",
"finger": "Littlefinger",
"hand": "Left hand"
}],
"birthdate": "19780731",
"document_type": "IOM ID"
},
"card_number": "3424567891123242",
"alternates": []
},
"c": {
"card_serial_number": 579001,
"household_uuid": "0444ed63908646a898148abcea00f2a9",
"interventions": [903, 905],
"qr_code": "{\"version\": 1, \"members\": [{\"firstName\": \"HAJJA\", \"middleName\": null, \"lastName\": \"DANLADI\", \"dateOfBirth\": \"1990-02-14\", \"gender\": \"Female\", \"recipient\": \"Principal\", \"id\": 17321251}, {\"firstName\": \"FATIMA\", \"middleName\": null, \"lastName\": \"MOHAMMED\", \"dateOfBirth\": \"2014-02-14\", \"gender\": \"Female\", \"recipient\": \"\", \"id\": 17321250}], \"card\": \"212323311232A213\", \"householdID\": 4201878}",
"household_name": "08A00598",
"compressQR": 1,
"pin_number": "8141",
"location": "Bidu llari",
"recipient": {
"picture": "/9j/4AAQSkrDLgXB5opIJBIMN1HeikB/9k=",
"first_name": "HAJJA",
"last_name": "DANLADI",
"gender": "Female",
"document_number": "08A00598_01",
"fingerprints": [{
"code": "XmwAIDHAYJLAIBuAMyNAAAA",
"finger": "Forefinger",
"hand": "Right hand"
}, {
"code": "Rk1SAEDoATxSAED5AYQNAAAA",
"finger": "Ringfinger",
"hand": "Right hand"
}, {
"code": "Rk1SACAMz6AEA2APSyAEBhAP69AAAA",
"finger": "Middlefinger",
"hand": "Right hand"
}, {
"code": "Rk1SACAyECPAG95AIC2AIXuAEEBAUbcAEAgAaDNAEAwAaXQAIDKAL5yAEAOAVurAAAA",
"finger": "Thumb",
"hand": "Right hand"
}, {
"code": "HAICwAXFDAIDNASDSAECzAavpAEDtAY/aAEBuAVE3AAAA",
"finger": "Littlefinger",
"hand": "Right hand"
}, {
"code": "AEETAXIhAEAsAV5mAAAA",
"finger": "Forefinger",
"hand": "Left hand"
}, {
"code": "AAAA",
"finger": "Ringfinger",
"hand": "Left hand"
}, {
"code": "ICYAXd3AECwAXcDAAAA",
"finger": "Middlefinger",
"hand": "Left hand"
}, {
"code": "RAEBPAcNQAICYAM2QAEE1AY7AAAAA",
"finger": "Thumb",
"hand": "Left hand"
}, {
"code": "Rk1S9AIDLATUuAAAA",
"finger": "Littlefinger",
"hand": "Left hand"
}],
"birthdate": "19900731",
"document_type": "IOM ID"
},
"card_number": "212323311232A213",
"alternates": []
}
}
PHPがインストールされている場合は、次のことができます。
文書:parse.php
<?php
$json = file_get_contents('data.json');
$data_obj = json_decode($json);
/* var_dump($data_obj); */
foreach ( $data_obj as $key => $value ) {
echo $data_obj->{$key}->household_name . PHP_EOL;
echo $data_obj->{$key}->card_number . PHP_EOL;
}
コマンドラインからPHPスクリプトを実行すると、検索したいキーが出力されます。
php parse.php
出力:
38BA1234
2132312312333344
2134A213
3424567891123242
08A00598
212323311232A213
これにより、通常のgrep、sedコマンドなどを使用できます。
答え2
次のようなものを使用してくださいjq
姓とカード番号の抽出:
$ jq -r '.[]|[.household_name,.card_number]|@tsv' data.json
38BA1234 2132312312333344
2134A213 3424567891123242
08A00598 212323311232A213
これは、データがJSONオブジェクトの正しい配列であると仮定します。このjq
コマンドは、各オブジェクトの家族名とカード番号を含む配列を作成し、複数のタブで区切られた値で印刷します。
同様の方法で抽出したい他のフィールドを簡単に追加できます。