Search code examples
jsonshellvisual-studio-codevimsublimetext3

How can I extract specific lines from a json document?


I have a json file with thousands of lines and 90 json objects. Each object come with the following structure:

{
        "country_codes": [
            "GB"
        ],
        "institution_id": "ins_118309",
        "name": "Barclaycard (UK) - Online Banking: Personal", // I want to extract this line only
        "oauth": true,
        "products": [
            "assets",
            "auth",
            "balance",
            "transactions",
            "identity",
            "standing_orders"
        ],
        "routing_numbers": []
    },

For the ninety objects, I would like to delete all the lines and keep only the one with the name of the institution.

I guess that I will have to use a regex here? I'm happy to use with vim, sublime, vscode or any other code editor that will alow me to do so

How can I extract these lines so I will stay with the following 90 lines?

 "name": "Barclaycard (UK) - Online Banking: Personal",
 "name": "Metro Bank - Commercial and Business Online Plus",
 ...
 ...

 "name": "HSBC (UK) - Business",

Solution

  • If you must use a code editor, then in Vim you can delete all lines not matching a pattern with: :v/^\s*"name":/d

    The above pattern says:

    • ^ line begins with
    • \s* zero or more white spaces
    • "name:" (pretty explanatory)

    Although it's better to use a dedicated tool for parsing json files rather than regex as json is not a 'regular language'.

    Bonus

    If you do end up doing it in Vim, you can finish up by left align all the lines, do :%left or even just :%le.