Linq Reporting Engine - JSON & WORD - dynamic json node

I want to output JSON values in Word, but the JSON nodes are dynamic.

For example, sometimes they are like this: {"data":{"occupation1": "Other Occupation"}}

, and sometimes they are like this: ‌{"data":{"occupation2": "Engineer"}}

So, I can’t use it like this: <<[data.occupation1]>>, please help, thanks!!

@zhenglifan

Provided JSON snippets are not valid, they sould be corrected. In this case, it seems like they should be as follows:

{"data":{"occupation1": "Other Occupation"}}
{"data":{"occupation2": "Engineer"}}

If this is the case, then you can use the following template syntax:

<<[data[0]]>>

@ivan.lyagin I need to replace the existing way of generating reports with Aspose. These json files already exist, and I want to implement the existing logic of the current report without modifying the json files, such as dynamic json node, because the json node name can sometimes be ‘occupation1’, sometimes ‘occupation2’ or ‘occupation3’; So I want to see if there is a way to easily get its value and display it in the word, such as : key.startsWith('occupation').value or <<[data.occupation%]>>;

If there is indeed no simple way, I will have to modify the JSON.

@zhenglifan

As mentioned in my previous comment, the provided JSON snippets are not valid. JsonDataSource throws an exception on loading {"data":{"occupation1": {"Other Occupation"}}}, for example. It seems to be a typo or misuse, that is why I suggested to fix the snippets like so: {"data":{"occupation1": "Other Occupation"}}, which makes them valid JSON objects that can be loaded by JsonDataSource. For the fixed JSON snippets, the suggested template syntax makes what is requested.

If this does not help, then please provide us with sample valid JSON data for further analysis on our end.

@ivan.lyagin Sorry for the grammatical errors from manual typing. I’m looking for help to see if there’s a similar grammar that can achieve <<[data.occupation%]>> in the word, as ‘occupation’ is a dynamic node that could be 'occupation1' or 'occupation2', necessitating the use of "%".

If there is indeed no simple way, I will have to modify the JSON. harcode “occupation1” to “occupation”;

json:

{
    "form": {
        "stageEdu": {
            "data": {
                "formId": "118b3767-9c3b-4b1e-970b-bf5b940d4f7d",
                "saveType": "submit",
                "userInput": {
                    "other": "super man",
                    "industry": [
                        "K. Other Occupations"
                    ],
                    "occupation12": [
                        "C. Other Occupation"
                    ],
                    "educationLevel": [
                        "C. Tertiary or Above"
                    ]
                }
            },
            "token": "6dc57586-50fc-4ac6-a86c-eb71192e826c"
        }
    }
}

@ivan.lyagin It seems that I can refer to this method:Can i add customerizing formatter to the linq report engine?

@zhenglifan

Internally, JsonDataSource is mapped to com.aspose.words.net.System.Data.DataSet and related classes. This fact can be used to fulfill the requirement. Please consider the following code:

JsonDataSource dataSource = new JsonDataSource(new ByteArrayInputStream(json.getBytes()));

DocumentBuilder builder = new DocumentBuilder();
builder.write("<<[Util.getOccupation(ds)]>>");

ReportingEngine engine = new ReportingEngine();
engine.getKnownTypes().add(Util.class);
engine.buildReport(builder.getDocument(), dataSource, "ds");

System.out.println(builder.getDocument().getText().trim());

//----------------------------------------------

public class Util
{
    public static Object getOccupation(DataRow row)
    {
        DataRow childRow = getFirstChildRow(row, "stageEdu");
        childRow = getFirstChildRow(childRow, "data");
        childRow = getFirstChildRow(childRow, "userInput");
        childRow = getFirstChildRow(childRow, "occupation");

        return childRow.get(childRow.getTable().getTableName() + "_Text");
    }

    private static DataRow getFirstChildRow(DataRow row, String partialName)
    {
        for (DataRelation relation : row.getTable().getChildRelations())
        {
            if (relation.getChildTableName().contains(partialName))
            {
                DataRow[] childRows = row.getChildRows(relation);
                return (childRows.length == 0) ? null : childRows[0];
            }
        }

        return null;
    }
}

Here, json stands for a string with the JSON data provided in your comment.

1 Like

@ivan.lyagin ok now, thanks for your support!

1 Like