liil'boo
liil'boo3y ago

✅ – Liil' Boo – 08-41 Aug 10

Hey Would something bad happen (or nothing), if I try to do
JSON.parse(thing)
JSON.parse(thing)
and 'thing' is not an object or array ? Or does that change nothing ?
28 Replies
liil'boo
liil'booOP3y ago
!title <JSON.parse question>
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
liil'boo
liil'booOP3y ago
JSON.parse() - JavaScript | MDN
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
liil'boo
liil'booOP3y ago
Could be anything else What i'm really trying to is
JSON.parse(window.LocalStorage.getItem('key')
JSON.parse(window.LocalStorage.getItem('key')
And I want to be sure how to catch error And if it always returns a string Documentation didn't help me figure it out Or I didn't understand
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
liil'boo
liil'booOP3y ago
Wait, what If I stringify everything before set to Local Storage ? Then I would always deal with strings, right ? As such
window.localStorage.setItem('key', JSON.stringy(value)
window.localStorage.setItem('key', JSON.stringy(value)
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
liil'boo
liil'booOP3y ago
It's purely theoritical, I'm not coding (Sorry I should have mentioned it) Well, thank you for your time I go with the approach of stringify-ing everything
marz
marz3y ago
either way localstorage can only store strings
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
liil'boo
liil'booOP3y ago
I guess so, But documentation doesn't say what happens if I try
localStorage.setItem('key', true)
localStorage.setItem('key', true)
Would it become the string 'true' ? Or throw an Error
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
liil'boo
liil'booOP3y ago
As a string ? So localStorage can store booleans ? 😮
Eva
Eva3y ago
It'll stringify it yes
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
liil'boo
liil'booOP3y ago
Thanks for confirmation
Eva
Eva3y ago
As if you set .setItem('... ', {}); it'll just stringify it into [object Object]
liil'boo
liil'booOP3y ago
window.localStorage.setItem('key', JSON.stringy(value) 
window.localStorage.setItem('key', JSON.stringy(value) 
I'll stick to this approach, stringying everything, I'll think about accuracy* later Thanks timo, thanks Eva
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
liil'boo
liil'booOP3y ago
I thought JSON.stringify would be harmless on things that are already a string
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
liil'boo
liil'booOP3y ago
That's bad?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
liil'boo
liil'booOP3y ago
Well, better safe than sorry I'll go with your advice, dear stranger Thanks again
reactibot
reactibot3y ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1006844848428568587
liil'boo
liil'booOP3y ago
!close
reactibot
reactibot3y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1006844848428568587