https://github.com/pudochu/extracting-json-from-string
(Simple!) Extracting JSON from String ✨
https://github.com/pudochu/extracting-json-from-string
Last synced: 7 months ago
JSON representation
(Simple!) Extracting JSON from String ✨
- Host: GitHub
- URL: https://github.com/pudochu/extracting-json-from-string
- Owner: Pudochu
- Created: 2023-04-19T15:21:18.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-24T08:12:04.000Z (over 1 year ago)
- Last Synced: 2025-03-17T02:46:32.597Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# extracting-json-from-string
(Simple!) Extracting JSON from String ✨```js
async function extractJSON(str) {
// If the input is already an object, return it directly.
if (typeof str === 'object') {
return str;
}// If the input is a string, try to parse it as JSON.
if (typeof str === 'string') {
try {
return JSON.parse(str);
} catch (e) {
// If the first parse attempt fails, try to find the part that contains a JSON object.
try {
const jsonStart = str.indexOf("{");
const jsonEnd = str.lastIndexOf("}");
if (jsonStart !== -1 && jsonEnd !== -1) {
const jsonString = str.slice(jsonStart, jsonEnd + 1);
return JSON.parse(jsonString);
}
} catch (e) {
// If the second parse attempt also fails, return false.
console.error(e);
return false;
}
}
}// If the input is neither a string nor an object, return false.
return false;
}// Sample usages to test the function:
extractJSON('demo string {"text": "hi!"}').then(console.log); // result: {"text": "hi!"}
extractJSON({"text": "hi!"}).then(console.log); // result: {"text": "hi!"}
extractJSON("hello").then(console.log); // result: false;
```