JSON Array Question - json

How to use jsp to get json resulting in folowing format ?
{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
"212 732-1234",
"646 123-4567"
]
}

You can use different libraries to convert from diferent java objects to JSON
For example the library json-simple http://code.google.com/p/json-simple/
In the json-simple page you can see some examples of encoding,decoding, and JSP & AJAX with these library.

Structure a Java object in a similar fashion and then use a serialization mechanism
such as json-simple or xstream with Json Driver
for instance
new XStream(new JettisonMappedXmlDriver());
xstream.toXML(object);

Got it.
Create JSONObject.
Create JSONArray.
Merge.
Thanks guy. Appreciate.

Related

How can we structure array of dictionary in firebase realtime database?

I want to create below json structure like in firebase realtime databse
{
"doner": [
{
"firstName": "Sandesh",
"lastName": "Sardar",
"location": [50.11, 8.68],
"mobile": "100",
"age": 21
},
{
"firstName": "Akash",
"lastName": "saw",
"location": [50.85, 4.35],
"mobile": "1200",
"age": 22
},
{
"firstName": "Sahil",
"lastName": "abc",
"location": [48.85, 2.35],
"mobile": "325846",
"age": 23
},
{
"firstName": "ram",
"lastName": "abc",
"location": [46.2039, 6.1400],
"mobile": "3257673",
"age": 34
}]
}
But when I imported file in firebase realtime database, it turns into below structure where ]1
I believe this is not array of dictionary, but it is a dictionary of multiple dictionaries.
Is there any way to structure array of dictionaries in firebase?
The Firebase Realtime Database doesn't natively store arrays in the format you want. It instead stores arrays as key-value pairs, with the key being the (string representation) of the index of each item in the array.
When you read the data from Firebase (either through an SDK, or through the REST API), it converts this map back into an array.
So what you're seeing is the expected behavior, and there's no way to change it.
If you'd like to learn more about how Firebase deals with arrays, and why, I recommend checking out Kato's blog post here: Best Practices: Arrays in Firebase.

Azure Logic Apps - Map Json to Json with Liquid flatten array

Any help would be much appreciated. What I am trying to achieve is to request a record from Dynamics 365(cloud) to an on-premise system (exposed by mulesoft) I have decided to use Azure logic apps to do the integration and to use Liquid to do the mapping, however I am battling to flatten the array with liquid, I'm getting a JSON payload from the on-premise system which I need to transform readily to load into dynamics 365, what I am getting is something like the following:
{
"person": {
"firstname": " Fred",
"surname" : "Smith",
"age": 27,
"phoneno":"123456789",
"addresses": [
{
"address": {
"AddressLine1":"1 milky way",
"AddressLine2":"galaxy cresent",
"city": "tempest",
"state": "Idiho",
"postcode": "12345"
}
},
{
"address": {
"AddressLine1":"52 Saturn Drive",
"AddressLine2":"Wharfridge",
"city": "tempest",
"state": "Idiho",
"postcode": "12345"
}
}
]
}
}
and what I need is to flatten the array into the root node like this:
{
"person": {
"firstname": " Fred",
"surname" : "Smith",
"age": 27,
"phoneno":"123456789",
"addr1_AddressLine1":"1 milky way",
"addr1_AddressLine2":"galaxy cresent",
"addr1_city": "tempest",
"addr1_state": "Idiho",
"addr1_postcode": "12345",
"addr2_AddressLine1":"52 Saturn Drive",
"addr2_AddressLine2":"Wharfridge",
"addr2_city": "tempest",
"addr2_state": "Idiho",
"addr2_postcode": "12345"
}
}
If there any other solutions\ideas, i am all ears.
Thanks in advance for your help
Paul
So i found a solution or rather a work around, for some reason the liquid connector in logic apps does not support the "increment" tag, this was causing my issue. i was able to evaluate a property form the input json to decide where my fields would reside. but thanks for

Formatting JSON files for SQLContext

I'm experiencing issues when loading JSON which are dependent on formatting of input JSON file.
According to Spark documentation on JSON Datasets, each line on input file must be a valid JSON Object. re:
"Note that the file that is offered as a json file is not a typical JSON file. Each line must contain a separate, self-contained valid JSON object. As a consequence, a regular multi-line JSON file will most often fail."
So, if I have an input JSON file such as:
{
"Year": "2013",
"First Name": "DAVID",
"County": "KINGS",
"Sex": "M",
"Count": "272"
},
{
"Year": "2013",
"First Name": "JAYDEN",
"County": "KINGS",
"Sex": "M",
"Count": "268"
}
Are there any existing tools or scripts to convert to:
{"Year": "2013","First Name": "DAVID","County": "KINGS","Sex": "M","Count":"272"},
{"Year": "2013","First Name": "JAYDEN","County": "KINGS","Sex": "M","Count": "268"}
where the JSON conforms to "Each line must contain a separate, self-contained valid JSON object"
If I format to this style above, things work as expected. But, I made these mods manually over a few rows. I cannot do this for entire data set, so looking for an existing script or tool.
OR
I could load to JDBC available database if that's a better option. Thoughts?
Thanks in advance
You can simply load the JSON files into an RDD first using sc.wholeTextFiles() and remove the file name column, then run the SQLContext read on the RDD contents.
e.g.
val jsonRdd = sc.wholeTextFiles("samplefile.json").map(x => x._2)
val jsonDf = sqlContext.read.json(jsonRdd)
What if you make it an array by adding square brackets. like this;
[
{
"Year": "2013",
"FName": "DAVID",
"County": "KINGS",
"Sex": "M",
"Count": "272"
},
{
"Year": "2013",
"FName": "JAYDEN",
"County": "KINGS",
"Sex": "M",
"Count": "268"
}
]
If I take your file and add the brackets I can iterate through it with Node.js and output a file that looks like what you want. The caveat in node.js being I cant have variable First Name-- I had to change it to FName.

How to write description document for JSON file?

I want to write a description document for my JSON files. I used to write it for database. It has ER Diagram and Data Dictionary. But when it comes to JSON file, are there any diagrams that can be used to describe JSON file that can be read easily?
Try out http://jsonviewer.stack.hu/. For a json string like this,
{
"address": {
"streetAddress": "21 2nd Street",
"city": "New York"
},
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
}
]
}
It produces an output like,

How does one enforce HTTP responses as JSON messages?

I am quite new to json and webservices.
I have a question which might as well be a dumb one but I have not been able to find an answer.
Lets say I have a html form which performs a GET method on submission.
The server performs an HTTP response.
How does one make sure that the content(body) of the response is in JSON format?
-V
This is quite a vague question as I don't know what your server is doing and replying (obviously it should be in the JSON format response) but you should also include a Content-type HTTP header as shown below so the browser knows it is receiving JSON:
Content-type: application/json
Your JSON response should look like this (taken from Wikipedia):
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
data=response_from_the_page;
try {
json = $.parseJSON(data);
} catch (e) {
// not json
}