Api.FromJSON error

When I tried to TOJSON a paragraph with a Heading style (retaining style) and then FROMJSON, I got an error

var oNewDocumentStyle = oDocument.GetStyle(“Heading 1”);
oParagraph.SetStyle(oNewDocumentStyle);
var json = oParagraph.ToJSON(true,true);
var fjson = Api.FromJSON(json);

In fact, the problem I encountered was that if I TOJSON the document and then FROMJSON,
it would lose all the Heading style (the styles are all the same, but just the Heading style are missing).
I tried many methods, but none of them worked

var json = oDocument.ToJSON(true,true,true,true,true);
var fjson = Api.FromJSON(json);
oDocument.RemoveAllElements();
oDocument.InsertContent(fjson);

Hello @saopanda

Please provide a screenshot of the error that you’ve faced and a complete example of the code that would reproduce the issue so we can analyze it.

You can try using your script here and checking the browser console for more info about the errors:
https://api.onlyoffice.com/docbuilder/try

sorry im late

script

builder.CreateFile("docx");
var oDocument = Api.GetDocument();
var oParagraph = oDocument.GetElement(0);
oParagraph.AddText("This sample text is saved to the document.");

var oNewDocumentStyle = oDocument.GetStyle("Heading 1");
oParagraph.SetStyle(oNewDocumentStyle);

var json = oParagraph.ToJSON(true,true); 
// oParagraph.ToJSON(true,true)  make  FromJSON to error
// ToJSON(true,false) make FromJSON well
var fjson = Api.FromJSON(json);   // no this line, no error

Api.Save();
builder.SaveFile("docx", "Save.docx");
builder.CloseFile();

error

sdk-all.js:12976 TypeError: Cannot read properties of undefined (reading '$c')
    at ie.Wn (sdk-all.js:7307:100)
    at D.Ze (sdk-all-min.js:1049:53)
    at a.ia (sdk-all.js:1586:53)
    at D.ia (sdk-all-min.js:1023:309)
    at new ie (sdk-all.js:20:294)
    at z.Qjf (sdk-all.js:12713:417)
    at z.Crg (sdk-all.js:12712:307)
    at d.MBe (sdk-all.js:12124:467)
    at eval (eval at g (sdk-all.js:12976:370), <anonymous>:3:406)
    at eval (eval at g (sdk-all.js:12976:370), <anonymous>:3:457)
g @ sdk-all.js:12976
Oci @ sdk-all.js:12998
(匿名) @ sdk-all-min.js:745
postMessage(异步)
d.sendMessage @ api.js:776
d.callCommand @ api.js:778
postScript @ try:7880
dispatch @ scripts?v=4NQ_h99RetC7QDCGJUxIJlZOUP5wvOH17cZBUXMgQyY1:5
a.handle @ scripts?v=4NQ_h99RetC7QDCGJUxIJlZOUP5wvOH17cZBUXMgQyY1:5

heading style is lost

builder.CreateFile("docx");
var oDocument = Api.GetDocument();
var oParagraph = oDocument.GetElement(0);
oParagraph.AddText("This sample text is saved to the document.");

var oNewDocumentStyle = oDocument.GetStyle("Heading 1");
oParagraph.SetStyle(oNewDocumentStyle);


var json = oDocument.ToJSON(true,true,true,true,true);
var fjson = Api.FromJSON(json);
oDocument.RemoveAllElements();
oDocument.InsertContent(fjson);


Api.Save();
builder.SaveFile("docx", "Save.docx");
builder.CloseFile();

I think it’s FromJSON

Because I looked at the file after ToJSON, which contains the style information, it was just lost during the restore

English is not good, machine translation, please forgive me, thank you

Thank you for the information.
We will check the situation and I will update this thread once any news come up or in case any additional information will be needed.

I wanted to inform you that we have registered a bug in our internal track system for this issue:

However, as for the second alternative method, I want to point to the moment that parameter bWriteStyles is 6th in the method ToJSON and it is responsible for writing styles. In your example, you are passing only 5 parameters:

Has to be like that:
var json = oDocument.ToJSON(true,true,true,true,true,true);