Could someone recommend what schema to choose in YAML file for API, if the expected response is the following:
"items": [{
"garden": [{
"tree": "pine",
"height": "33"
}, {
"height": "33",
"age": "200"
}
]
}
],
I was thinking about
list (items)
object (garden)
list (list of objects)
object (pairs - height+age)
I am confused, because property height may appear a lot of times in the list together with different params.
Related
I am trying with Nifi JOLT processor to shift the flat JSON object to a list of JSON object as in below output. The params "p_7_1_0", "px_2_7_1_0", "pv_7_1_1" may be different in names or numbers (e.g. I could have { "timestamp": 1559347670, "pw_2_1_0": 1, "p_2_2_1_0": 1 } )
Could someone help me with the jolt specs?
Input Json:
{
"timestamp": 1559347670,
"p_7_1_0": 6,
"px_2_7_1_0": 1,
"pv_7_1_1": 1
}
Expected output JSON:
{
"values": [
{
"key": "p_7_1_0",
"value": 6,
"timestamp": 1559347670
},
{
"key": "px_2_7_1_0",
"value": 1,
"timestamp": 1559347670
},
{
"key": "pv_7_1_1",
"value": 1,
"timestamp": 1559347670
}
]
}
Thanks in advance
After reading up on this question JOLT transformation to copy single value along an array
and the answer
https://stackoverflow.com/a/50438480/2733184
I can see that what you want is eerily similar. However, I would have never hit the nail on the head with the question that needed to be asked.
I encourage you to go to the aforementioned Q and the A, read all of it (including the comments inside the spec) and give them some upvotes.
[
{
"operation": "shift",
"spec": {
"timestamp": "timestamp",
// put everything but the timestamp in a member (the pivot)
"*": "all.&"
}
},
{
"operation": "shift",
"spec": {
"all": {
"*": {
// grab the member key and put it in its final place
"$": "values[#2].key",
// grab the member value and put it in its final place
"#": "values[#2].value",
// Walk back two steps (* -> all -> root) and grab the timestamp
"#(2,timestamp)": "values[#2].timestamp"
// I wish I understood the logic behind "#2" but I don't
// and I'll have to read on it further
}
}
}
}
]
I hope someone can explain what # is for. My immediate guess is that it is like & (the member name) but it looks like it is the member position (?).
How should i design a Restful API for PATCH operation that support update some property in the a list with condition?
say i have following json model:
{
"key1": "value",
"key2": "value",
"list": [
{
"property": "someValue",
"toBePatched": "value"
},
{
"property": "otherValue",
"toBePatched": "value"
}
]
}
I need to patch the "toBePatched" property in the list when the "property" equals to "someValue". By looking at the json patch here, i think it is a good way to go, but i dont think the json pointer supports the query? how should i define a path that supports "/list/property=someValue/toBePatch"?
One stupid way to do it is to pass it as query parameter to the api, and have some logic around it, but i dont think thats a standard way to do it.
[
{ "op": "test", "path": "/list/0/property", "value": "someValue"},
{ "op": "test", "path": "/list/0/toBePatched", "value": "value"},
{ "op": "replace", "path": "/list/0/toBePatched", "value": "the-new-value"}
]
test is important, it lets you verify that the server hasn't change the part of the document that you are intending to change. See section 5 on Error Handling for details.
Using apache nifi, I want to get the all the input twitter files which have the hashtags for "tech"
The input json is
{
"created_at": "Sun Mar 25 18:00:43 +0000 2018",
"id": 977968537028481025,
"id_str": "977968537028481025",
"text": "#bby__nim You know like datttt",
"entities": {
"hashtags": [
{
"text": "tech",
"indices": [
12,
17
]
},
{
"text": "BusinessPlan",
"indices": [
48,
61
]
}
],
"urls": [
],
"user_mentions": [
{
"screen_name": "bby__nim",
"name": "bbynim\ud83d\udc7d",
"id": 424356807,
"id_str": "424356807",
"indices": [
0,
9
]
}
],
"symbols": [
]
},
"favorited": false,
"retweeted": false,
"filter_level": "low",
"lang": "en",
"timestamp_ms": "1522000843661"
}
Under EvaulateJsonPath, validating whether the hashtags is present or not using $.{entities.hashtags:jsonPath('$[0]')}, which is successfully validating
But in RouteOnAttribute, can someone tell me how to validate whether the entities.hashtags has the value called tech?
Use the evaluate json path processor configs as shown below,
Now we are extracting all the text values from the hashtags array and keeping as flowfile attribute.
In addition you have to change the properties Destination to flowfile-attribute and Return Type as Json
Now we are having all the text values in an array then use RouteOnAttribute processor to validate the entities.hastags attribute having a tech value in it (or) not.
RouteOnAttribute configs:-
Add a new property as
contains tech
${entities.hashtags:contains("tech")} //checks as sub string if the array having tech
We are using contains function in our expression language will evaluate the array having tech substring in it or not.
But we need to check the values in the array so use the below expression language for that
contains tech
${anyDelineatedValue("${entities.hashtags:replace('[',''):replace(']','')}",","):equals('"tech"')} //checks values in the array
we are using anyDelineatedValue,replace,equals functions in our expression language will evaluate the array having tech values in it or not.
In addition if you want to check the first text value in hashtags array then your evaluatejson path would be
entities.hashtags
$.entities.hashtags[0].text
I have two tables with Many-To-Many mappings. Tables are content and tag. So, I have another table content_tag to normalize the many-to-many relationship. But, I am having problem in pagination. As example, when I am fetching a tag by name it returns a single tag object, but with multiple content object nested inside. I know how to do pagination for tag, but my question is how can I make pagination for the nested content object in a single tag object. Please see my result below, which I am getting from POSTMAN.
{
"id": 12,
"tag": "Viral",
"contents": [
{
"id": 15,
"idHide": "0",
"listCategory": {
"id": 11,
"title": "Dramai",
"titleBn": "ড্রামাই",
"status": "active"
},
"title": "Cat",
"titleBn": "#99",
"brief": "uytyyyy",
"briefBn": "#495",
"highlight": "0",
"dim": "0",
"sticky": "0",
"status": "active",
"createdAt": "Jan 24, 2018 3:08:34 PM",
"listTag": [
{
"id": 12,
"title": "Viral"
},
{
"id": 13,
"title": "Progress"
},
{
"id": 14,
"title": "Limit"
}
]
}
]
}
Please note contents tag in response is a list of content object for viral tag. Sometimes contents array size gone over 50 objects. So, I want to implement pagination for that. How can I implement pagination for content?
I have a JSON file contact.txt that has been parsed into an object called JSONObj that is structured like this:
[
{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumbers": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
},
{
"firstName": "Mike",
"lastName": "Jackson",
"address": {
"streetAddress": "21 Barnes Street",
"city": "Abeokuta",
"state": "Ogun",
"postalCode": "10122"
},
"phoneNumbers": [
{ "type": "home", "number": "101 444-0123" },
{ "type": "fax", "number": "757 666-5678" }
]
}
]
I envision editing the file/object by taking in data from a form so as to add more contacts. How can I do this?
The following method for adding a new contact to the JSONObj's array doesn't seem to be working, what's the problem?:
var newContact = {
"firstName": "Jaseph",
"lastName": "Lamb",
"address": {
"streetAddress": "25 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "13021"
},
"phoneNumbers": [
{ "type": "home", "number": "312 545-1234" },
{ "type": "fax", "number": "626 554-4567" }
]
}
var z = contact.JSONObj.length;
contact.JSONObj.push(newContact);
It depends on what technology you're using. The basic process is to read the file in, convert it to whatever native datatypes (hash, dict, list, etc.) using a JSON parsing library, modify or add data to the native object, then convert it back to JSON and store it to the file.
In Python, using the simplejson library it would look like this:
import simplejson
jsonobj = simplejson.loads(open('contact.txt'))
#python's dict syntax looks almost like JSON
jsonobj.append({
'firstName': 'Steve',
'lastName': 'K.',
'address': {
'streetAddress': '123 Testing',
'city': 'Test',
'state': 'MI',
'postalCode': '12345'
},
'phoneNumbers': [
{ 'type': 'home', 'number': '248 555-1234' }
]
})
simplejson.dump(jsonobj, open('contact.txt', 'w'), indent=True)
The data in this example is hardcoded strings, but it could come from another file or a web application request / form data, etc. If you're doing this in a web app though I would advise against reading and writing to the same file (in case two requests come in at the same time).
Please provide more information if this doesn't answer your question.
In response to "isn't there way to do this using standard javascript?":
To parse a JSON string in Javascript you can either eval it (not safe) or use a JSON parser like this one's JSON.parse. Once you have the converted JSON object you can perform whatever modifications you want to it in standard JS. You can then use that same library to convert a JS object to a JSON string (JSON.stringify). Javascript does not allow file access (unless you're doing serverside JS), so that would prevent you from reading & writing to your contact.txt file directly. You'd have to use a serverside language (like Python, Java, etc.) to read and write the file.
Once you have read in the JSON, you just have an associative array - or rather you have a pseudo-associative array, since this is Javascript. Either way, you can treat the thing as one big list of dictionaries. You can access it by key and index.
So, to play with this object:
var firstPerson = JSONObj[0];
var secondPerson = JSONObj[1];
var name = firstPerson['firstName'] + ' ' + firstPerson['lastName'];
Since you will usually have more than two people, you probably just want to loop through each dictionary in your list and do something:
for(var person in jsonList) {
alert(person['address']);
}
If you want to edit the JSON and save it back to a file, then read it into memory, edit the list of dictionaries, and rewrite back to the file.
Your JSON library will have a function for turning JSON into a string, just as it turns a string into JSON.
p.s. I suggest you observe JavaScript conventions and use camelcase for your variable names, unless you have some other customs at your place of employment. http://javascript.crockford.com/code.html