question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[TypeError: JSON.stringify cannot serialize cyclic structures.] object

See original GitHub issue

I’m getting error [TypeError: JSON.stringify cannot serialize cyclic structures.] object when i try to convert realm object to json object using JSON.parse(JSON.stringify(item))

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
steffenaggercommented, Sep 10, 2020

Yes toJSON() returns the same structure, where instances of Realm.Object & Realm.Collection has been replaced by flat JS objects & arrays. For more info on toJSON, see MDN toJSON.

Realm.JsonSerializationReplacer is intended as a replacer (2nd argument) for JSON.stringify, is this how you utilized it? (see example bellow).

const chats = realm.objects('Chat');
const serialized = JSON.stringify(chats, Realm.JsonSerializationReplacer)

If downgrading to realm@6.0.3 fixed the circular structure error, could I ask you to supply some code or a small project (node/RN version used), where the error occurs in 6.1.0 and not in 6.0.3?

0reactions
steffenaggercommented, Sep 16, 2020

@mahipalsingh-syt Realm.JsonSerializationReplacer is only intended for serialization to a string, where serialization is not possible due to circular references (e.g. a JSON export) - the added $ref is to indicate, in a readable manner, to where the circular reference points. In other words: parsing it would require additional logic.

If you’re “just” trying to make a copy of the Realm.Object as plain JS (your naming highly indicates this), then please try this instead (only works on 6.1.0+):

/** Converts a Realm.Object into a plain JS object */
export const copyRealmObject = (item: Realm.Object) => item.toJSON()

/** Converts a Realm.Collection, Realm.List or Realm.Results into a plain JS array, containing plain JS objects */
export const copyRealmCollection = (collection: Realm.Collection) => collection.toJSON()

This should keep the circular references through the objects.

Let me know if it yields problems.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serializing object that contains cyclic object value
Use the second parameter of stringify , the replacer function, to exclude already serialized objects: var seen = []; JSON.stringify(obj, ...
Read more >
TypeError: cyclic object value - JavaScript - MDN Web Docs
The JavaScript exception "cyclic object value" occurs when object ... TypeError: JSON.stringify cannot serialize cyclic structures. (Safari) ...
Read more >
Random error 'JSON.stringify cannot serialize cyclic structures'
The error was on trying to call the API (Executing the API post method) it failures random on different days only with mobile...
Read more >
React Native JSON.stringify cannot serialize cyclical structures
iOS : React Native JSON. stringify cannot serialize cyclical structures [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] ...
Read more >
iOs9 XCODE error: JSON.stringify cannot serialize cyclic ...
error JSON.stringify()ing argument: TypeError: JSON.stringify cannot serialize cyclic structures. I do not know what is causing this error ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found