Is anyone aware of a JSON-XPath style library that allows data manipulation; update, delete, create, etc...
JsonPath.write(json, "$.store.book[*].author", value);
I've looked into the following, but none allow altering the content.
JPath
JSONQuery
JSONiJ
JsonPath (im using 2.2.0) now allows the manipulation of JSON data. e.g.
String jsonData = "{\"drink\":\"juice\"}";
JsonPath.parse(jsonData).set("$.drink", "beer").jsonString();
results in {"drink":"beer"}
JSON was not meant to be a database.
If you want to store your data in the JSON format;
Read the JSON records into your Java application and create data objects.
Modify the data objects in the Java application.
When the application closes, write the JSON records back out.
You'd be better off using an actual database, relational or NoSQL, to store your data, and write JSON records when they're needed.
You might want to have a look at this library I developped to be able to use XML libraries to manipulate JSON: https://github.com/bhabegger/json-n-xml/
It parses JSON to a DOM structure which you can manipulate with standard XML tools and then allows you to serialize back to json.
(JSON may not be meant to be a database but you do have occasions where you just want simple modifications.)
Hope it helps.
Related
I have the following csv file (In production the number of records can range from 20k-100k and has many fields )
id,firstname,lastname,email,profession
100,Betta,Wandie,Betta.Wandie#gmail.com,developer
101,Janey,Firmin,Janey.Firmin#gmail.com,doctor
I need to convert this to json and do further processing.
CSV->JSON->PROCESS FURTHER
.I am able to convert it to JSON directly using the code given here
directly convert CSV file to JSON file using the Jackson library
But i want do validations for json like if lastname has null value then ignore that record or id is missing then ignore that record.
How can i handle the validation?I am using Java 8 and spring boot latest version
I have done something similar by using JavaScript (Nashorn). Yes, that is nasty, but it works, and it is astonishingly fast!
Unfortunately, I do not have the source code at hand …
Why I did it that way had the same reasons as #chrylis-on strike implies in their comment: the validation is much easier if you have an object for each JSON record. But as I was lazy, and there was definitely no need for the Java object I had to create for this, I had the idea with the JavaScript script inside my Java program.
Basically, a JSON String is the source for a JavaScript program; you can assign it directly to a JavaScript variable and then you can access the records as array elements and the fields by their name. So your JavaScript code walks through the records and drops those that do not match your validation rules.
Now the Java part: the keyword here is JSR223; it is an API that allows you to execute scripts inside your Java environment. In your case, you have to provide the converted JSON in the context, then you start the script that writes the modified JSON back to the context.
If the converted JSON is too large, you can even use the same technique to check record by record; if you compile the script, it is nearly as fast as native Java.
You can even omit Jackson and let JavaScript do the conversion …
Sorry that I cannot provide code samples; I will try to get hold on them and add them if I get them.
I am developing an Android app. For this app I need to store some kind of data and access that data.
What is the best way I can store data in the Android app?
After saving somehow in Android, when a user inserts a value from the UI, the app should query the data set and check whether that value is present or not.
Please tell me how to do this in Android.
You don't have to deal with the JSON Objects directly (althought you can if you want to). Have a look at the Gson library. It serializes and deserializes JSON data into objects and makes your life much easier!
Additionally, since JSON Objects can be easily represented by strings, you can use Shared Preferences to store them in a key-value approach. If you end up using Gson, you can save your objects too using its toJson() and fromJson() methods.
Note: If you do use SharedPreferences, remember that the user can manually delete the file via the device app manager.
You can get easier the JSON data using Retrofit that convert the JSON to a POJO and you can save the object in SQLite, I recommend you use ORM like SugarORM.
I work with MongoDb and Jersey in Java. I like to return the Documents I read out of the DB directly as JSON to my AngularJS client. If I call toJson() on a Document I get this BSON representation. That means that a Long for example is represented as an object with a key-value-pair "$numberLong". I don't like this type information in my JSON.
Is there any way to get a simple JSON out of this Document, without converting it to a Java-Object and back? What are the best practices to work with this Documents? Would you include that type information?
I want to create an object from a Ruby program and store it in an SQL database as a blob. I then want to be able to read this blob directly into a Java program at a later date. I understand that these blobs will probably be incompatible. How can I go about making a Java-readable blob from Ruby?
You can use json (xml, yaml) format to store object as string. And parse it in java.
Serialize ruby object to YAML (or JSON, XML etc)
Save it in the DB
Deserialize it from YAML to Java object
You should use thrift, it is much faster than (de)serializing json. See the benchmark here
JSON stands for JavaScript Object Notation. But how come languages like php, java, c etc can also communication each other with json.
What I want to know is that, am i correct to say that json is not limited to js only, but served as a protocol for applications to communicate with each other over the network, which is the same purpose as XML?
JSON cannot handle complex data hierarchies like XML can (attributes, namespaces, etc.), but on the other hand you don't get the same overhead with JSON as you get with XML (if you don't need the complex data structures).
Since JSON is plain text with a special notation for JS to interpret, it's an easy protocol to adopt in other languages.
It is easy for a JS script to parse JSON, since it can be done using 'eval' in which the JS enginge can use its full power.
On the other hand, it is more complicated to generate JSON from within JS. Usually one uses the JSON package from www.json.org in which an object can easily be serialised using JSON.stringify, but it is implemented in JS so its not running with optimal performance.
So serialising JSON is about the same complexity using JS as when using Java, PHP or any other server side language.
Therefore, in my opinion, JSON is best suited when there is asymmetry between produce/consumer e.g. a web server that generates a lot of data that is consumed by the web application. Not the other way around.
But! When one choses JSON as data format it should be used in both directions, not XML<>JSON. Except for when simple get requests are used to retrieve JSON data.
yes, JSON is also wildly used as a data exchange protocol much like XML.
Typically a program (not written in JavaScript) needs a JSON library to parse and create JSON objects (although you can probably create them even without one).
Your right - it's a light weight data interchange format -- more details at: http://www.json.org
You are completely correct. JSON definition of how data should be formatted. It is more light weight than XML and therefore well suited to things like AJAX where you want to send data back and forth to the server quickly.