data tree CHANGE optional direct use of any value
diff --git a/src/parser_json.c b/src/parser_json.c
index 3f426a4..4f09f86 100644
--- a/src/parser_json.c
+++ b/src/parser_json.c
@@ -1100,7 +1100,7 @@
             ret = lydjson_metadata_finish(lydctx, &tree);
             LY_CHECK_RET(ret);
 
-            ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, node);
+            ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node);
             LY_CHECK_RET(ret);
         }
     } else if (ret == LY_ENOT) {
diff --git a/src/parser_lyb.c b/src/parser_lyb.c
index cb7caff..056d70c 100644
--- a/src/parser_lyb.c
+++ b/src/parser_lyb.c
@@ -857,7 +857,7 @@
         }
 
         /* create node */
-        ret = lyd_create_any(snode, value, value_type, &node);
+        ret = lyd_create_any(snode, value, value_type, 1, &node);
         LY_CHECK_GOTO(ret, cleanup);
 
         dynamic = 0;
diff --git a/src/parser_xml.c b/src/parser_xml.c
index 432bb7d..bddd17f 100644
--- a/src/parser_xml.c
+++ b/src/parser_xml.c
@@ -584,7 +584,7 @@
         lydctx->parse_options = prev_opts;
 
         /* create node */
-        ret = lyd_create_any(snode, anchor, LYD_ANYDATA_DATATREE, &node);
+        ret = lyd_create_any(snode, anchor, LYD_ANYDATA_DATATREE, 1, &node);
         LY_CHECK_GOTO(ret, error);
     }
     assert(node);
diff --git a/src/tree_data.c b/src/tree_data.c
index 7cb0e45..870dd44 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -656,9 +656,12 @@
 }
 
 LY_ERR
-lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
+lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value,
+        struct lyd_node **node)
 {
+    LY_ERR ret;
     struct lyd_node_any *any;
+    union lyd_any_value any_val;
 
     assert(schema->nodetype & LYD_NODE_ANY);
 
@@ -670,8 +673,15 @@
     any->flags = LYD_NEW;
 
     /* TODO: convert XML/JSON strings into a opaq data tree */
-    any->value.str = value;
-    any->value_type = value_type;
+
+    if (use_value) {
+        any->value.str = value;
+        any->value_type = value_type;
+    } else {
+        any_val.str = value;
+        ret = lyd_any_copy_value((struct lyd_node *)any, &any_val, value_type);
+        LY_CHECK_ERR_RET(ret, free(any), ret);
+    }
     lyd_hash((struct lyd_node *)any);
 
     *node = (struct lyd_node *)any;
@@ -889,7 +899,7 @@
     schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0);
     LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
 
-    LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret));
+    LY_CHECK_RET(lyd_create_any(schema, value, value_type, 1, &ret));
     if (parent) {
         lyd_insert_node(parent, NULL, ret);
     }
@@ -945,7 +955,7 @@
     case LYS_ANYDATA:
     case LYS_ANYXML:
         /* create a new any node */
-        LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
+        LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
 
         /* compare with the existing one */
         if (lyd_compare_single(node, new_any, 0)) {
@@ -1389,7 +1399,7 @@
             break;
         case LYS_ANYDATA:
         case LYS_ANYXML:
-            LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
+            LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
             break;
         default:
             LOGINT(ctx);
diff --git a/src/tree_data_internal.h b/src/tree_data_internal.h
index 71681c3..35a3839 100644
--- a/src/tree_data_internal.h
+++ b/src/tree_data_internal.h
@@ -162,14 +162,15 @@
  * Hash is calculated and flags are properly set based on @p is_valid.
  *
  * @param[in] schema Schema node of the new data node.
- * @param[in] value Value of the any node, is directly assigned into the data node.
+ * @param[in] value Value of the any node.
  * @param[in] value_type Value type of the value.
+ * @param[in] use_value Whether to directly assign (eat) the value or duplicate it.
  * @param[out] node Created node.
  * @return LY_SUCCESS on success.
  * @return LY_ERR value if an error occurred.
  */
 LY_ERR lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type,
-        struct lyd_node **node);
+        ly_bool use_value, struct lyd_node **node);
 
 /**
  * @brief Create an opaque node.