How to get values from json string with aspJSON.asp? [duplicate] - json

This question already has answers here:
ASP JSON: Object not a collection
(2 answers)
Closed 2 years ago.
I have this JSON and no matter what I try I can´t get any values from it. I don´t know what Im missing?
This is the json string.
{
"groups": [{
"name": "Credit Card",
"types": ["mc", "visa", "amex"]
}],
"paymentMethods": [{
"brands": ["mc", "visa", "amex"],
"details": [{
"key": "encryptedCardNumber",
"type": "cardToken"
}, {
"key": "encryptedSecurityCode",
"type": "cardToken"
}, {
"key": "encryptedExpiryMonth",
"type": "cardToken"
}, {
"key": "encryptedExpiryYear",
"type": "cardToken"
}, {
"key": "holderName",
"optional": true,
"type": "text"
}],
"name": "Card",
"type": "scheme"
}, {
"name": "Få först. Betala sen med Klarna.",
"supportsRecurring": true,
"type": "klarna"
}, {
"name": "Trustly",
"supportsRecurring": true,
"type": "trustly"
}, {
"name": "Swish",
"supportsRecurring": true,
"type": "swish"
}, {
"name": "Paysafecard",
"supportsRecurring": true,
"type": "paysafecard"
}, {
"name": "Dela upp med Klarna.",
"supportsRecurring": true,
"type": "klarna_account"
}, {
"name": "Pay now with Klarna.",
"supportsRecurring": true,
"type": "klarna_paynow"
}]
}
And then I try to get one single value(the "Credit Card") and also loop through all the paymetsMethods.details.key values and then loop all the last "name".
<!--#include file="aspJSON1.17.asp" -->
jsonstring = CStr(objXmlHttp.ResponseText)
'response.write "---"&jsonstring &"---"
Set oJSON = New aspJSON
oJSON.loadJSON(jsonstring)
groupsname=oJSON.data("groups").item("name")
response.write groupsname
For Each x In oJSON.data("paymentMethods").item("details")
paymentMethods=oJSON.data("paymentMethods").item("details").item("key")
response.write paymentMethods
NEXT
For Each x In oJSON.data("name")
thename=oJSON.data.item("name")
response.write thename
NEXT
Im clearly missing something, but what?

When parsing JSON if you come across a collection (i.e. an Array) you have to iterate through it before trying to retrieve values deeper in the JSON structure.
In this case, to get to the key property you have to iterate through two collections.
paymentMethods
details
So this For loop;
For Each x In oJSON.data("paymentMethods").item("details")
paymentMethods=oJSON.data("paymentMethods").item("details").item("key")
response.write paymentMethods
NEXT
Should look more like;
Dim paymentMethod, paymentMethodsItem, details, detailsItem
For Each paymentMethodsItem In oJSON.data("paymentMethods")
Set paymentMethod = oJSON.data("paymentMethods").item(paymentMethodsItem)
For Each detailsItem In paymentMethod.data("details")
Set details = paymentMethod.data("details").item(detailsItem)
Call Response.Write(details.item("key"))
Next
Next
#Code provided untested
Useful Links
Looping though JSON classic ASP
ASP JSON: Object not a collection

Related

IF statement in Couchbase Map of view - I'm sure I'm missing something simple

I'm trying to limit the map in my view to a specific set of documents by either having the id "startsWith" a string or based on there being a specific node in the JSON> I can't seem to get a result set once I add an IF statement. The reduce is a simple _count:
function(doc, meta) {
if (doc.metricType == "Limit_Exceeded") {
emit([doc.ownedByCustomerNumber, doc.componentProduct.category], meta.id);
}
}
I've also tried if (doc.metricType) and also if(meta.id.startsWith("Turnaway:")
Example Doc:
{
"OvidUserId": 26105400,
"id": "Turnaway:00005792:10562440",
"ipAddress": "111187081038",
"journalTurnawayNumber": 10562440,
"metricType": "Limit_Exceeded",
"oaCode": "OA_Gold",
"orderNumber": 683980,
"ovidGroupID": 3113900,
"ovidGroupName": "tnu999",
"ovidUserName": "tnu999",
"ownedByCustomerNumber": 59310,
"platform": "Lippincott",
"samlString": "",
"serialName": "00005792",
"sessionID": "857616ee-dab7-43d0-a08b-abb2482297dd",
"soldProduct": {
"category": "Multidisciplinary Subjects",
"name": "Custom Collection For CALIS - LWW TA 2020",
"productCode": "CCFCCSI20",
"productNumber": 33410,
"subCategory": "",
"subject": "Multidisciplinary Subjects"
},
"soldToCustomer": {
"customerNumber": 59310,
"keyAccount": false,
"name": "Tongji University"
},
"turnawayDateTime": "2022-05-04T03:01:44.600",
"usedByCustomer": {
"customerNumber": 59310,
"keyAccount": false,
"name": "Tongji University"
},
"usedByCustomerNumber": 59310,
"yearMonth": "202205"
},
"id": "Turnaway:00005792:10562440"
}
Thanks,
Gerry
Found it (of course after posting the question) The second component of the Key in the emit has to exist. I entered doc.componentProduct.category instead of doc.soldProduct.Category.

How to POST a record with fk data in Strapi?

I have a problem when I try to POST a new record.
I have a legacy app that I'm consuming with Strapi, so I didn't let Strapi create the tables, it just only use what is on the DDBB.
This one of the collection type: (enfermedadrepeticion) (go to repeticion_id)
{
"kind": "collectionType",
"connection": "atdbconnection",
"collectionName": "enfermedadrepeticion",
"info": {
"name": "RepeticionEnfermedad"
},
"options": {
"increments": false,
"timestamps": false
},
"attributes": {
"valor": {
"type": "decimal"
},
"enfermedad_id": {
"type": "integer"
},
"valor_1": {
"type": "string"
},
"valor_2": {
"type": "string"
},
"fechaTomaDato": {
"type": "string"
},
"repeticion_id": {
"via": "repeticion_enfermedads",
"model": "repeticion"
}
}
}
this is the model "repeticion" ... go to repeticion_enfermedads...
{
"kind": "collectionType",
"connection": "atdbconnection",
"collectionName": "repeticiones",
"info": {
"name": "Repeticion"
},
"options": {
"increments": false,
"timestamps": false
},
"attributes": {
"activa": {
"type": "boolean"
},
"altura": {
"type": "string"
},
"linea_id": {
"model": "linea"
},
"ensayo_id": {
"type": "integer"
},
"esp": {
"type": "string"
},
... bunch of fields ...
"repeticion_enfermedads": {
"collection": "repeticion-enfermedad",
"via": "repeticion_id"
}
}
}
My relation is: One Repeticion have 0 or N repeticion-enfermedad, via repeticion_id field.
Using this relation, when I fetch data from "Repeticion" I get one or more "Repeticion-Enfermedad".
So, when I need to update una entry in "Repeticion-Enfermedad" I PUT the data with this body :
body {
enfermedad_id: 1,
fechaTomaDato: '2021-05-05 03:29:29',
fechaUltCambio: '2021-05-05 03:29:29',
repeticion_id: { id: 392571 },
valor: 60,
valor_1: '60',
valor_2: 'S'
}
and everything works fine!!
But when I try to create a record using POST, with the same body, I get a 500 error.
error Error: ER_NO_DEFAULT_FOR_FIELD: Field 'repeticion_id' doesn't have a default value
I try to send the body using Swagger, and I get the same error.
I try, sending "repeticion_id" field, using integer, string, object, etc, but I cant't make it work...
My Strapi version is 3.0.6
I really don't want to alter the model definition, it's working fine...
Any suggestion ??
Best Regards
OK, I found the problem.
There is nothing to do with Strapi, I have to add NOT NULL to the field in RepeticionEnfermedad table.
It looks like Strapi first tries to create a record in that table, and then updates the recently added record...
I leave my answer in case someone got the same problem.
Chiao!

JSON get value without using position in array

How do I get the value linked to number inside of properties? I would just use properites[0] but the order of Number and Order Date can sometimes change in my scenario.
{
"properties": [
{
"name": "Number",
"value": "1206327198"
},
{
"name": "Order Date",
"value": "2018-07-16"
}
]
}
Using python - loop over the properties and find a match. Once a match is found - stop the loop.
data = {
"properties": [
{
"name": "Number",
"value": "1206327198"
},
{
"name": "Order Date",
"value": "2018-07-16"
}
]
}
for prop in data['properties']:
if prop.get('name') == 'Number':
print(prop.get('value'))
break
output
1206327198

How to add tag inside a nest depending on a variable while keeping rest of json

I'm failry new to jq and I've learned to do most things on my own but I'm hitting my head on my keyboard for this one. Look at the following json
JSON:
{
"importType": "Upsert",
"immediateDeployment": false,
"isIgnoreNulls": false,
"isOverwriteNullsOnly": false,
"isPreferredandSync": false,
"outputLayout": {
"fields": [
{
"name": "TEMP_KEY",
"type": "String",
"length": "2000",
"displayName": "TEMP_KEY"
},
{
"name": "LoadSeqNum",
"type": "String",
"length": "2000",
"displayName": "LoadSeqNum"
}
]
}
}
What I'm trying to do is inside of .outputLayout.fields[] I want to create a new pair called "isIdentifier" where it is true if the .outputLayout.fields[].name is LoadSeqNum and false if it's not but I need to keep the rest of the json just as it is. So target should look as following:
Goal:
{
"importType": "Upsert",
"immediateDeployment": false,
"isIgnoreNulls": false,
"isOverwriteNullsOnly": false,
"isPreferredandSync": false,
"outputLayout": {
"fields": [
{
"name": "TEMP_KEY",
"type": "String",
"length": "2000",
"displayName": "TEMP_KEY"
"isIdentifier": false
},
{
"name": "LoadSeqNum",
"type": "String",
"length": "2000",
"displayName": "LoadSeqNum"
"isIdentifier": true
}
]
}
}
I tried this:
jq '.outputLayout.fields[] | . + {"isIdentifier": (if (.name)=="LoadSeqNum" then true else false end)}'
But of course I'm missing all the higher level things. When I try to do:
.outputLayout.fields[].isIdentifier=(if (.outputLayout.fields[].name)=="LoadSeqNum" then true else false end)
I get the whole thing twice, once with both true and the other one with both false. I understand why it's doing that but I'm having a tough time figuring out what would work. Any help or point in right direction?
.outputLayout.fields[] |= (.isIdentifier = (.displayName == "LoadSeqNum") )
Or equivalently but perhaps a little less cryptically:
.outputLayout.fields |= map( .isIdentifier = (.displayName == "LoadSeqNum") )

Search JSON for multiple values, not using a library

I'd like to be able to search the following JSON object for objects containing the key 'location' then get in return an array or json object with the 'name' of the person plus the value of location for that person.
Sample return:
var matchesFound = [{Tom Brady, New York}, {Donald Steven,Los Angeles}];
var fbData0 = {
"data": [
{
"id": "X999_Y999",
"location": "New York",
"from": {
"name": "Tom Brady", "id": "X12"
},
"message": "Looking forward to 2010!",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X999/posts/Y999"
},
{
"name": "Like",
"link": "http://www.facebook.com/X999/posts/Y999"
}
],
"type": "status",
"created_time": "2010-08-02T21:27:44+0000",
"updated_time": "2010-08-02T21:27:44+0000"
},
{
"id": "X998_Y998",
"location": "Los Angeles",
"from": {
"name": "Donald Steven", "id": "X18"
},
"message": "Where's my contract?",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X998/posts/Y998"
},
{
"name": "Like",
"link": "http://www.facebook.com/X998/posts/Y998"
}
],
"type": "status",
"created_time": "2010-08-02T21:27:44+0000",
"updated_time": "2010-08-02T21:27:44+0000"
}
]
};
#vsiege - you can use this javascript lib (http://www.defiantjs.com/) to search your JSON structure.
var fbData0 = {
...
},
res = JSON.search( fbData0, '//*[./location and ./from/name]' ),
str = '';
for (var i=0; i<res.length; i++) {
str += res[i].location +': '+ res[i].from.name +'<br/>';
}
document.getElementById('output').innerHTML = str;
Here is a working fiddle;
http://jsfiddle.net/hbi99/XhRLP/
DefiantJS extends the global object JSON with the method "search" and makes it possible to query JSON with XPath expressions (XPath is standardised query language). The method returns an array with the matches (empty array if none were found).
You can test XPath expressions by pasting your JSON here:
http://www.defiantjs.com/#xpath_evaluator