Last active
January 17, 2020 21:10
-
-
Save sebringj/617439bf2a5b8e5d044c0af96119f507 to your computer and use it in GitHub Desktop.
JSON to text in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function textFromJson(json) { | |
if (json === null || json === undefined) { | |
return ''; | |
} | |
if (!Array.isArray(json) && !Object.getPrototypeOf(json).isPrototypeOf(Object)) { | |
return '' + json; | |
} | |
const obj = {}; | |
for (const key of Object.keys(json)) { | |
obj[key] = textFromJson(json[key]); | |
} | |
return Object.values(obj).join(' '); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use this to extract text from json specifically for full text searches on log dumps