I have a text file which consists of json data and I do not need some of the data fields. How do I remove these strings and bring them to another form?
Current Data:
[{"EMPTYPE":"21","EMPSUM":"1","VERSION":2.2,"UID":212121,"EMPID":454354,"EMPNAME":"abc","EMPGPRID":123,"AID":121,"DATE":"2015-07-07","EMPGPRID":21}]
Required Data:
[{"EMPTYPE":"21","EMPID":454354,"EMPNAME":"abc","EMPGPRID":123}]
Parser's or delimeters
"(?!EMP(?:TYPE|ID|NAME|GPRID)")[^"]*":.*?(?:,|(?=}))
Try this.Replace by empty string.See demo.
https://regex101.com/r/hR7tH4/18
You should look into Jackson for the json parsing (it will take the json string and make more like a regular Java object). It also has a nice remove method you can use (if you know what you don't want) or you can simply make a new object with just the info you like. It's then easy to serialize it back to json.
Related
How to escape double quotes inside a JSON given the data in the JSON will be obtained dynamically in Java.
Example:
{
"key": "I ask silly questions on "https://www.stackoverflow.com". "
}
In this case, the value in the JSON is populated from dynamically let's say from a user input.
I have tried StringEscapeUtils provided by apache-commons-lang (https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html), but this escapes the entire JSON where as the double quotes inside the JSON needs to escape again.
Possible solution will be to use regex and filter out the value in JSON and escape once before escaping the overall JSON.
But, is there any JAR to escape the inner contents of a JSON?
If doing json processing in java I would use Jackson databind and let it handle json generation. You will probably want to turn some knobs eventually, but simplest possible usage will probably do for this use case:
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(singletonMap("key", "I ask silly questions..."));
My JSON contains:
{"state":1,"employee":{"user":"raparra","pass":"1234","work":"taxy_driver"}}
I want to extract the pass (1234). Is there any function to make it easy and fast? Thanks!
Use JSON parser with JsonPath support - it will find and extract only required data that are defined by a query.
I'm currently writing a Java program which involves goals. It's basically a to-do list. Each goal has a few strings, such as name, description etc. I can save and load these goals to a file. My issue was separating the strings - I couldn't think of a character that couldn't be in the string itself. I ended up prefixing each string with it's length and then a colon.
I'm sure there is something in the Java API that will handle this, like ObjectOutputStream. I'm curious about the 'general case', though. This must be an issue for any program that saves and loads strings from a file without being able to assume anything about the string. Is there a better way to go about this?
There are couple of ways to handle your case, e.g:
Encoding your String with something like base64
Applying a well defined format, e.g. JSON or CSV
There are tons of tools support you including:
Apache Commons codec for base64 encoding/decoding
Jaskson for JSON serializing/deserializing
opencsv for csv serializing/deserializing
I have a XML file which was encoded using java.beans.XMLEncoder. I cannot use java.beans.XMLDecoder to decode it, as the class of encoded object is not present in my project. Is there a way to obtain values in xml without using java.beans.XMLDecoder, xmlDecoder.readObject() method ?
There are several APIs for parsing XML in Java which don't require you to deserialize an object. Once you have a DOM, XPath is a useful way to query its contents. You will need to know what you're looking for, though.
I need to add a URL typically in the format http:\somewebsite.com\somepage.asp.
When I create a string with the above URL and add it to JSON object json
using
json.put("url",urlstring);
it's appending an extra "\" and when I check the output it's like http:\\\\somewebsite.com\\somepage.asp
When I give the URL as http://somewebsite.com/somepage.asp
the json output is http:\/\/somewebsite.com\/somepage.asp
Can you help me to retrieve the URL as it is, please?
Thanks
Your JSON library automatically escapes characters like slashes. On the receiving end, you'll have to remove those backslashes by using a function like replace().
Here's an example:
string receivedUrlString = "http:\/\/somewebsite.com\/somepage.asp";<br />
string cleanedUrlString = receivedUrlString.replace('\', '');
cleanedUrlString should be "http://somewebsite.com/somepage.asp".
Hope this helps.
Reference: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replace(char,%20char)
Tichodroma's answer has nailed it. You can solve the "problem" by storing valid URLs.
In addition, the JSON format requires that backslashes in strings are escaped with a second backslash. If the 2nd backslash is left out, the result is invalid JSON. Refer to the JSON syntax diagrams at http://www.json.org
The fact that the double backslashes are giving you problems actually means that the software that is reading the files is broken. A properly written JSON parser will automatically de-escape the strings. The site I linked to above lists many JSON parser libraries written in many languages. You should use one of these rather than trying to write the JSON parsing code yourself.