Editing
Search for string in json files
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
Search for string in multiple JSON files == Approaches == === Approach 1: Content of JSON file contains the keyword(s) === * Input: (1) Specify the folder path (2) Specify the keyword(s) * Output: file name list * Command: {{kbd | key=<nowiki>find *.json | xargs grep -E "詐騙|詐欺" -sl</nowiki>}} to find json files which its content contains the string {{kbd | key=<nowiki>詐騙</nowiki>}} or {{kbd | key=<nowiki>詐欺</nowiki>}}. === Approach 2: The specified index/column contains the keyword(s) === * Requirement: Install [https://stedolan.github.io/jq/ jq] * Input: (1) Specify the folder path (2) Specify the keyword(s) (3) Specify the index/column of JSON * Output: file name and the value of specified index/column * Command 1: {{kbd | key=<nowiki>find . -name '*.json' | xargs -I {} jq '{"file": input_filename, "MYCOLUMN": ..|.MYCOLUMN?}' {} | jq -c 'select(.MYCOLUMN | contains("詐騙"))'</nowiki>}} to find json files which content of column {{kbd | key=MYCOLUMN}} contains the string {{kbd | key=<nowiki>詐騙</nowiki>}}<ref>[https://medium.com/@marvinirwin/bash-snippet-find-every-file-containing-a-non-empty-array-value-with-find-xargs-and-jq-cc456d3705e4 bash snippet: Find every file containing a non empty array value with find, xargs, and jq | by Marvin Irwin | Medium]</ref>. * Command 2 for multiple keywords using boolean OR operator: {{kbd | key=<nowiki>find . -name '*.json' | xargs -I {} jq '{"file": input_filename, "MYCOLUMN": ..|.MYCOLUMN?}' {} | jq -c 'select(.MYCOLUMN | contains("詐騙") or contains("詐欺"))'</nowiki>}} to find json files which content of column {{kbd | key=MYCOLUMN}} contains the string {{kbd | key=<nowiki>詐騙</nowiki>}} or {{kbd | key=<nowiki>詐欺</nowiki>}}. * Command 3 for multiple keywords using boolean AND operator: {{kbd | key=<nowiki>find . -name '*.json' | xargs -I {} jq '{"file": input_filename, "MYCOLUMN": ..|.MYCOLUMN?}' {} | jq -c 'select(.MYCOLUMN | contains("詐騙") and contains("詐欺"))'</nowiki>}} to find json files which content of column {{kbd | key=MYCOLUMN}} contains the string {{kbd | key=<nowiki>詐騙</nowiki>}} and {{kbd | key=<nowiki>詐欺</nowiki>}}. == References == <references /> [[Category:Search]] [[Category:Programming]] [[Category:String manipulation]]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Template:Kbd
(
edit
)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information