Storing and Parsing JSON Objects
Serialize JavaScript objects to JSON before writing to AsyncStorage and deserialize them on read, handling null values for first-time app launches.
Why AsyncStorage Only Stores Strings
AsyncStorage is a string-only key-value store — you cannot store JavaScript objects, arrays, numbers, or booleans directly. To save any non-string value you must convert it to a string first, and convert it back when reading. The standard approach is JSON.stringify before writing and JSON.parse after reading.
Saving an Object with JSON.stringify
Call JSON.stringify on your JavaScript object before passing it to setItem. This converts the object to a JSON string that AsyncStorage can store. If the object contains functions, undefined, or circular references, JSON.stringify will omit or error on those values — keep stored objects plain and serializable.
const userProfile = {
name: 'Alice',
email: 'alice@example.com',
preferences: { theme: 'dark', language: 'en' },
};
async function saveProfile(profile: object) {
try {
await AsyncStorage.setItem('userProfile', JSON.stringify(profile));
} catch (err) {
console.error('Failed to save profile:', err);
}
}All lessons in this course
- Reading and Writing with AsyncStorage
- Storing and Parsing JSON Objects
- Building a Persistent Settings Screen
- Clearing Storage and Migration Strategies