error passing JSON array to cgi script - javascript

I'm having a javascript passing a json array to a cgi script like below.
getData.cgi?jsontext=JSON.strigify(array)
The array is below.
[ { "attr" : "1",
"id" : "1",
"name" : "John"
},
{ "attr" : "2",
"id" : "2",
"name" : "foobar"
}
]
CGI script(getData.cgi):
#!/usr/bin/perl
use CGI;
my $q = CGI->new();
print $q->header;
print $q->param('jsontext');
The script works when I pass only 1 value in array. It is giving 406 error when I pass 2 or more elements like the one I mentioned above.
What can be the reason? The same script works in my loacalhost with multiple elements of array. Is there any htaccess settings problem?

Related

Can't read my JSON file using require method in JS

I'm trying to loop through JSON data that I'm pulling from a file. I'm using the require function to get the JSON file but it keeps telling me there is an unexpected '{' token? positions on IDE and console are a little off but I'm pretty sure it's the second objects opening { -position 44-
Any suggestions?
myData.json
{
"colour" : "blue",
"cm" : 2
}
{
"colour" : "red",
"cm" : 5
}
index.js
const fileContents = require("./myData.json");
Because the JSON is invalid. Your JSON file needs to define one entity, where currently you're defining two. Even if the code could read the data, there's no logical way to indicate which of those two objects you want to refer to.
It looks like you meant to put them in an array:
[
{
"colour" : "blue",
"cm" : 2
},
{
"colour" : "red",
"cm" : 5
}
]
Then you can import the whole array, and refer to elements of the array for each object.

Mapping values between JSON arrays

I seem to have run into a rather unusual problem. Using a form builder I am able to store and render an admin created form in JSON format like so:
EDIT: this is my pulled data from MongoDB hence the id, i directly push my generated JSON into Mongo, i eliminate the ID part on fetching the data.
{
"_id" : ObjectId("5985608427f8510788b468ab"),
"fdata" : [
{
"type" : "header",
"subtype" : "h1",
"label" : "Date Of Commencement"
},
{
"type" : "paragraph",
"subtype" : "p",
"label" : "The partnership business shall be deemed to have commenced on and from <div><br></div>"
},
{
"type" : "date",
"required" : true,
"label" : "Date Field",
"description" : "Date Of Commencement",
"className" : "form-control",
"name" : "date-1501913203863"
}
]
}
Once it is rendered and values are filled in by a user i receive the values at the back end like so: (NODEJS)
{"date-1501913203863":"2017-08-12"}
Now I have to render the form back but with the filled values and it should look like a filled form. Is there a native way in JSON to do this? I was planning to go ahead with a python script to match the values to the fields but that seemed like overkill for this problem.
I hope to get some advise on this topic and not code. Your help is greatly appreciated!

elasticsearch completion field not deleting

I have a simple user info document which includes a completion field.
Mapping:
"properties" : {
"fname" : { "type" : "string" },
"lname" : { "type" : "string" },
"dob" : { "type" : "string" },
"sex" : { "type" : "string" },
"autocomplete" : {
"type" : "completion"
}
}
This is an example of my document.
"person" : {
"sex" : "Male",
"dob" : "11/11/2014",
"fname" : "Julie",
"lname" : "Thomas",
"autocomplete" : "Julie Thomas"
}
The complete suggestion query works perfectly fine. But when I delete this document, the complete suggestion still exist for "Julie Thomas" when I expect this to be deleted as well.
Any suggestion on what I am doing incorrectly?
Thanks!
I am having this problem too. According to the ElasticSearch documentation, completion suggestions need to be expunged from the index using the Optimize command. This is what they say at the bottom of the Completion Suggester Docs.
The suggest data structure might not reflect deletes on documents
immediately. You may need to do an Optimize for that. You can call
optimize with the only_expunge_deletes=true to only cater for deletes
or alternatively call a Merge operation.
... that said. I did this:
curl -XPOST 'http://server:9200/attribute/_optimize?pretty' -d '{
"only_expunge_deletes": true
}'
and the deleted doc still shows up in the completion suggester, even though it does not show up in queries or filters.
... Sorry. Not much of an answer... It's supposed to be the answer, but it might not work for you, just as it's not working for me.
We might have to move to Index-Time Search, which uses a 1-character ngram and do a normal _search query.
Adding an id field to the data, and then refrencing that id field for delete and update, works for us
"index":{"_index":"esIndex","_type":"document","_id":"23d68f060118fbeee699d14eff044a1f"}}
{"text":"vision zero","id":"23d68f060118fbeee699d14eff044a1f","suggest":{"input":"vision zero","payload":{"entity_id":"23d68f060118fbeee699d14eff044a1f"}},"type":"Content"}
Something like the above bulk query data, notice the id field in the data. If we don't put it, we face same issue that after deleting document, it still show up in suggestions.

How do I create JSON in ExtJS for an AJAX call?

I want to create this parameter file so that I can send it to my web service.
var parms = {
"quiz_id":"120",
"owner_id":"1",
"solver_id":"1",
"answers":
[
{
"answer_text" : "YES",
"question_id" : "1"
},
{
"answer_text" : "NO",
"question_id" : "2"
},
{
"answer_text" : "YES",
"question_id" : "3"
},
{
"answer_text" : "YES",
"question_id" : "4"
},
{
"answer_text" : "YES",
"question_id" : "5"
}
]
};
I am stuck with the contents inside of the answers. I don't know how to create it dynamically.
for serializing Javascript objects to JSON strings either you can use
JSON.stringify(Object);
which is available in most of the latest browers, else you can use ExtJS inbuilt method
Ext.encode(Object);
and for deserializing JSON string you can use JSON.parse(JSONString) or Ext.decode(JSONString)
An easy way to do this is to create your data as a javascript object and then use a Json "stringifier" to turn it into a json string, which can then be passed to your server.
This same problem was answered previously at Serializing an object to JSON
If you use jquery (and I highly recommend it as a very useful tool for all serious javascript programmers), there is a nice plugin that I use for passing json back and forth in Ajax calls. See http://code.google.com/p/jquery-json/
Create an object with array and some objects inside and then look at the Ext.data.proxy.Server.encodeFilters() method.

Nested Maps in JQuery ajax data to send - how to serialize 'properly'?

var location = { "location" : {
"name" : $("#user_loc_name").val(),
"street_address" : $("#user_loc_street_address").val(),
"city" : $("#user_loc_city").val(),
"province" : $("#user_loc_province").val(),
"country" : $("#user_loc_country").val(),
"postal_code" : $("user_loc_postal_code").val(),
"public" : $("#user_loc_public").attr('checked')
}};
( ... )
$.post(url, location, success_callback);
The reason I need this 'nested' map is because I'm sending this to my rails backend, and I'm hoping I can make a simple update _ attributes(params[:location]) in the controller. Unfortunately, with this solution I get Parameters:
{"location"=>"[object Object]", ...}
Not what I'm hoping for. I'm hoping for:
{"location"=> {"name" => "valforname", "street_address" => "valforstreetadress", ...}, <other params>...}
If I get rid of the 'nesting' and just send the inner map it works fine, but each attribute shows up separately in the params hash and it's just cumbersome and messy. If I could get the whole map nested under a key of "location" it would be much nicer.
Have a look at this:
Serializing Objects in Javascript
jQuery does not support JSON serializing OOTB. Try any number of libraries. Here's the standard one:
<script src="http://www.json.org/json2.js"></script>
Your code then looks like:
$.post(url, JSON.stringify(location), success_callback);

Categories