Search code examples
javajsonparsingtostringnashorn

ScriptObjectMirror member to JSON string


I have ScriptObjectMirror object from jdk.nashorn.api.scripting. I get a JSON node using:

Object clientDataNode = scriptObjectMirror.getMember("clientData");

How to convert clientDataNode to String to obtain sth like this:

  "clientData": {
    "name": "John",
    "surname": "Smith",
    "age": 22
  }

Solution

  • Eventually I just converted clientData node to string property using javascript method

    JSON.stringify(clientData)
    

    with Nashorn engine and then obtain it this way, cause it's no longer a member:

    String jsonClientData = (String) scriptObjectMirror.get("clientData");