1

I have this JSON file in a single line containing thousands of records and I want to copy out sections of it, corresponding to specific household names or card numbers.

Attached is a sample containing 3 sets of data. Each unique section starts with {"card and ends with []}.

The unique searchable detail in each section is either household name "38BA1234" or card number "2132312312333344"

From the JSON file, I want to retrieve about 100 uniques details but it must be section by section, separated by commas. I have tried grep and sed, no luck.

{"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": []}
  • 3
    Use a dedicated JSON parser, like jshon... – jasonwryan Feb 15 '17 at 23:21
  • 4
    Or jq. – Michael Homer Feb 16 '17 at 0:01
  • Or jsoncatcmd; install with pip – ILMostro_7 Feb 16 '17 at 4:36
  • Thanks for your quick response & help Jeff, Michael and The_velour_fog but I do not want it in typical JSON format. I just want the information as as a single line text, extracted according to the unique number. I do not want it in any other format. The sections will be separated by comma. I think the title is misleading – Figaro Zadok Feb 16 '17 at 7:24
1

Firstly your json is invalid. You've got multiple valid json objects separated by commas. So you either have to treat each object as a separate json string or mount each object as a key on an outer object. Here I have taken your json and mounted each object on keys a, b, c.

you can inspect your error by copy pasting your json at http://jsonlint.com/

so now the json looks like

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

}

If you have PHP installed you could do something like:

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

from the command line if you run the php script it will output the keys you are targeting:

php parse.php

output:

38BA1234
2132312312333344
2134A213
3424567891123242
08A00598
212323311232A213

then you should be able to use your normal grep, sed commands etc

1

Using something like jq to extract the household names and card numbers:

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

This is assuming the data is a proper JSON array of objects. The jq command will create an array of the household name and card number for each object and then print it as a couple of tab-separated values.

You could easily add the other field that you'd like to extract in a similar manner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.