Parse a JSON object using ASPjson - vbscript

In Classic ASP (VBScript) I can do a general request of POST using request.form or GET using request.querystring which would give me the entire string that was sent.
But, I now need to receive a JSON object from a client side location.
This is an example of what it might look like:
{
"firstName": "John",
"lastName" : "Smith",
"age" : 25
}
How do I request this entire object (which I will then be parsing with ASP.JSON)?
PS: I know that I can probably convert the JSON object to a string on the client side, and then parse as text on the server side, but that feels like a work-around rather than a straight solution.

First of all, I wouldn't use that AspJson, but this: https://github.com/rcdmk/aspJSON
Second, you're not receiving an object per se, but request that contains a "string version of the json object". In this case, probably bytes, thats why you're going to BinaryRead and convert it into a body first.
You then will be able to parse the body with any parser you want.
Now let's try to give you an example code:
<%Response.LCID = 1033%>
<!--#include file="__jsonObject.class.v3.8.1.asp" -->
Set UTF8Enc = CreateObject("System.Text.UTF8Encoding") ' .NET COMPONENT, required on the server app pool
Set JSON = new JSONobject
lngBytesCount = Request.TotalBytes
request_body = UTF8Enc.GetString(Request.BinaryRead(lngBytesCount))
Set request_json = JSON.parse(request_body)
first_name = request_json("firstName")
last_name = request_json("lastName")
age = request_json("age")

Related

How to parse a json with dynamic property name in OIC?

I need to consume and parse incoming json from a third party system in my code. I used RestTemplate to do it. So the response from the system looks like below.
{ "data": { "05AAAFW9419M11Q": { "gstin": "05AAAFW9419M11Q", "error_cd": "SWEB_9035", "message": "Invalid GSTIN / UID" } } }
Now the problem is the property name ("05AAAFW9419M11Q" in this case) in dynamic and in the next response it would be another string. In this case, how can I parse this json as this is not fixed in Oracle Integration Cloud? Response wrapper is not capturing the data apart from the one that is used for configuring the adapter which is fair enough as fieldname itself is changing.
Is there is any workaround for this?
You will have to go to PL/SQL and dynamic SQL, and if it's always the value of gstin entry, you can get the path of the key with
select '$.data.' ||
json_query(js_column, '$.data.*.gstin') into v_key path from table_with_json_column where ... conditions... ;
(assuming there is only 1 "data" per JSON payload) to later build a dynamic query based on json_table.

Mapping Nest response to a C# object

I am trying to map a nest response to a c# object and having little luck.
My query returns the result from
var resp = Eclient.Search<dynamic>(q => q
.Type("movies")
.From(0)
.Size(20)
.QueryRaw(queryString));
How can I map the response to a C# object?
It needs to be dynamic, i.e there are arrays that wary in length in the response.
Serialize a dynamic object to JSON and then deserialize a string to a proper C# object. But that would be totally inefficient. Why not pass a dynamic object around or just do manual mapping?

In Nest (Elasticsearch), how can I get the raw json mapping of an index?

I want to check the discrepancies between my current mapping (as in my C# code) and the mapping in the elasticsearch index.
With only:
var res = esClient.GetMapping<EsCompany>();
I get GetMappingResponse object in c#, I will have to compare field by field for equality. Even worse, each field has their own properties, I have to descend into those properties for further comparison.
In my application, I prefer obtaining the raw json of the mapping, and I can easily diff two json objects for equality.
I then tried this:
var res = esClient.Raw.IndicesGetMapping(myIndexName);
But when I read res.Response, I get an AmbiguousMatchException exception.
When you connect to Elasticsearch you can choose to expose the raw response like this:
var client = new ElasticClient(new ConnectionSettings().ExposeRawResponse());
Then you should be able to access the raw json via:
var json = res.ConnectionStatus.ResponseRaw;

How can I change the column name of an existing Class in the Parse.com Web Browser interface?

I couldn't find a way to change a column name, for a column I just created, either the browser interface or via an API call. It looks like all object-related API calls manipulate instances, not the class definition itself?
Anyone know if this is possible, without having to delete and re-create the column?
This is how I did it in python:
import json,httplib,urllib
connection = httplib.HTTPSConnection('api.parse.com', 443)
params = urllib.urlencode({"limit":1000})
connection.connect()
connection.request('GET', '/1/classes/Object?%s' % params, '', {
"X-Parse-Application-Id": "yourID",
"X-Parse-REST-API-Key": "yourKey"
})
result = json.loads(connection.getresponse().read())
objects = result['results']
for object in objects:
connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()
objectId = object['objectId']
objectData = object['data']
connection.request('PUT', ('/1/classes/Object/%s' % objectId), json.dumps({
"clonedData": objectData
}), {
"X-Parse-Application-Id": "yourID",
"X-Parse-REST-API-Key": "yourKEY",
"Content-Type": "application/json"
})
This is not optimized - you can batch 50 of the processes together at once, but since I'm just running it once I didn't do that. Also since there is a 1000 query limit from parse, you will need to do run the load multiple times with a skip parameter like
params = urllib.urlencode({"limit":1000, "skip":1000})
From this Parse forum answer : https://www.parse.com/questions/how-can-i-rename-a-column
Columns cannot be renamed. This is to avoid breaking an existing app.
If your app is still under development, you can just query for all the
objects in your class and copy the value of the old column to the new
column. The REST API is very useful for this. You may them drop the
old column in the Data Browser
Hope it helps
Yes, it's not a feature provided by Parse (yet). But there are some third party API management tools that you can use to rename the fields in the response. One free tool is called apibond.com
It's a work around, but I hope it helps

How to post json data using West Wind in VFP

There's a document from West Wind says you can post data using application/json format:
https://west-wind.com/webconnection/docs/_2110q21c9.htm
But when using it, actually it'll do a http get instead of post:
DO wwhttp
oHTTP=CREATEOBJECT("wwHttp")
oHTTP.nHTTPPostMode = 8
oHttp.cContentType = "application/json"
oHTTP.AddPostKey("name","somename")
lcURL = "https://mywebserver.com/"
lcXML = oHTTP.HTTPGet(lcURL)
If using nHTTPPostMode = 1 or 2, the http request parameters are not correctly formatted as json. If I change to 4, it's using Get instead of Post again. Is there anyway to get around this?
When you post JSON data you need to post a JSON document not key value pairs as you are doing in your example. Essentially you need to provide the full content - the entire JSON document as a string - that you're sending to the server (just like the example shows).
To post a JSON document looks like this:
DO wwHttp
oHTTP=CREATEOBJECT("wwHttp")
oHttp.cContentType = "application/json"
lcJson = "{ name: 'somename' }"
oHTTP.AddPostKey(lcJson) && this sets the entire post buffer
lcURL = "https://mywebserver.com/"
lcJson = oHTTP.HTTPGet(lcURL)
Using name value pairs only works with POST buffers for URLEncoded or Multipart documents - every other type of content is sent as raw data that you have to provide.
in version 6 and later you can also use the following simpler syntax to .Post data:
oHttp.cContentType = "application/json"
lcJson = oHttp.Post(lcUrl, lcJson)
If you want to create JSON documents under program control you can serialize data using the wwJsonSerializer class (also part of Client Tools). It can serialize from objects, arrays/collections and cursors.

Resources