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.
Related
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'
}
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
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
Good day community, I am using LUIS to train a data set to let it classified between different meaning of the words. After I've done trained, I want to import a set of data to let it test. There is a batch testing options for me to import a json file, however it keeps showing this error:
BadArgument: Dataset object cannot be null. Parameter name: dataSet
I have already follow the json format that it gave which is like this:
[
{
"text": "hey dad, are you hungry?",
"intent": "None",
"entities":
[
{
"entity": "FamilyMember",
"startPos": 4,
"endPos": 6
}
]
},
{
.
.
.
}
]
My json file has the format like this:
[
{
"text" : "Hello"
"intent": "Greetings"
},
{
"text" : "I want bread"
"intent": "Request"
}
]
Can anyone tell me what am I doing wrong? The training doesn't include any entities so I did not put it into my json file.
Thank you.
You still need to provide the entities attribute and give it an empty array, otherwise you'll receive a different error. Regarding your format, you're missing commas after your text attributes.
[
{
"text" : "Hello",
"intent": "Greetings",
"entities": []
},
{
"text" : "I want bread",
"intent": "Request",
"entities": []
}
]
When I used the above code the batch test successfully completed for me.
I have a basic Json question - I have a JSON file. Every object in this file has columns repeated.
[
{
id: 1,
name: "ABCD"
},
{
id: 2,
name: "ABCDE"
},
{
id: 3,
name: "ABCDEF"
}
]
For optimization I was thinking to remove repeated column names.
{
"cols": [
"id",
"name"
],
"rows": [
[
"1",
"ABCD"
],
[
"2",
"ABCDE"
]
]
}
What I am trying to understand is - is this a better approach? Are there any disadvantages of this format? Say for writing unit tests?
EDIT
The second case (after your editing) is valid json. You can derive it to the following class using json2csharp
public class RootObject
{
public List<string> cols { get; set; }
public List<List<string>> rows { get; set; }
}
The very important point to note about a valid json is that it has no other way but to repeat the column names (or, keys in general) to represent values in json. You can test the validity of your json putting it # jsonlint.com
But if you want to optimize json by compressing it using some compression library like gzip (likewise), then I would recommend Json.HPack.
According to this format, it has many compression levels ranging from 0 to 4 (4 is the best).
At compression level 0:
you have to remove keys (property names) from the structure creating a header on index 0 with each property name. Then your compressed json would look like:
[
[
"id",
"name"
],
[
1,
"ABCD"
],
[
2,
"ABCDE"
],
[
3,
"ABCDEF"
]
]
In this way, you can compress your json at any levels as you want. But in order to work with any json library, you must have to decompress it to valid json first like the one you provided earlier with repeated property names.
For your kind information, you can have a look at the comparison between different compression techniques:
{
"cols": [
"id",
"name"
],
"rows": [
"1",
"ABCD"
], [
"2",
"ABCDE"
], [
"3",
"ABCDEF"
]
}
In this approach it will be hard to determine which value stand for which item (id,name). Your first approach was good if you use this JSON for communication.
A solution for it, is use any type (by your preference) of Object-Relational-Mapper,
By that, you can compress your JSON data and still using legible structure/code.
Please, see this article: What is "compressed JSON"?