sql data convert to json - mysql

Below is my query result. i want the json out put as :
[
{id:1,
name:a,
category : [
{name:balakrishnan},
{name:nikhil}
]
},
{id:2,
name:b,
category : [
{name:midhun},
{name:amith}
]
}
]
I have done this with async parallel, i just want to know where we can do in another approach like map functions

Related

How to extract in python3 a signle value from json key with multiple values?

import json
person = '{"name": "Bob", "languages": ["Italian", "English", "Fench"], "location": "Naples"}'
person_dict = json.loads(person)
print(list(person_dict.values())[1:1])
Hi guys i've a little issue handling json value in py3.
The question is simple.. considering the code above, how i can extract only 'Italian' value from 'languages' key?
The code is surely wrong because it give nothing:
[]
Any help will be appreciated.
Edit:
The imported pkgs:
from urllib3 import PoolManager, request
import certifi
import json2
the right api output in json format:
{
"error": [],
"result": {
"AUCTION1": {
"asks": [
[
"281.00000",
"0.163",
1609860353
]
],
"bids": [
[
"277.60000",
"0.100",
1609860353
]
]
}
}
}
And this is the function where i've the issue:
p_urll = PoolManager(ca_certs=certifi.where())
p_req = "https://api.auctionsite.com/0/public/Depth?pair=AUCTION1&count=1"
p_api_req = p_urll.request('GET', p_req)
p_api_res = json2.loads(p_api_req.data.decode('utf-8'))
print(p_api_res['result']['AUCTION1']['asks'][0])
It's not simple like the 1st example.. my fault..
Using [0] the code will give this result:
['26098.60000', '0.781', 1609861809]
i need only the auction price, so the 1st "asks" value (this "281.00000" without quotes)

JSON - replacement characters for Rest API query

I'm trying to retrieve data from a WEB API rest server.
https://stat-xplore.dwp.gov.uk/webapi/online-help/Open-Data-API.html
Which stores the data in Cubes.
I understand the basics and am successfully retrieving data. However I want to use a Postcode, (ZIP Code) grouping dimension.
This dimension groups postcodes by their first 3 characters. CF1, CF2 etc.
The schema has this as the definition for sending the request for this dimension.
["str:group:UC_Households:X_Geography+%28postcode%29"]
I'm assuming I'm meant to replace the end part with the Postcode group that I'm interested in and enclose the postcode group with some special characters.
So far I have tried
["str:group:UC_Households:X_Geography:CF1"]
["str:group:UC_Households:X_Geography:'CF1'"]
["str:group:UC_Households:X_Geography:#CF1#"]
["str:group:UC_Households:X_Geography:CF1%"]
["str:group:UC_Households:X_Geography#CF1#"]
["str:group:UC_Households:X_GeographyCF1"]
["str:group:UC_Households:X_Geography:CF1"]
the complete JSON is
{ "database" : "str:database:UC_Households",
"measures" : [ "str:count:UC_Households:V_F_UC_HOUSEHOLDS" ],
"dimensions" : [
[ "str:field:UC_Households:V_F_UC_HOUSEHOLDS:HNFAMILY_TYPE" ],
["str:group:UC_Households:X_Geography:CF1"]
]
}
Any help would be appreciated.
How to easily generate the query JSON for the Stat-Xplore API
Log into https://stat-xplore.dwp.gov.uk
Open the data set and table you want
In the top-right hand corner, find the "Download Table" tool
In the drop-down box, select "Open Data API Query (.json) and click "Go"
This will download a JSON file with the parameters for the query that describes the table you're looking at in your web browser. An example is shown below:
{
"database" : "str:database:UC_Monthly",
"measures" : [ "str:count:UC_Monthly:V_F_UC_CASELOAD_FULL" ],
"recodes" : {
"str:field:UC_Monthly:F_UC_DATE:DATE_NAME" : {
"map" : [ [ "str:value:UC_Monthly:F_UC_DATE:DATE_NAME:C_UC_DATE:202010" ] ],
"total" : false
},
"str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE" : {
"map" : [ [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE:C_UC_EMPLOYMENT:0" ], [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE:C_UC_EMPLOYMENT:1" ] ],
"total" : true
},
"str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE" : {
"map" : [ [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE:V_C_MASTERGEOG11_PARLC_TO_REGION:E14000541" ], [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE:V_C_MASTERGEOG11_PARLC_TO_REGION:E14000542" ], [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE:V_C_MASTERGEOG11_PARLC_TO_REGION:W07000041" ] ],
"total" : true
}
},
"dimensions" : [ [ "str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE" ], [ "str:field:UC_Monthly:F_UC_DATE:DATE_NAME" ], [ "str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE" ] ]
}
The API table endpoint docs give more info.
An explanation for OP's issue
I'm also attempting to use this API. (I happen to be using Python requests library.)
If I make a request to the /v1/table endpoint where one dimension is of type GROUP (rather than FIELD)
measures=["str:count:UC_Monthly:V_F_UC_CASELOAD_FULL"],
dimensions=[["str:field:UC_Monthly:F_UC_DATE:DATE_NAME"],
["str:group:UC_Monthly:X_Geography+%28residence-based%29"]
]
then this error 422 is returned in the HTTP response:
requests.exceptions.HTTPError: 422 Client Error: Unprocessable Entity for url: https://stat-xplore.dwp.gov.uk/webapi/rest/v1/table
This is the payload of the response, which describes the error in more detail:
{
"message":"Unexpected schema component type 'group' for 'str:group:UC_Monthly:X_Geography+%28residence-based%29'. Expected one of type [field, valueset, udf].",
"errorType":"UNEXPECTED_SCHEMA_COMPONENT_TYPE_EXCEPTION",
"component" : "str:group:UC_Monthly:X_Geography+%28residence-based%29",
"type" : "group",
"expectedTypes" : [ "field", "valueset", "udf" ]
}
If I understand this correctly, it's not complaining about the formatting of the string, it's saying that only FIELD, VALUESET and UDF objects are expected.
If you look at the schema endpoint one can retrieve info about the possible groups, fields, etc.
{
'id': 'str:group:UC_Monthly:X_Geography+%28postcode%29',
'label': 'Geography (postcode)',
'location': 'https://stat-xplore.dwp.gov.uk/webapi/rest/v1/schema/str:group:UC_Monthly:X_Geography+%2528postcode%2529',
'type': 'GROUP'
}

Is it possible to combine two Json with Ruby in MongoDB?

I have to insert documents in my Mongo database by using Ruby (not on Rails, on Notepad++), many documents have duplicates with some modifications.
I want to write a script which use a json file, read it, import it in MongoDB by checking if each documents do not have any duplicate in the database, and if there is a duplicate I want to combine it if it contains any additional information:
Such as :
Document 1
{ "Name" : "Lila",
"Files":
[
{ "Name": "File1", "Date" : "05-11-2017"},
{ "Name": "File2", "Date" : "26-03-2018"}
]
}
Document 2
{ "Name" : "Lila",
"Files":
[
{ "Name": "File3", "Date" : "26-03-2018"}
]
}
Combine them to have:
{ "Name" : "Lila",
"Files":
[
{ "Name": "File1", "Date" : "05-11-2017"},
{ "Name": "File2", "Date" : "26-03-2018"},
{ "Name": "File3", "Date" : "26-03-2018"}
]
}
I founded that it is possible in mongo shell thanks to the aggregation-accumulation $mergeObjects, but in Ruby it do not seems to exists.
You can use all the operators in ruby, too. You need to get the underlying collection object first.
require 'mongo'
db = Mongo::Connection.new.db("mydb")
coll = db.collection('posts')
coll.aggregate([
{"$project" => {"last_name" => 1, "first_name" => 1 }},
{"$match" => {"last_name" => "Jones"}}
])
This is an example pipeline. You can give the same aggregation pipeline that worked for you on the mongo shell to aggregate.
For more information, refer to the MongoDB Ruby driver documentation:
http://www.rubydoc.info/gems/mongo/1.8.2/Mongo%2FCollection%3Aaggregate

Json array in mongoDB

I want to get objects according to an ID they have in an array in a json file in mongodb.
I tried a lot of ways to get them with no success:
db.collection.find({"Id":"2"})
db.collection.find({"Messages.Id":"2"})
db.collection.find({"Messages":{$elemMatch:{"Id":"2"}}})
db.collection.find({"Messages.Id":{$elemMatch:{"Id":"2"}}})
{
"Messages" : [
{
"text":"aaa",
"Id" : [ "1", "2" ]
},
{
"texts" : "bbb",
"Id" : [ "1", "3" ]
}
]
}
Even though that's how it's supposed to be done according to the mongodb documentation.
So I thought something was wrong with my json design (I tried changing it but that didn't help either).
Can anyone suggest to me a good design or query to get the objects with a certain id will work?
UPDATE:
I want for example that if in the query i request the id 2
only the first message and all of it will be displayed (I don't mind if the Id field wont be displayed)
{
"text":"aaa",
"Id":["1","2"]
}
To find single elements that match you will need to utilize the positional operator ($).
db.collection.find({"Messages.Id": "2"}, {"Messages.$": 1, _id: 0})
For finding multiple matches, you would use the aggregation pipeline:
db.collection.aggregate([
{ $unwind: "$Messages" },
{ $match: {"Messages.Id": "1"}},
{ $group: { _id: null, messages: { $push: "$Messages"}}}
])

add to existing object in json array Rails 3

render :json mailbox.inbox.as_json(:include => [
{:messages => {:only => [:body]}},
{:recipients => {:only => [:Firstname,:Surname]}}
])
produces the following output from my Rails controller:
[
{
"conversation":
{
"subject":"Subject for conversation 2",
"messages" :[
{"body":"Message 1"},
{"body":"Message 2"}
],
"recipients": [
{"Firstname":"James","Surname":"Smith"},
{"Firstname":"John","Surname":"Doe"}
]
}
}
]
What I would like to do is to add a current_user key:value pair to each conversation object so the out sample would look like this:
[
{
"conversation":
{
"subject":"Subject for conversation 2",
"messages" :[
{"body":"Message 1"},
{"body":"Message 2"},
],
"recipients": [
{"Firstname":"James","Surname":"Smith"},
{"Firstname":"John","Surname":"Doe"}
],
"current_user" :[
{"id":10}
]
}
}
]
I don't have access to the conversation model in question (it's running off the mailboxer gem) so can't override as_json. Any help would be appreciated
So, formalizing the comments.
By looking the apidock of json_as, you can see that the only way to include arbitrary data is to add a method that retunrs the data you want to use in the model.
Since it is not the best thing to do in many ways and you don't have access to the model, one way out to customize your json is using a popular gem called jbuilder, that allows you to create a more complex json.