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",
6
Answers
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 withs*
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
.you can use grep eventually :
grep '^s*"name":' your_file.json
In VSC
institution_id
Arrow Left
Ctrl+X
Esc
Ctrl+V
In vscode (although I would think it is the same for any regex-handling editor), use this Find:
and replace with nothing. See regex101 demo.
^(?!s*"name":.*).*n?
: get all lines that are not followed by"name":...
including the newline so that line is completely discarded.^s*
gets the whitespace before"name":......
– also discarded since we are replacing all matches with nothing.Parsing JSON in Vim natively:
NB. Line continuation is only available when sourcing script from file. If run interactively then type command on a single line.
That doesn’t sound like the job for a text editor or even for regular expressions. How about using the right tool for the job™?
See https://stedolan.github.io/jq/.
If you really want to do it from a text editor, the simplest is still to filter the current buffer through a specialized external tool. In Vim, it would look like this:
See
:help filter
.FWIW, here it is with another right tool for the job™:
See https://github.com/tidwall/jj.