Convert one json format to another in java - java

I am looking for a utility which converts one json format to another by respecting at the conversion definitions from a preferably xml file. Is there any library doing something like this in java ?
For example source json is:
{"name":"aa","surname":"bb","accounts":[{"accountid":10,"balance":100}]}
target json is :
{"owner":"aa-bb","accounts":[{"accountid":10,"balance":100}]}
sample config xml :
t.owner = s.name.concat("-").concat(surname)
t.accounts = t.accounts
Ps:Please dont post solutions for this example, it is just for giving an idea, there will be quite different scenarios in mapping.

Is this what u need?
Open input file.
Read / parse JSON from file using a JSON library.
Convert in-memory data structure to new structure.
Open output file
Unparse in-memory data structure to file using JSON library.

Related

How do you import a specific element from an external JSON file to an excel file?

I have a long JSON file and i want to copy a specific element from it(i know its name) to an excel file.
eg :: Suppose i want to make an excel file having "Product" (Baleno, i20, Ford Figo etc) imported from a JSON file, how to do it using GET POST or without AJAX.
So, obviously there are ways to write this yourself. What I recommend, however, is using a library (or two. I'd recommend JSON Simple and/or Apache POI) Software engineering is about efficiency, and that includes for the engineer. Using libraries is not shameful. I'd recommend doing that first. Try out using librarys, okay?
-Batista
One simple method I have used, when you only require the content you have in the JSON and if the output needs no formatting!
Create/Construct/Return a CSV File containing the content.
Product,Q1Sales,Q2Sales,Q3SalesQ4Sales
"Baleno",6000,5000,7000,5500
Return the Mimetype Filename as "BalenoSales.xls"
Make the Suffix of the Servlet URL ".xls" as well so Excel/IE likes it.

Easy way to convert complex json to simple POJO files for Android

I'm trying to convert complex json response to simple POJO objects. What I mean, I can convert this json response to pojo with www.jsonschema2pojo.org , but it generates me 47 files. I don't want this solution.
I want simple and light POJO objects. So, what I need, a few pojo files (May be max 5-6 files) with entire json attributes.
Example
My sample json response ( Wordpress ):
https://pastebin.ca/3855759
And the generator creates these files:
see image
I read these and similar posts but I could't fix my issue;
stackoverflow.com/questions/41692991/convert-nested-json-to-simple-json
stackoverflow.com/questions/31117784/convert-complex-json-to-one-level-pojo-using-jackson
So, do you have any solution to accomplish this?
try GSOnFormat plugin from in android studio plugins.
you can install it from file >Settings>plugins>browse repository>GSONFORMAT
use it using alt+insert

How can I efficiently read multiple json files into a Dataframe or JavaRDD?

I can use the following code to read a single json file but I need to read multiple json files and merge them into one Dataframe. How can I do this?
DataFrame jsondf = sqlContext.read().json("/home/spark/articles/article.json");
Or is there a way to read multiple json files into JavaRDD then convert to Dataframe?
To read multiple inputs in Spark, use wildcards. That's going to be true whether you're constructing a dataframe or an rdd.
context.read().json("/home/spark/articles/*.json")
// or getting json out of s3
context.read().json("s3n://bucket/articles/201510*/*.json")
You can use exactly the same code to read multiple JSON files. Just pass a path-to-a-directory / path-with-wildcards instead of path to a single file.
DataFrameReader also provides json method with a following signature:
json(jsonRDD: JavaRDD[String])
which can be used to parse JSON already loaded into JavaRDD.
function spark.read.json accepts list of file as a parameter.
spark.read.json(List_all_json file)
This will read all the files in the list and return a single data frame for all the information in the files.
Using pyspark, if you have all the json files in the same folder, you can use df = spark.read.json('folder_path'). This instruction will load all the json files inside the folder.
For reading performance, I recommend you for providing dataframe the schema:
import pyspark.sql.types as T
billing_schema = billing_schema = T.StructType([
T.StructField('accountId', T.LongType(),True),
T.StructField('accountName',T.StringType(),True),
T.StructField('accountOwnerEmail',T.StringType(),True),
T.StructField('additionalInfo',T.StringType(),True),
T.StructField('chargesBilledSeparately',T.BooleanType(),True),
T.StructField('consumedQuantity',T.DoubleType(),True),
T.StructField('consumedService',T.StringType(),True),
T.StructField('consumedServiceId',T.LongType(),True),
T.StructField('cost',T.DoubleType(),True),
T.StructField('costCenter',T.StringType(),True),
T.StructField('date',T.StringType(),True),
T.StructField('departmentId',T.LongType(),True),
T.StructField('departmentName',T.StringType(),True),
T.StructField('instanceId',T.StringType(),True),
T.StructField('location',T.StringType(),True),
T.StructField('meterCategory',T.StringType(),True),
T.StructField('meterId',T.StringType(),True),
T.StructField('meterName',T.StringType(),True),
T.StructField('meterRegion',T.StringType(),True),
T.StructField('meterSubCategory',T.StringType(),True),
T.StructField('offerId',T.StringType(),True),
T.StructField('partNumber',T.StringType(),True),
T.StructField('product',T.StringType(),True),
T.StructField('productId',T.LongType(),True),
T.StructField('resourceGroup',T.StringType(),True),
T.StructField('resourceGuid',T.StringType(),True),
T.StructField('resourceLocation',T.StringType(),True),
T.StructField('resourceLocationId',T.LongType(),True),
T.StructField('resourceRate',T.DoubleType(),True),
T.StructField('serviceAdministratorId',T.StringType(),True),
T.StructField('serviceInfo1',T.StringType(),True),
T.StructField('serviceInfo2',T.StringType(),True),
T.StructField('serviceName',T.StringType(),True),
T.StructField('serviceTier',T.StringType(),True),
T.StructField('storeServiceIdentifier',T.StringType(),True),
T.StructField('subscriptionGuid',T.StringType(),True),
T.StructField('subscriptionId',T.LongType(),True),
T.StructField('subscriptionName',T.StringType(),True),
T.StructField('tags',T.StringType(),True),
T.StructField('unitOfMeasure',T.StringType(),True)
])
billing_df = spark.read.json('/mnt/billingsources/raw-files/202106/', schema=billing_schema)
Function json(String... paths) takes variable arguments. (documentation)
So you can change your code like this:
sqlContext.read().json(file1, file2, ...)

Object Sereialization, JAVA, Javascript

I have n object whose properties are being sent to a front end using REST protocols. There the object is taken in as an XML file and then parsed to JSON using JSON.parser. Now my target is to save this JSON file for some specified time on the disk. I tried serializing the object and storing it but it gets stored in binary/hex format. I need it to be in xml or JSON format.
Can anybody help me with this ?
Front-end is in JavaScript and the back-end is in Java.
Why you need to save JSON file on client side disk, it is not recommended practice. Rather you should use HTML5 web storage.
are you using JSON.simple? if so, there are several examples on their page for converting a string to json and back. in this case you already have a deserialized object so you would just need to serialize it to a string see https://code.google.com/p/json-simple/wiki/DecodingExamples
if you have your json object as a map you can
String jsonString = JSONValue.toJSONString(json);
or if it is already a JSONObject then simply
String jsonString = json.toJSONString();
then write the jsonString to your .json file.
FileWriter file = new FileWriter("/path/to/file.json");
file.write(jsonString);
file.flush();
file.close();
apologies if that is not the library you are using.

parse XML to POJO using XStream

I have an xml file which I want to read and parse it into POJO.
I am using XStream for this.
I am not able to send the file as an input to the code for parsing(file is on my local drive).
How to read the xml file and parse it using fromXML() method ?
I would be gratefull if someone can give example for sending xml file as input and parsing it to POJO and printing it on the screen
Thanks...
Based on the provided comments, I can better understand the problem you are facing. I believe the answer you are looking for is implicit collections. Check out the XStream tutorial on aliasing: http://x-stream.github.io/alias-tutorial.html .

Categories