解析 JSON 以获得特定值

解析 JSON 以获得特定值

我的 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 并将每个对象安装在keys 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命令将为每个对象创建一个包含家庭姓名和卡号的数组,然后将其打印为几个制表符分隔的值。

您可以轻松地添加您想要以类似方式提取的其他字段。

相关内容