My json:
{ "question_1":
{ "type" : "string"
, "title" : "1. 1. What did he want to make for dinner?"
, "enum":
[ " a.) He wanted to make some salad and spaghetti"
, " b.) He wanted to make some pasta salad"
]
, "required": false
}
, "question_2":
{ "type": "string"
, "title": "2. 2. Did he have the ingredients to make dinner?"
, "enum":
[ " a.) Yes, he had the ingredients"
, " b.) No, he didn't have the ingredients"
]
, "required": false
}
, "question_3":
{ "type" : "string"
, "title" : "3. 3. Where did he go shopping?"
, "enum":
[ " a.) He went to Albertsons"
, " b.) He went to Albertos"
]
, "required": false
}
}
in my json there are many numbers next to each other and are duplicated
Eg:
1. 1. => 1.
2. 2. => 2.
3. 3. => 3.
and so on
How can i remove this duplication?
I want to remove duplicate numbers next to each other in json
You can replace it using a regex. Using grouping references (the parenthesis around (\d) form a group of one decimal and the \1 refers to it) will make sure that you don't remove numbers like "1. 2." and allow you to refer to it during replacement:
const data =
{ "question_1":
{ "type" : "string"
, "title" : "1. 1. What did he want to make for dinner?"
, "enum":
[ " a.) He wanted to make some salad and spaghetti"
, " b.) He wanted to make some pasta salad"
]
, "required": false
}
, "question_2":
{ "type": "string"
, "title": "2. 2. Did he have the ingredients to make dinner?"
, "enum":
[ " a.) Yes, he had the ingredients"
, " b.) No, he didn't have the ingredients"
]
, "required": false
}
, "question_3":
{ "type" : "string"
, "title" : "3. 3. Where did he go shopping?"
, "enum":
[ " a.) He went to Albertsons"
, " b.) He went to Albertos"
]
, "required": false
}
};
Object.values(data).forEach(v => v.title = v.title.replace(/(\d)\. \1\./, '$1.'))
console.log(Object.values(data).map(v => v.title))
If it actually is JSON, which you are going to parse using JSON.parse, you could make use of the optional reviver parameter.
reviver
If a function, this prescribes how each value originally produced by
parsing is transformed before being returned. Non-callable values are
ignored. The function is called with the following arguments:
key
The key associated with the value.
value
The value produced by parsing.
const JSONString = `{
"question_1": {
"type": "string",
"title" : "1. 1. What did he want to make for dinner?"
},
"question_2": {
"type": "string",
"title": "2. 2. Did he have the ingredients to make dinner?"
}
}`;
console.log(
JSON.parse(JSONString, (key, value) => typeof value === 'string' ? value.replace(/(\d)\. \1\./, '$1.') : value)
)
Expanding on Moritz Ringler's solution.
Related
Is it possible to make a JSON Schema which validates that an array contains at least 1 instance of three non-overlapping types. A simple example is an array of numbers of any length that contains at least one 1, one 2, and one 3.
I can't use allOf because that will validate that every number in the array is a 1, 2, and 3 - which no number can be 😅
I can't use anyOf because then I can have an array with only one element 😢
I can't use oneOf because, again, I can have an array with one number
(...also I'm unclear if I should be working with the contains key or inside the items key)
What I'm looking for is a way to say "one of each" of these items. Is it possible with JSON Schema?
Here's a broken example with oneOf
{
"type": "object",
"properties": {
"numbers": {
"type": "array",
"contains": {
"oneOf": [
{
"const": 1
},
{
"const": 2
},
{
"const": 3
}
]
},
"items": {
"type": "number"
}
}
}
}
[1] – Should NOT validate
[1,2] - Should NOT validate
[1,2,3] - SHOULD validate
[1,2,3,4] - SHOULD validate
You can put the contains inside of an allOf:
{
"type": "object",
"properties": {
"numbers": {
"type": "array",
"allOf": [
{
"contains": {
"const": 1
}
},
{
"contains": {
"const": 2
}
},
{
"contains": {
"const": 3
}
}
]
}
}
}``
I need help with prefix autocomplete suggestions from elasticsearch 7.3 documents with a multiple fields search and an associated field type in the output to categorize the suggested term type.
tried with search_as_you_type field type and adding multiple completion analyzers with no luck so far. I wanted a solution that is very easy to implement, eliminates duplicates and reduce my memory footprint. Something like a Redis sorted set.
Here is my el indexed docs and expected auto-complete results.
{
"id": "7cBLdyLTkPNTC8c1Ntq",
"tech": {
"body": "Nikon D600",
"gear": "Nikon AF-S Nikkor 16-35mm f/4 ED VR"
},
"artist": {
"name": " Hilton Paris"
},
"details": {
"place": "Nepal",
"text": "Mount Everest .Natures mirror",
"tags": [
"mountain",
"rock",
"nature",
"wooden trail",
"mirror",
"past and furious"
],
"score": 0.87
},
"createdDate": {
"_seconds": 1566260863,
"_nanoseconds": 12000000
}
}
{
"id": "8cBLdyLTkPNTC8c1Ntq",
"tech": {
"body": "Panasonic Lumix DC-S1R Mirrorless",
"gear": "Panasonic Lumix S PRO 50mm"
},
"artist" : {
"name": "Jhon Gabriel"
},
"details": {
"place": "paris",
"text": "mirror lake reflection",
"tags": [
"lake",
"rock",
"nature",
"water"
],
"score": 0.87
},
"createdDate": {
"_seconds": 1566260863,
"_nanoseconds": 12000000
},
}
search for "pa" should give the following output.
output [ {
"type" :"artist",
"value" : "Paris Peace",
},
{
"type" :"body",
"value" : "Panasonic",
},
{
"type" :"gear",
"value" : " Panasonic Lumix S PRO 50mm"
},
{
"type" :"place",
"value" : "paris"
},
{
"type" : "tag",
"value" : "past and furious"
}
]
What i need is
for a given input prefix e.g 'pa', get all the matching terms from the elsticsearch datastore with the following conditions.
input satisfies a case insensitive prefix to one of the following fields
1.1 "tech.body" ---> token level prefix matching, output only the token value
1.2 "tech.gear" ---> token level prefix matching, output full string value of the filed.
e.g if tehc.gear has a token with 'pa' as the prefix,
output should be the full value of this field ,like "Panasonic Lumix S PRO 50mm"
1.3 "artist.name" --> token level prefix matching
1.4 "details.place" --> token level prefix matching
1.5 "details.tags" --> token level prefix matching, output full string value of the filed, eg. pa--> "past and furious"
1.6 "details.text" --> token level prefix matching
Any help here is highly appreciated, as this is turning out to be a blocker for an upcoming release.
I am trying to upload my intent everything is working fine, i am writing script in V2 and everything works but for parameter webhookState data type is enum ( [here] ) and we have to enter following of the three values ( [here][1]) now when i enter any one and try to upload it shows.
" Unable to load file: SyntaxError: Unexpected token W in JSON at position 98 "
now when I pass WEBHOOK_STATE_ENABLED(or any one) in quotes code uploads successfully (because as per my knowledge it treats it as string and gets executed) and intent is created for my agent but webhook remains off and also training phrases are not there, i am certain that the parameter webhookState is not being activated when i pass in quotes(because of above mentioned reason) Same is the case with rest, like when i try to put in type of training phrases.
{
"name": "Warehouse_Management",
"displayName": "Warehouse_Management",
"webhookState": "WEBHOOK_STATE_ENABLED_FOR_SLOT_FILLING" ,
"priority": 50000,
"isFallback": false,
"mlDisabled": false,
"trainingPhrases": [
{
"name":"Try1" ,
"type": "EXAMPLE",
"parts": [
{
"text": "for",
"userDefined": true
},
{
"text": "warehouse",
"entityType": "#Properties",
"alias": "Properties",
"userDefined": true
},
{
"text": "management",
"userDefined": true
}
]
},
{
"name":"Try2" ,
"type": "EXAMPLE",
"parts": [
{
"text": "i want app for ",
"userDefined": true
},
{
"text": "warehouse",
"alias": "Properties",
"entityType": "#Properties",
"userDefined": true
}
]
}
],
"outputContexts": [
{
"name": "Yes",
"lifespanCount": 2
},
{
"name": "No",
"lifespanCount": 2
},
{
"name": "Device_Integration",
"lifespanCount": 2
}
],
"resetContexts": false,
"parameters": [
{
"name": "Properties",
"displayName": "Properties",
"value": "$parameter_name",
"entityTypeDisplayName": "#Properties",
"mandatory": false,
"isList": true
}
],
"messages": [
{
"text":"This is sample response"
}
],
"rootFollowupIntentName": "root",
"parentFollowupIntentName": "parent"
}
Please NOTE that in the attached Json i have put it in quotes so it would get successfully executed and will create intent.
In order for it to be valid JSON, the enum value should be wrapped in quotes. It's expecting the enum value as a String.
I am working with the Code Canyon script Spin2Win (https://codecanyon.net/item/spin2win-wheel-spin-it-2-win-it/16337656), which uses a local JSON file to create the segments.
The part of the JSON file that control the segments has the following format.
"segmentValuesArray" : [
{"probability":10, "type": "string", "value": "$10^Coupon", "win": true, "resultText": "You won a $10 Coupon", "userData": {"score":0}},
{"probability":30, "type": "string", "value": "Lose", "win": false, "resultText": "Thank you for Playing", "userData": {"score":0}},
{"probability":5, "type": "string", "value": "$25^Coupon", "win": true, "resultText": "You won a $25 Coupon", "userData": {"score":0}},
{"probability":30, "type": "string", "value": "Lose", "win": false, "resultText": "Thank you for Playing", "userData": {"score":0}},
{"probability":2, "type": "string", "value": "Free Spin", "win": true, "resultText": "You Won a Free Spin", "userData": {"score":0}},
{"probability":30, "type": "string", "value": "Lose", "win": false, "resultText": "Thank you for Playing", "userData": {"score":0}},
{"probability":3, "type": "string", "value": "SWAG", "win": true, "resultText": "You won SWAG", "userData": {"score":0}},
{"probability":30, "type": "string", "value": "Lose", "win": false, "resultText": "Thank you for Playing", "userData": {"score":0}},
{"probability":1, "type": "string", "value": "GOOGLE^Home", "win": true, "resultText": "You won a Google Home", "userData": {"score":0}}
],
My issues is I have a $50 and $100 coupon as well that will have lower probability values. But I only ever want 2 coupons on the wheel at any given time. So my thought was to pick a coupon value at random and then use a find and replace to update to the two coupon segements before the spin script processes the json.
I have tested and I can add an ID tag to the segmentValueArrary such as follows:
{"id":"01", "probability":10, "type": "string", "value": "$10^Coupon", "win": true, "resultText": "You won a $10 Hosting Coupon", "userData": {"score":0}},
I have found this answer: JSON Object array inside array find and replace in javascript
But it only gives an example of how to replace one value and I need to replace the probability, value and the resultText Any attempts to modify the code find and replace code has failed.
***** UPDATE #1 *****
I have played around with a little bit of code and gotten to work in Jsfiddle. (http://jsfiddle.net/6347s9bo/)
The issues I am running into now is how the developer loads and processes the JSON. Here is his code
loadJSON(function(response) {
// Parse JSON string to an object
var jsonData = JSON.parse(response);
........
And here is his loadJSON function
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', './wheel_data.json', true);
xobj.onreadystatechange = function() {
if (xobj.readyState == 4 && xobj.status == "200") {
//Call the anonymous function (callback) passing in the response
callback(xobj.responseText);
}
};
xobj.send(null);
}
The error I am getting now is object.map is not a function. I am placing my findAndReplace() call after his JSON.parse call, which should turn the text into an object.
Not sure what I am missing
you could try this:
function changeAttributes(objArray, objId, probability, value, resultText) {
objArray.forEach(function(obj) {
if (obj.id === objId) {
obj.probability= probability;
obj.value = value;
obj.resultText = resultText;
}
});
}
The objId is the Id of the object in the Array you want to replace.
The 3 values are the new values you want to set.
You have to call the method 2 times, one time for every coupon to replace the attribuites.
Not sure to understand your question exactly. JSON is basically a javascript array. So updating it do not require to convert it to a string. Finding a value in an array is pretty simple. In your case, simply create a function like this :
function isCoupon(item) {
return item.id === '01';
}
then search it like this :
arrayItem = segmentValuesArray.find(isCoupon);
arrayItem.value.replace('10^','100^');
So I would iterate over the list and mutate the coupons accordingly. You will have to add extra logic to handle selecting which coupon at random you are selecting but I think this gives a place to start.
segmentValuesArray.forEach(segment => {
if (segment.value.includes("Coupon")) {
segment.probability = 0;
segment.resultText = '';
}
});
reference on includes:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
reference on forEach:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
I need to define a JSON for the following requirement;
Company company : Object (Name, Id)
User user – Object (Name, Age)
List<CustomObj> serviceList
o Service service
o String mode
o Boolean flag1
o Boolean flag2
I have tried something like;
{
Company : { "name" : "Company1", "id" : 123 },
User : {"name" : ["PV","PR", "DM", "TN"], "age" : null},
serviceList :
{
Service: {},
"mode" : null,
"flag1" : null;
"flag2" : null
}
}
The reason I have null values in a few places is because there are 2 modes; Create and Edit..
So in Create mode, some of the fields would have null values...
But you can let me know generically if it is correct?
Here's the answer before your edit, see below for a follow-up:
company looks like a simple object:
{"Name": "the name", "Id": "the ID"}
As does user:
{"Name": "the name", "Age": "the age"}
serviceList looks like an array of objects:
[
{
"service": {},
"mode": "the mode",
"flag1": true,
"flag2": false
},
...
]
You haven't shown what Service should look like, so I've just used {} above; it looks like it would be an object. The ... in the serviceList isn't literal, it's meant to indicate you'll probably repeat the thing above it.
So pulling those all together into a single JSON document:
{
"company": {"Name": "the name", "Id": "the ID"},
"user": {"Name": "the name", "Age": "the age"},
"serviceList": [
{
"service": {},
"mode": "the mode",
"flag1": true,
"flag2": false
},
...
]
}
Again, the .. in the serviceList is not literal, it indicates that you'll probably repeat the object above it.
You edited your question to add the
I have tried something like;
and the following. As you can tell from my original answer, I'd say the serviceList would be an array, not a single object, because List generally means exactly that: A list of something, which in JSON would be an array. You've also used lower-case identifiers where whatever it is you're quoting that you have to replicate is using initially-capped identifiers. JSON is case-sensitive, so that could be a problem.