Home > Enterprise >  How can I extract specific lines from a json document?
How can I extract specific lines from a json document?

Time:09-13

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",

CodePudding user response:

you can use grep eventually :

grep '^\s*"name":' your_file.json

CodePudding user response:

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

In Vim, to left align all the lines, do :%left or even just :%le.

CodePudding user response:

In VSC

  • select institution_id
  • execute Selection > Select All Occurenses
  • Arrow Left
  • Ctrl X
  • Esc
  • Ctrl V
  • Related