Express JS fetch proper JSON with Mysql - mysql

I am working on WebAPI on ExpressJS. I am fetching data from MySQL database by using node module mysql. I have a mater and a detail table let say
Countries(ID, Name, Code)
States (ID, Name, CountryId)
Now I want to query the data in JSON as following:
{
"country":{
"ID" : 1,
"Name" : "United States of America"
"Code" : "USA"
"Cities" :
[
{
"1",
"Alabama",
"AL"
},
{
"2",
"Alaska",
"AK"
},
{
"3",
"California",
"CA"
}
]
}
}
How can I query data in the above format?
Thanks and Regards.

How can I query data in the above format?
You can't because this is not a valid JSON. No matter what you do, you will never be able to represent your data in that format.
To represent data coming from a relational database as a JavaScript nested objects and arrays, it will be easiest to use an ORM like Sequelize, Bookshelf, ORM2 or Waterline. See:
http://docs.sequelizejs.com/
http://bookshelfjs.org/
https://github.com/dresende/node-orm2
http://sailsjs.com/documentation/reference/waterline-orm
ORM is an Object-Relational Mapper and lets you map the relational model to objects, which is exactly what you're trying to do here.

Related

mySQL JSON Document Store method for inserting data into node 3 levels deep

I want to take the data from here: https://raw.githubusercontent.com/usnistgov/oscal-content/master/examples/ssp/json/ssp-example.json
which I've pulled into a mySQL database called "ssp_models" into a JSON column called 'json_data', and I need add a new 'name' and 'type' entry into the 'parties' node with a new uuid in the same format as the example.
So in my mySQL database table, "ssp_models", I have this entry: Noting that I should be able to write the data by somehow referencing "66c2a1c8-5830-48bd-8fdd-55a1c3a52888" as the record to modify.
All the example I've seen online seem to force me to read out the entire JSON into a variable, make the addition, and then cram it back into the json_data column, which seems costly, especially with large JSON data-sets.
Isn't there a simple way I can say
"INSERT INTO ssp_models JSON_INSERT <somehow burrow down to 'system-security-plan'.metadata.parties (name, type) VALUES ('Raytheon', 'organization') WHERE uuid = '66c2a1c8-5830-48bd-8fdd-55a1c3a52888'
I was looking at this other stackoverflow example for inserting into JSON:
How to create and insert a JSON object using MySQL queries?
However, that's basically useful when you are starting from scratch, vs. needing to add JSON data to data that already exists.
You may want to read https://dev.mysql.com/doc/refman/8.0/en/json-function-reference.html and explore each of the functions, and try them out one by one, if you're going to continue working with JSON data in MySQL.
I was able to do what you describe this way:
update ssp_models set json_data = json_array_append(
json_data,
'$."system-security-plan".metadata.parties',
json_object('name', 'Bingo', 'type', 'farmer')
)
where uuid = '66c2a1c8-5830-48bd-8fdd-55a1c3a52888';
Then I checked the data:
mysql> select uuid, json_pretty(json_data) from ssp_models\G
*************************** 1. row ***************************
uuid: 66c2a1c8-5830-48bd-8fdd-55a1c3a52888
json_pretty(json_data): {
"system-security-plan": {
"uuid": "66c2a1c8-5830-48bd-8fdd-55a1c3a52888",
"metadata": {
"roles": [
{
"id": "legal-officer",
"title": "Legal Officer"
}
],
"title": "Enterprise Logging and Auditing System Security Plan",
"parties": [
{
"name": "Enterprise Asset Owners",
"type": "organization",
"uuid": "3b2a5599-cc37-403f-ae36-5708fa804b27"
},
{
"name": "Enterprise Asset Administrators",
"type": "organization",
"uuid": "833ac398-5c9a-4e6b-acba-2a9c11399da0"
},
{
"name": "Bingo",
"type": "farmer"
}
]
}
}
}
I started with data like yours, but for this test, I truncated everything after the parties array.

Create the structure of an empty collection in mongoDB

Is there any possibility to create the structure of an empty collection in MongoDB using mongoimport from a JSON file like the one below?
"Users" : {
"name" : "string",
"telephone" : {
"personal": { "type": "number" },
"job": { "type" : "number" }
},
"loc" : "array",
"friends" : "object"
}
My goal is to create a mongoDB schema from JSON files.
Yes, you can mongoimport a JSON file and if you clear out the values of those field (set them to ""), importing your JSON file should do just that.
However, MongoDB is a NoSQL database, and creating a schema in the MongoDB database doesn't really make sense. What will happen is that you'll have one record with fields whose values are empty.

Constructing JSON string from Oracle DB

I have a Web application which gets it data from a JSON string.
The JSON is in the following format
{
"contacts": [{
"type": "contact",
"name": "John Doe",
"contact": 1,
"links": ["Spouse", "Friends","Jane Doe","Harry Smith"]
}]
}
Now this is a sample data. My actual DB is in Oracle. My question would be how do I construct this JSON from Oracle.
This is the best method I've come across: http://ora-00001.blogspot.sk/2010/02/ref-cursor-to-json.html.
To summarise:
Use the DBMS_XMLGEN package to generate XML from a SYS_REFCURSOR.
Then transform it using this XSLT.
I like it because there's no manual generation and because you have the option of returning XML too by skipping the final transformation.

Mule Database Query to JSON output nesting not working?

I am new to using Mule and having a problem with outputting nested Json. I am using mule 3.3 and the GUI interface. I add the database component to my flow and run some simple SQL including a 'one to many' join. After this I added an 'Oject to Json' component, the problem is the resulting JSON is flat it does not nest the 'one to many' elements. For example I expect:
{
"firstName": "John",
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
I actually get:
{
"firstName": "John",
"type": "home",
"number": "212 555-1234"
}
Can anyone give me any direction on what I am missing?
Since you haven't posted your configuration, it's hard to tell exactly what you're doing. Anyways...
The select query returns a flat view of the users data. If you want to create a structured representation of it, you'll have to create a transformer to do so, prior to serializing to JSON.
Alternatively, you can use an ORM to map your data to objects then serialize these objects to JSON.

JSON data with array structure

I am using Neo4j and ExtJS in my application.
One good thing is that both handle JSON with array structure.
Neo4j returns and ExtJS can consume JSON like this:
{
columns: ["name", "age"],
data: [
["Peter", 34],
["Mike", 52]
]
}
instead of:
[
{"name": "Peter", "age" 34},
{"name": "Mike", "age" 52},
]
However, from my application server's HTTP API I want people to be able to choose which one of these JSON structures to receive.
So they are both JSON which means the HTTP header should be "Accept": "application/json". But how should I allow them to pick either one of the structures? Should they set a header or a query param? What is best practice?
I think that a simple param in the request would be enough.