I have the following table I am trying to transform the json data into multiple calculated fields but I am stuck as I have multiple firstname in the same column. Please see the example below of what Im trying to achieve. Thank You for your help.
School
StudentJson
Middle School
[{"firstname":"John","lastname":"doe","firstname""Patrick","lastname":"mahomes","firstname":"steve","lastname":"segal"}]
High School
[{"firstname":"tom","lastname":"brady","firstname""jason","lastname":"fred"}]
Final Result
School
firstname
lastname
Middle School
John
doe
Middle School
Patrick
mahomes
Middle School
steve
doe
High School
Tom
segal
High School
Jason
Fred
High School
John
doe
I have tried using a forloop and split but it didnt work
In the case of
[
{
"firstname":"tom",
"lastname":"brady",
"firstname":"jason",
"lastname":"fred"
}
]
You have all the paramaters under the same object. Does changing your json to be
[
{
"firstname":"tom",
"lastname":"brady"
},
{
"firstname": "jason",
"lastname":"fred"
}
]
Fix your issue?
Related
I have a Postgres statement that returns extracts/iterates over a json blob in the value column of a table. I am able to get a count one level deep using the query below but I can't count any deeper. I was using:
select jsonb_array_length(value -> 'team') as team_count
This returns the proper count but I cant seem to leverage this to count the names under each team.
In a perfect world I would my results to return 4 lines of results like this(title and a matching count of names):
Product Owner, 2
Technical Product Manager, 2
Data Modeler, 0
Engineer, 0
How would I go about amending this query to give me the count of names under team? I tried all sorts of stuff but nothing that got me close.
Sample Json is below.
"team":[
{
"title":"Product Owner",
"names":[
"John Smith",
"Jane Doe"
]
},
{
"title":"Technical Project Manager",
"names":[
"Fred Flintstone",
"Barney Rubble"
]
},
{
"title":"Data Modeler"
},
{
"title":"Engineer"
}
You seem to be looking for
SELECT
role -> 'title' AS team_role,
jsonb_array_length(role -> 'names') AS member_count
FROM jsonb_array_elements(value -> 'team') AS team(role)
I made a simple mapping with two fields where i am analyzing one field which is text type and other field is a keyword type. example
fields: Category_one, Category_two
Data
{"Category_one": "liked wine", "Category_two":"Wine"}
{"Category_one": "liked pasta", "Category_two":"pasta"}
{"Category_one": "liked wine and pasta", "Category_two":"Wine and pasta"}
{"Category_one": "liked wine so much", "Category_two":"Wine"}
...........
..........
.....
Now I wrote a search query for the index.
Get _search/cat
{
"size": 20,
"query": {
"match": {
"Category_one.ngrams": {
"query": "Nice food place in XYZ location",
"analyzer": "standard"
}
}
}
}
}
It's working fine. I want to find the top 5 unique Category_two values according to their match query score in Category_one.
For example :
Let's say the query returns 20 results("size" in the query above) with different scores, out of which first 6 are wine(Category_two), next 4 are pasta(Category_two), and so on..
These 20 results have duplicates. How can I fetch top 3 unique values from Category_two for "Wine", "pasta", "wine and pasta" as per their scores in the Category_one match score?
Can someone help me understand how to approach this problem? Any suggestions would be appreciated.
Thank you.
I'm using NODEJS and MySQL for an API server and I ran into an issue. The queries that I'm running have multiple rows of results due to foreign keys.
User Table Address Table
|id| name| |id|user_id| Address|
| 1| frank| | 1| 1| 123 main street, Moncton|
| 2| steve| | 2| 2|97 duplessie street, Edmundston|
| 3| 2| 933 manhole av, Grand Sault|
How would I go about to get a multilevel json object like below for my query result, or at least, for my API response.
{
"id":1,
"name": "frank",
"addresses": [
{"id":1,"Address":"123 main street, Moncton"}
]
},
{
"id":2,
"name": "steve",
"addresses": [
{"id":2,"Address":"97 duplessie street, Edmundston"},
{"id":3,"Address":"933 manhole av, Grand Sault"}
]
}
I followed #dmfay direction and came up with a solution that works for me.
This a function where I pass the 2 JSON objects to merge addresses into users JSON and returns everything in a single JSON object.
function combineResults(users, addresses){
***********************************
*** Go through each user object ***
***********************************
users.forEach(function(user) {
user.addresses = addresses.find(address => address.user_id === user.id);
}
return users;
}
If you're just running raw queries, the results you get are the results you get. Object-relational mapping libraries like Objection, Bookshelf, or Sequelize will handle this for you automatically but may involve more setup and/or abstraction than you'd like.
If you'd rather keep it minimal, you can always process the result array yourself. I ran into this exact issue some time ago with my own project (a data mapper for Postgres) and developed a decomposer you should be able to cannibalize without issues. See the docs for constructing schemas.
I need help with the output of my json file. I'm trying to print out the keys in a list called keypairs. I have to generate 60 keys, which i have included in the (count<60) part of my code (which isnt here). Im just showing the exporting part of my codes which i have a problem with. Here are my codes:
with open("/home/pi/Desktop/database.json", 'w+'):
db = TinyDB('/home/pi/Desktop/database.json')
table = db.table('Books')
db.insert({'Book ID' : keypair[bookid], 'Serial No.' : keypair[bookserial] })
However, the problem I have right now is that it prints out all the keys in the lists - bookid and bookserial instead of printing a pair of keys in one { }.
Here's an example output where count<2:
Pillar": {}, "_default":
{"1":
{"Bookid": ["b'\\XXXXXX bookid 1 XXXXXXX'", "b'\\AAAAAA bookid 2 AAAAAAA'"],
"Serial No.": ["b'\\YYYYYserialno 1YYYYY'", "b'BBBBserial no2BBBB'"]
}
}
This is the output that i intend to get:
in a format like this where a pair of keys are printed everytime it runs again:
{ Books:
{
Book ID: XXXX bookid 1 XXXX
Serial No.: XXX serialno 1 XXXX
}
{
Book ID: XXX bookid 2 XXXX
Serial No.: XXX serial no. 2 XXX
}
}
As you can see, the json file is messy, i have no idea how to make it automatically neat and also, you can see that the keys print out together into one instead of separately. Imagine having to print 60 pairs of these and they are all under one { }.
Help!
I see multiple syntax problems in your code when you are trying to create the json. You could try to do this.
{ Books:
{
{
Book ID: XXXXXXXX
Serial No.: XXXXXXX
},
{
Book ID: XXXXXXX
Serial No.: XXXXXX
}
}
}
As you can see i wrapped the book items in brackets as well and the items are comma separated now.
This could help you with your problem.
I am currently looking into my first application using Firebase as the backend.
I have 2 models, School and User. Each user can sign up for a date to attend the school, so I also need a Date.
A SQL table would look like this:
schools: id, name
users: id, name, email
schools_users: id, school_id, user_id, date
What would be the proper way of designing this data structure in Firebase?
Since you don't specify any requirements, I suggest starting with the most naive mapping at first:
root
schools
1: "name of school1"
2: "name of school2"
users:
1: { "name": "Maeh", "email": "2523229#stackoverflow.com" }
2: { "name": "Frank", "email": "209103#stackoverflow.com" }
schools_users:
1_1: "20141031"
1_2: "20130102"