Analisa JSON para valores específicos

1

Eu tenho este arquivo JSON em uma única linha contendo milhares de registros e quero copiar seções dele, correspondendo a nomes de famílias ou números de cartão específicos.

Anexado é uma amostra contendo 3 conjuntos de dados. Cada seção exclusiva começa com {"card e termina com []} .

O detalhe exclusivo pesquisável em cada seção é o nome da família "38BA1234" ou o número do cartão "2132312312333344"

No arquivo JSON, quero recuperar cerca de 100 detalhes únicos, mas ele deve ser seção por seção, separados por vírgulas. Eu tentei grep e sed, sem sorte.

{"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": []}
    
por Figaro Zadok 16.02.2017 / 00:18

2 respostas

2

Em primeiro lugar, o seu json é inválido. Você tem vários objetos json válidos separados por vírgulas. Então você tem que tratar cada objeto como uma string json separada ou montar cada objeto como uma chave em um objeto externo. Aqui eu peguei o json e montei cada objeto nas chaves a , b , c .

você pode inspecionar seu erro copiando e colando seu json no link

então agora o json parece

arquivo

: 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": []
    }

}

Se você tem o PHP instalado, você pode fazer algo como:

arquivo

: 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;
}

da linha de comando, se você executar o script php, ele emitirá as chaves que você está direcionando:

php parse.php

saída:

38BA1234
2132312312333344
2134A213
3424567891123242
08A00598
212323311232A213

então você deve ser capaz de usar seus comandos grep, sed normais, etc.

    
por 16.02.2017 / 03:46
1

Usando algo como jq para extrair os nomes das famílias e os números dos cartões:

$ jq -r '.[]|[.household_name,.card_number]|@tsv' data.json
38BA1234        2132312312333344
2134A213        3424567891123242
08A00598        212323311232A213

Isso pressupõe que os dados sejam uma matriz JSON adequada de objetos. O comando jq criará uma matriz do nome da família e do número do cartão para cada objeto e, em seguida, o imprimirá como um par de valores separados por tabulações.

Você pode adicionar facilmente o outro campo que gostaria de extrair de maneira semelhante.

    
por 16.02.2017 / 10:06