GuntherKushner
GuntherKushner

Reputation: 142

How to Convert Edn string to Json

I have to retrieve data from some site that sends back responses with edn bodies. I am trying to convert the sent back Edn to Json so I can parse it with Jsoup.

I found a website that was able to do the conversion, but how do I implement something like this in java?

I tried something like this, but it didn't a full job:

public static String edmToJson(String edm) {
    String json = edm;
    json = json.replaceFirst("(\\(\\{).*?(}\\))", "1").replace("(", "").replace("})", "").replace("} {", "},{");
    return json;
}

Is there a way to do it without using closure?

Upvotes: 4

Views: 606

Answers (1)

Smile
Smile

Reputation: 4088

You can parse EDN data in java by using a library like edn-java.

Sample usage:

@Test
public void simpleUsageExample() throws IOException {
    Parseable pbr = Parsers.newParseable("{:x 1, :y 2}");
    Parser p = Parsers.newParser(defaultConfiguration());
    Map<?, ?> m = (Map<?, ?>) p.nextValue(pbr);
    assertEquals(m.get(newKeyword("x")), 1L);
    assertEquals(m.get(newKeyword("y")), 2L);
    assertEquals(Parser.END_OF_INPUT, p.nextValue(pbr));
}

Complete docs available at edn-java

Upvotes: 1

Related Questions