I've never used a regex before and I am looking to split up a file that has one or more JSON objects, the JSON objects are not separated by a comma. So I need to split them between "}{" and keep both curly braces. This is what the string looks like:
{id:"123",name:"myName"}{id:"456",name:"anotherName"}
I would like a string array like using string.split()
["{id:"123",name:"myName"}", "{"id:"456",name:"anotherName"}"]
split(String regex) method splits this string around matches of the given regular expression. This method works in the same way as invoking the method i.e split(String regex, int limit) with the given expression and a limit argument of zero. Therefore, trailing empty strings are not included in the resulting array.
The JSONata type system is based on the JSON type system, with the addition of the function type. In order to accommodate the regex as a standalone expression, the syntax /regex/ evaluates to a function. Think of the /regex/ syntax as a higher-order function that results in a 'matching function' when evaluated.
Using BigTextFileSplitter, you can split large JSON file in Windows easily and fast, just a few mouse clicks!
JSON has the following syntax. Objects are enclosed in braces ( {} ), their name-value pairs are separated by a comma ( , ), and the name and value in a pair are separated by a colon ( : ). Names in an object are strings, whereas values may be of any of the seven value types, including another object or an array.
If your objects aren't more complex than what you show, you may use lookarounds like this :
String[] strs = str.split("(?<=\\})(?=\\{)");
Exemple :
String str = "{id:\"123\",name:\"myName\"}{id:\"456\",name:\"yetanotherName\"}{id:\"456\",name:\"anotherName\"}";
String[] strs = str.split("(?<=\\})(?=\\{)");
for (String s : strs) {
System.out.println(s);
}
prints
{id:"123",name:"myName"}
{id:"456",name:"anotherName"}
{id:"456",name:"yetanotherName"}
If your objects are more complex, a regex wouldn't probably work and you would have to parse your string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With