Skip to content

Instantly share code, notes, and snippets.

@sebringj
Last active January 17, 2020 21:10
Show Gist options
  • Save sebringj/617439bf2a5b8e5d044c0af96119f507 to your computer and use it in GitHub Desktop.
Save sebringj/617439bf2a5b8e5d044c0af96119f507 to your computer and use it in GitHub Desktop.
JSON to text in JavaScript
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(' ');
}
@sebringj
Copy link
Author

I use this to extract text from json specifically for full text searches on log dumps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment