Free Support Forum - aspose.com

Convert word to json in parent child hierarchy using aspose words java

Hi Team,

I want to convert my doc to JSON structure. I want to preserve the relationship among nodes like Section having tables having section having cells etc. Is this possible to do. I tried by iterating over ChildNodeCollection but its not working properly. Please help.

@yogesh30890,

We have logged your requirement in our issue tracking system. Your ticket number is WORDSNET-21493. We will further look into the details of this requirement and will keep you updated on the status of the linked issue.

@yogesh30890 You can try using DocumentVisitor to output document structure to JSON.

The issues you have found earlier (filed as WORDSNET-21493) have been fixed in this Aspose.Words for .NET 21.1 update and this Aspose.Words for Java 21.1 update.

Can you please help me with WORDSNET-21493. How to convert doc to json with new changes?

@yogesh30890,

Please try the following Java code of Aspose.Words.

System.out.println(getJson(new Document("C:\\Temp\\Test21493.docx")));


private static String getJson(Document doc) {
    StringBuilder sb = new StringBuilder();
    int indent = 1;
    sb.append(openJson());
    sb.append(openElement(doc, indent++));
    for (Section section : doc.getSections()) {
        sb.append(openElement(section, indent++));
        RefInt referenceToIndent = new RefInt(indent);
        handleContainer(sb, section.getBody(), referenceToIndent);
        indent = referenceToIndent.get();
        sb.append(closeElement(--indent, (section.getNextSibling() == null)));
    }
    sb.append(closeElement(--indent, true));
    sb.append(closeElement(0, true));
    return sb.toString();
}

private static void handleContainer(StringBuilder sb, CompositeNode container, RefInt indent) {
    if (!container.hasChildNodes())
        sb.append(openAndCloseElement(container, indent.get(), (container.getNextSibling() == null)));
    else {
        sb.append(openElement(container, indent.set(indent.get() + 1) - 1));
        for (Node node : (Iterable<Node>) container.getChildNodes()) {
            CompositeNode childContainer = as(node, CompositeNode.class);
            if (childContainer != null)
                handleContainer(sb, childContainer, indent);
            else
                handleNode(sb, node, indent);
        }
        sb.append(closeElement(indent.set(indent.get() - 1), (container.getNextSibling() == null)));
    }
}

private static void handleNode(StringBuilder sb, Node node, RefInt indent) {
    switch (node.getNodeType()) {
        case NodeType.RUN:
            sb.append(openElement(node, indent.set(indent.get() + 1) - 1));
            Run run = as(node, Run.class);
            // for shorter output
        {
            //sb.append(writeElement("Text", run.getText(), indent.get(), false));
            //handleFont(sb, run.getFont(), indent, true);
            sb.append(writeElement("Text", run.getText(), indent.get(), true));
        }
        sb.append(closeElement(indent.set(indent.get() - 1), (node.getNextSibling() == null)));
        break;
        default:
            break;
    }
}

private static void handleFont(StringBuilder sb, Font font, RefInt indent, boolean isLast) {
    sb.append(openElement("Font", indent.set(indent.get() + 1) - 1));
    sb.append(writeElement("Name", font.getName(), indent.get(), false));
    sb.append(writeElement("Size", font.getSize(), indent.get(), true));
    sb.append(closeElement(indent.set(indent.get() - 1), isLast));
}

private static String openJson() {
    return "{\n";
}

private static String openElement(Node node, int indent) {
    return openElement(getNodeName(node), indent);
}

private static String openElement(String name, int indent) {
    return getIndent(indent) + getQuoted(name) + " : {\n";
}

private static String openAndCloseElement(Node node, int indent, boolean isLast) {
    return getIndent(indent) + getQuoted(getNodeName(node)) + " : { }" + getComma(isLast) + "\n";
}

private static String writeElement(String name, String value, int indent, boolean isLast) {
    return getIndent(indent) + getQuoted(name) + " : " + getQuoted(value) + getComma(isLast) + "\n";
}

private static String writeElement(String name, double value, int indent, boolean isLast) {
    return getIndent(indent) + getQuoted(name) + " : " + value + getComma(isLast) + "\n";
}

private static String closeElement(int indent, boolean isLast) {
    return getIndent(indent) + "}" + getComma(isLast) + "\n";
}

private static String getNodeName(Node node) {
    return NodeType.toString(node.getNodeType());
}

private static String getQuoted(String value) {
    return "\"" + value + "\"";
}

private static String getComma(boolean isLastElement) {
    return isLastElement ? "" : ",";
}

private static String getIndent(int indent) {
    if (indent == 0)
        return "";

    char[] charArray = new char[indent];
    for (int i = 0; i < indent; i++)
        charArray[i] = ' ';

    return new java.lang.String(charArray);
}

public static <T> T as(Object obj, Class<T> type) {
    return type.isInstance(obj) ? (T) obj : null;
}

Attachment: Test21493.zip (9.5 KB)

This is generating invalid JSON with multiple keys for a paragraph. Attaching sample document.FA_Broker Dealer Agreement (1).zip (31.4 KB)

@yogesh30890,

I have produced following JSON from the “FA_Broker Dealer Agreement (1).docx” document that you shared by using the latest 21.1 version of Aspose.Words for Java.

Do you see the same problem in this JSON? Can you please provide a screenshot of the problematic Paragraph(s)? This will help us to pinpoint the issue.