내 JSON 파일에는 한 행에 수천 개의 레코드가 포함되어 있으며 특정 성 또는 카드 번호에 해당하는 부분을 복사하고 싶습니다.
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
명령은 각 개체의 가족 이름과 카드 번호를 포함하는 배열을 만든 다음 여러 탭으로 구분된 값으로 인쇄합니다.
유사한 방식으로 추출하려는 다른 필드를 쉽게 추가할 수 있습니다.