Append double quotes in nested json string - Java - java

I have a nested json string like below for which I need to append double quotes for each key and value
{messageFilters:[{filterCriteria:[{paramNameAndVals:{},values:[abc[0-9]],context:Request,name:Destination-Host,operator:abc}],filterActions:[{paramNameAndVals:{Set
Name:sdf,Value To
Hash:as},context:Context,name:Stick-To-Pool}],name:MF}],name:fg1,subGroups:[],advancedView:false}
I want below output like ,
{"messageFilters":[{"filterCriteria":[{"paramNameAndVals":{},"values":["abc[0-9]"],"context":"Request","name":"Destination-Host","operator":"abc"}],"filterActions":[{"paramNameAndVals":{"Set
Name":"sdf","Value To
Hash":"as"},"context":"Context","name":"Stick-To-Pool"}],"name":"MF"}],"name":"fg1","subGroups":[],"advancedView":false}
I tried various regex patterns, but evrerything in vain. Could anyone please help

the first example of your question is not a valid json, try convert your not valid json to string.
kindly chechk this link for more information
How to convert any Object to String?

Related

How to convert String to Json Object in java or groovy

I saved a json into the database as a string like this:
"[_district:_1_2_5village, _name:_1_1_2id_inter, _gender:_1_3_5sex]"
Now i want to convert it back to a Json Object so as to pick the key and value eg _district is the key and _1_2_5village is the value. Any help on how i can achieve this. Thanks
I tried to convert the string back into JSON by parsing but that dint work for me.
It doesn't work because that's not a JSON format, a JSON is a way of mapping objects and uses key value syntax like so:
{"key": "value"}
and an array would look like this:
[{"key": "value"},{"key": "value"}]
You'll need to make a custom parser for your syntax
Here's the json specification:
https://www.json.org/json-en.html

Take non-nested JSON String and Build Json Nested String

This is very new to me. I am reading data from a cassandra table. This data is being extracted via a "select json * ..." query but here's the thing. The format of that json is
{"acct_ref_nb": 1401040701, "txn_pst_dt": "2020-02-26", "txn_pst_tm": 1934131, "txn_am": 15000.0 ....
Every field is in quotation marks, followed by a colon, followed by the value, then a comma and the next field, so on and so forth.
We need to reformat this and have a nested structure. We also need to change the names of the fields. So you would have something like...
"{
"ccEvent": {
"account": {
"accountReferenceNumber": 1401040701,
"transactionPostDate": "2020-02-26",
"transactionPostTime": 1934131,
"transactionAmount": 15000.0,
........
Is there a preferred library to do this? I'm literally lost even at a high level on how to do this. Thanks.

Java XML Parsing into object

We have a requirement of parsing xml data in java.
The xml data looks like this:
xml_data
1> The first question here is how to parse such data.I have tried the code suggested in this link :
xmlparse with Node
and have partial success. The only doubt here is how can i can to reach "" tag.
2>The second question is if the data is parsed how can we can store the values in single object,so that we can get length and values of "CRAWL", "Test", "Check"(Tables) separately(from single object)?
Please help me as how can we achieve this?
Abhi

Mongodb- java function com.mongodb.util.JSON.parse()

I am now using java method com.mongodb.util.JSON.parse(param) to convert my json string to a DBObject.I find that, in most cases , for example , when the key in the json string is simple type like string or int ,it can be used by both JSON.parse() method and terminal.
For example:
json string:{'times':8}
Both JSON.parse({'times':8}) and db.collection.find({'times':8}) in terminal can work correctly.
But when I do a query on ISODate or _id,things become different:
json string 1 :{'createDate':ISODate('2013-10-21T06:39:16.692Z')}
json strign 2: {'createDate':{'$date':'2013-10-21T06:39:16.692Z'}}
json string 1 can work correctly in terminal but cannot be parsed by JSON.parse() method.
On the contrary , json string 2 can be parsed by JSON.parse() method but cannot be used in terminal.
The same thing happened in _id.
json string 3:{'_id':ObjectId('1231daf213432414321431')}
json string 4:{'_id':{'$oid':'fadf234234sdfadfasdfa12'}}
json string 3 can work correctly in terminal but cannot be parsed by JSON.parse() method.
On the contrary , json string 4 can be parsed by JSON.parse() method but cannot be used in terminal.
I don't know the reason of these differences.
The constructors you are using in the Mongo Shell are JavaScript code that is obviously not part of strict JSON that you want to parse in your Java application. The point to be criticized here is rather that the shell does not support something like '$date' (as I have just tested myself as well).
This is for example discussed here.

Converting from XMLSchema datatype to Java int

I am working on a project that involves fetching data from Dbpedia and I was wondering whether there is anyway to convert the the object returned from a dbpedia query i.e a XMLSchema#double into a java int so that I can perform operations on it and modify the data for my use. I am using jena to fetch the data from the sparql endpoints jena provide. I tried using the toString method to change the RDFnode into a string and than converting into an int/double, but that doesnt seem to work and gives me the exception that is listed below:
Exception in thread "main" java.lang.NumberFormatException: For input string: "147181000000^^http://www.w3.org/2001/XMLSchema#double"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
Does anyone here have a work around this problem??
After Converting to String it look like:
"147181000000^^http://www.w3.org/2001/XMLSchema#double"
And you want to convert into int/double for that you have to separate only first number from this string
Number= 147181000000 .
You can use indexOf or split method to get Number from the String.
Sample Code:
String number = "147181000000^^http://www.w3.org/2001/XMLSchema#double";
number = number.subString(0,nuber.indexOf(^));
int num = Integer.ParseInt(number);

Categories