I want to encode json/xml payload based on other property/json file.
master file:
{
"keys": {
"Name": "abcd",
"age": "trst",
"USA": "bcd",
"country": "wert"
}
}
Source payload:
{
"Name": "John",
"age": 23,
"Address": {
"state": "Texas",
"country": "USA"
}
}
expected encoded payload:
{
"abcd": "John",
"trst": 23,
"Address": {
"state": "Texas",
"wert": "bcd"
}
}
NOTE:
this Source payload can be a xml file if needed. ( if that can provide fast solution than json. in that case expected encoded payload also can be a xml)
I have couple of ideas,
keep master file in a map and traverse through json object / xml file reading each key and value. while traversing read from map and replace
consider source payload as string and do string replace using regex. (create dynamic regex using master file like ("Name"|"age"|"USA"|"country") and parse and replace that
Objective is find most accurate and performance wise good solution. appreciate if you can share your ideas and small sample if possible. OR is there any library where we can do this type of things?
Related
I have a Json file or string for example:
{
"my-key0": "ke0",
"key-Arr": [
{
"nested-key1": {
"value": "val",
"seqno": 12
},
"nested2": 1
},
{
"dns-sss-qqq": [
{
"some": "aaaaa"
}
]
}
],
"recsize": 459,
"my-obj": {
"my-key1": {
"my-key2": "key2"
}
}
}
My purpose is to replace "-" char to "_" char only in keys in Scala/Java.
In first I thought it can be done with REGEX but the keys can be UNQUOTED and it also can effect on values.
What is most efficient way to it?(Performance is matter)
I have to process GBs of such records.
Thank you
Try jsoniter-scala - it supports kebab-case since v0.17.0 and also it is more efficient in parsing and serialization than jackson-module-scala.
Here are latest results of benchmarks which compare parsing & serialization performance of jsoniter-scala vs. jackson-module-scala, circe and play-json libraries using JDK 8.
Also it has ability to parse streaming JSON values and JSON arrays from java.io.InputStream w/o need of holding all parsed values in the memory.
Extraction of some selected fields or substructures instead of parsing whole message or document is where jsoniter-scala shines.
So try just use it instead of conversion of all your data.
I have a json file and have used simple.json jar to parse the elements. I could parse the elements successfully. But what I want is that if my json file has three elements by same name, then I want to print each name only when their index is called.
get() prints out all the elements of that name.
Please help!
Following is the json file:
{
"nodes":
[
{
"node":"1",
"ipaddr":"127.0.0.1",
"port":"8443",
"mgport":"9000"
},
{
"node":"2",
"ipaddr":"127.0.0.1",
"port":"8556",
"mgport":"9000"
},
{
"node":"3",
"ipaddr":"127.0.0.1",
"port":"8000",
"mgport":"9000"
}
]
}
I need to retrive only one port value rather than all the values.
I have successfully used the JSON Processing library to do something similar to what you describe.
Here is the link for you to have a look: https://jsonp.java.net/
Could you post also an example of the JSON you'll like to parse?
Use Jackson for parsing JSON. It will make your life easier.
This will help you.
http://www.tutorialspoint.com/jackson/jackson_first_application.htm
Thanks
I need to load a text file of information into Java. The Text file looks like this
"reproduce": {
"VB": 7
},
"drill": {
"VB": 8,
"NN": 16
},
"subgross": {
"JJ": 2
},
"campsites": {
"NNS-HL": 1,
"NNS": 1
},
"streamed": {
"VBN": 1,
"VBD": 2
}
It is basically a huge collection of words with some tags included. I need to save this information in some sort of Java data-structure so that the program can search and retrieve tag statistics for a given word.
From what I've read, using a type of HashMap would be the best idea? Something like:
Map<KeyType, List<ValueType>>
Is that a good idea? How would I go about scanning this data from the text file? I could probably find a way to print the dictionary to the text file that would be easier to scan into Java.
While your input does not look exactly like JSON, you might be able to preprocess[1] it in a simple way to make it valid JSON. Because JSON is probably much more widespread and therefore better supported than your custom format.
If your problem then is JSON deserialization, then take a look at Jackson or Gson, which will convert your input string into objects.
Simple example in Jackson:
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
Map<String,Object> data = mapper.readValue(new File("file.json"), Map.class);
// process data further here ...
Both Jackson and Gson have a lot of options and can handle complex inputs in various ways, e.g. they can serialize and deserialize from and to Maps, custom Objects, can handle polymorphism (mapping different inputs to objects of different classes) and more.
Given the input, that is currently in your question, you can simply prepend and append a curly bracket, and you would have valid JSON:
{
"reproduce": {
"VB": 7
},
"drill": {
"VB": 8,
"NN": 16
},
"subgross": {
"JJ": 2
},
"campsites": {
"NNS-HL": 1,
"NNS": 1
},
"streamed": {
"VBN": 1,
"VBD": 2
}
}
Using JSR-353 (https://jsonp.java.net/index.html)
I would like to open a json file and append some object in the root array, eg :
[{"foo":"bar"}]
I would like with a code about like this :
try(JsonGenerator writer = Json.createGenerator(new FileOutputStream(this.file))){
writer.writeStartObject().write("hello", "world").writeEnd();
} catch (IOException e) {
e.printStackTrace();
}
And obtain in the end :
[
{"foo":"bar"},
{"hello":"world"}
]
Note : I don't want to have to load the full json in-memory to append my data.
Note : I don't want to have to load the full json in-memory to append
my data.
Basically, you can't. You would have to parse the full data structure, so that your write(..) would know where to write. Otherwise, it's just appending somewhere and that might break the JSON format.
So read the JSON from the file, generate a JsonArray from it. Create a new JsonObject from your values. Add it to the array. Then write the full array.
You can't simply "append". In the general case you must read in the JSON, modify the tree-structured memory image, then "serialize" it back to linear JSON.
In very simple cases such as the above you could in theory seek to the end, backspace over the closing ], then write out ,, the second object, and a new closing ], but it's not a general solution to updating JSON.
I have really big JSON file for parsing and managing. My JSON file contains structure like this
[
{"id": "11040548","key1":"keyValue1","key2":"keyValue2","key3":"keyValue3","key4":"keyValue4","key5":"keyValue5","key6":"keyValue6","key7":"keyValue7","key8":"keyValue8","key9":"keyValue9","key10":"keyValue10","key11":"keyValue11","key12":"keyValue12","key13":"keyValue13","key14":"keyValue14","key15":"keyValue15"
},
{"id": "11040549","key1":"keyValue1","key2":"keyValue2","key3":"keyValue3","key4":"keyValue4","key5":"keyValue5","key6":"keyValue6","key7":"keyValue7","key8":"keyValue8","key9":"keyValue9","key10":"keyValue10","key11":"keyValue11","key12":"keyValue12","key13":"keyValue13","key14":"keyValue14","key15":"keyValue15"
},
....
{"id": "11040548","key1":"keyValue1","key2":"keyValue2","key3":"keyValue3","key4":"keyValue4","key5":"keyValue5","key6":"keyValue6","key7":"keyValue7","key8":"keyValue8","key9":"keyValue9","key10":"keyValue10","key11":"keyValue11","key12":"keyValue12","key13":"keyValue13","key14":"keyValue14","key15":"keyValue15"
}
]
My JSON file contains data about topics from news website and practically every day this JSON file will be increased dramatically.
For parsing of that file I use
URL urlLinkSource = new URL(OUTBOX_URL);
urlLinkSourceReader = new BufferedReader(new InputStreamReader(
urlLinkSource.openStream(), "UTF-8"));
ObjectMapper mapper = new ObjectMapper();
List<DataContainerList> DataContainerListData = mapper.readValue(urlLinkSourceReader,new TypeReference<List<DataContainerList>>() { }); //DataContainerList contains id, key1, key2, key3..key15
My problem is that I want to load in this line
List<DataContainerList> DataContainerListData = mapper.readValue(urlLinkSourceReader,new TypeReference<List<DataContainerList>>() { });
only range of JSON object - just first ten object, just second ten object - because I need to display in my app just 10 news in paging mode (all the time I know the index of which 10 I need to display). It totally stuped to load 10 000 objects and to iterate just first 10 of them. So my question is how I can load
in similar way like this one:
List<DataContainerList> DataContainerListData = mapper.readValue(urlLinkSourceReader,new TypeReference<List<DataContainerList>>() { });
only objects with indexes FROM -TO (for example from 30 to 40) without loading of all objects in the entire JSON file?
Regards
It depends of what you mean by "load object with indexes from to", if you want to
Read everything but bind only a sublist
The solution in that case is to read the full stream and only bind values within those indexes.
You can use jacksons streaming api and do it yourself. Parse the stream use a counter to keep track of actual index and then bind to POJOs only what you need.
However this is not a good solution if your file is large and its done in real time.
Read only the data between those indexes
You should do that if your file is big and performance matters. Instead of having a single big file, do the pagination by splitting your json array into multiple files matching your ranges, and then just deserialize the specific file content into your array.
Hope this helps...