综合开发

Rjson: Convert `list` to JSON string compatible with Cypher DSL

微信扫一扫,分享到朋友圈

Rjson: Convert `list` to JSON string compatible with Cypher DSL

Let’s have:

desired_output="{a:'1', b:'foo'}"
D = list(a=1, b="foo")

Then:

out = toJSON(D)
out
"{"a":1,"b":"foo"}"
identical(out, desired_output) # FALSE

Is there a better function f
(other than gsub
) so that this holds?

identical( f(toJSON(D)), desired_output) == TRUE

Using cat
just prints to the screen:

cat(toJSON(D))
{"a":1,"b":"foo"}

Background:

The desired_output
format of the string is required for dynamically constructing cypher/Neo4j graph database queries using RNeo4j
package for a call such as:

# match node n with properties a=1 and b="foo"
RNeo4j::cypher(graph, query="MATCH (n{a:'1', b:'foo'}) RETURN n")

This works on your example and hopefully more general cases:

gsub("',", "', ",                             # adds spaces after commas
gsub('"', "'",                             # replaces " with '
gsub('"([^"]+)":', "\1:",              # removes " around key names
toJSON(rapply(D, as.character)))))   # puts " around numerics
# [1] "{a:'1', b:'foo'}"

微信扫一扫,分享到朋友圈

Rjson: Convert `list` to JSON string compatible with Cypher DSL

Requiring a folder with index.js

上一篇

Beginner Guide to In-Memory Optimized Tables in SQL Server

下一篇

你也可能喜欢

评论已经被关闭。

插入图片

热门栏目

Rjson: Convert `list` to JSON string compatible with Cypher DSL

长按储存图像,分享给朋友