What is the ideal JSON API format for Restkit integration? - restkit

Is there a model API I can watch/try, that has the easiest integration with RestKit (using the most defaults)?
I'm talking about the JSON format (having a root or not, nested objects, etc) and the expected answer after a POST for example...

The best resource I could find is this writeup here that teaches by example:
https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md
It would seem the basic format to represent a record from your model is something like:
{
"recordType": {
"attribute1": "value1",
"attribute2": "value2",
"attribute3": "value3"
}
}
and if you have a collection of records, use Javascript array, like this: [ {...}, {...} ]
The writeup also suggests this form, which is:
{
"recordType": [
{
"attribute1": "value1",
"attribute2": "value2",
"attribute3": "value3"
},
{
"attribute1": "value1",
"attribute2": "value2",
"attribute3": "value3"
}
]
}
I guess you can map both ways.
This question may be helpful:
JSON format inconsistency

Related

Add array of objects with the dot notation

With the following code Arr::set($array, 'messages.to.email', $value);
That will generate an output like:
{
"messages": {
"subject": "this is out email",
"to": {
"email": "myemail#gmail.com"
}
}
}
My issue is that I need this output instead:
{
"messages": [{
"subject": "this is out email",
"to": {
"email": "myemail#gmail.com"
}
}]
}
Being messages an array of objects. I haven't been able to find a native way of doing it, so I'm thinking of manually adding something like messages[].to.email
Am I missing something? A magic helper or syntax? to achieve what I'm looking for?
If you used an index for the messages it would force it to be an array:
Arr::set($array, 'messages.0.to.email', $value);
This is just adding another level/container to this by specificying that there is something between 'messages' and 'to'. Could be named anything, but 0 is for zero indexed array here, which would be an array in JSON not an object.

Accessing array value in Nifi FreeFormTextRecordSetWriter

I'm trying to access values of an array in Json using FreeFormTextRecordSetWriter
Input data:
{"data":[["1580860800000","67.2"]],"itemid":5917,"label":"xxx","type":"stacked_element"}
Desired output
{"data1":"1580860800000", "data2":"67.2","itemid":5917,"label":"xxx","type":"stacked_element"}
Can this be done using Nifi Expression language ?
I don't believe FreeFormTextRecordSetWriter currently allows access to nested fields, please feel free to write a Jira to add this capability or perhaps a FreeFormTextRecordPathWriter to enable the use of RecordPath expressions.
I assume if you're trying FreeFormTextRecordSetWriter, then you know there will always be two entries in the data array. If that's the case, since the input/output is valid JSON, if there's one object in the flowfile you can use JoltTransformJSON with the following spec:
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"0": "data1",
"1": "data2"
}
},
"*": "&"
}
}
]
If there is more than one JSON object in the file, you can use JoltTransformRecord with a JsonTreeReader and JsonRecordSetWriter and the above spec.
If you don't know how many elements are in the array, you can still split them up with the following spec, but note that the first element has an index of 0 not 1 (so data0 instead of data1):
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"*": "data&"
}
},
"*": "&"
}
}
]
UpdateRecord is another option, but I believe you'd still have to know how many elements are in the array.

How to get name/confidence individually from classify_text?

Most of the other methods in the language api, such as analyze_syntax, analyze_sentiment etc, have the ability to return the constituent elements like
sentiment.score
sentiment.magnitude
token.part_of_speech.tag
etc etc etc....
but I have not found a way to return name and confidence in isolation from classify_text. It doesn't look like it's possible but that seems weird. Am missing something? Thanks
The language.documents.classifyText method returns a ClassificationCategory object which contains name and confidence. If you only want one of the fields you can filter by categories/name or categories/confidence. As an example I executed:
POST https://language.googleapis.com/v1/documents:classifyText?fields=categories%2Fname&key={YOUR_API_KEY}
{
"document": {
"content": "this is a test for a StackOverflow question. I get an error because I need more words in the document and I don't know what else to say",
"type": "PLAIN_TEXT"
}
}
Which returns:
{
"categories": [
{
"name": "/Science/Computer Science"
},
{
"name": "/Computers & Electronics/Programming"
},
{
"name": "/Jobs & Education"
}
]
}
Direct link to API explorer for interactive testing of my example (change content, filters, etc.)

Golang equivalent of `jq --stream -c`

Using jq --stream -c on the command line, I can format pretty JSON like this:
{
"object": {
"something": {
"key1": 123,
"key2": 456
},
"something_else": {
"key1": [
"value1",
"value2"
]
}
}
}
into this:
[["object","something","key1"],123]
[["object","something","key2"],456]
[["object","something","key2"]]
[["object","something_else","key1",0],"value1"]
[["object","something_else","key1",1],"value2"]
[["object","something_else","key1",1]]
[["object","something_else","key1"]]
[["object","something_else"]]
[["object"]]
I've looked through the Golang documentation for JSON but couldn't find anything similar. Is there such a function that I've missed?
More precisely I'd like to print the above JSON like so:
object.something.key1=123
object.something.key2=345
object.something_else.key1.0=value1
object.something_else.key1.1=value2
I've looked through the Golang documentation for JSON but couldn't find anything similar. Is there such a function that I've missed?
No, you haven't missed anything. The current JSON library doesn't support what you described "out of the box".
If you want this to work, you'll need to find a package that offers the flexibility you need or satisfy json.Marshaler with a custom type yourself.

Try to understand normalizr's schema.entity VS Array and Object

All:
I am trying to understand the relationship between Entity Array and Object:
Are they just different format to describe diff structure of data? Or Entity is quite diff from the rest two?
The normalized data result has a structure like {result:,entities:}, are the data structures only defined with schema.Entity put inside entities or so can schema.Array and Object? When I define a schema only use Object and Array, it seems nothing put in entities, I am not sure if it is my schema def fault or this is how normalizr work?
If only schema.Entity() defined data can put into entities, then how can I put an data array into it, something like {0:.., 1:..,2:,}?
For exmaple, I have data like:
var data = [
{
id:"0",
items:[
{
id: "0",
data: {name:"data-0-0"}
},
{
id: "1",
data: {name:"data-0-1"}
}
]
},
{
id:"1",
items:[
{
id: "0",
data: {name:"data-1-0"}
},
{
id: "1",
data: {name:"data-1-1"}
}
]
}
]
const normalizedData = normalize(data, [{items:[{data:{}}]}]);
And the normalized data is like:
{
"entities": {},
"result": {
"0": {
"id": "0",
"items": [
{
"id": "0",
"data": {
"name": "data-1-0"
}
}
]
}
}
}
Thanks
Question: Are they just different format to describe diff structure of data? Or Entity is quite diff from the rest two?
Answer: Yes. An Entity is a singular object that has a unique identifier associated with it. Array and Object are more generic structures that can't be uniquely identified. In your case, it looks like you only need to use Array and Entity for the data you're describing.
Question: Are the data structures only defined with schema? Entity put inside entities?
Answer: Yes.

Resources