How do i check if certain value is a string using Postman - json

I am trying to access a values inside keys using Postman and test what type of value it. The format i receive is Json and it is an array of few Json objects with the same Key and i am trying to test if the value matches the format ( for example if the value of a key is a string or int or...)
[
{
"id": 1,
"accountNo": "1234567891234567",
"companyCode": "Test",
"apiKey": null,
"name": "Test",
"channel": "SFTP",
"levelOne": 1,
"levelTwo": 1,
"levelThree": 1,
"templateName": null,
"codeFormat": "testni",
"isAmountChangeAllowed": true,
"isActive": false,
"phoneNumber": "123456789",
"email": "test#test.com",
"pdfFileUrl": "/app/",
"interval": 0,
"day": 0
},
{
"id": 4,
"accountNo": "54345",
"companyCode": "TEST",
"apiKey": null,
"name": "Testno Name",
"channel": "SFTP",
"levelOne": 1,
"levelTwo": 1,
"levelThree": 1,
"templateName": null,
"codeFormat": "testno",
"isAmountChangeAllowed": true,
"isActive": true,
"phoneNumber": "312",
"email": "test#test.com",
"pdfFileUrl": "/app/op",
"interval": 2,
"day": 5
},
Error message i get is "AssertionError: expected undefined to deeply equal 5"

Related

VBA JSON Parsing with VBA-Web Assistance

I have some JSON parsing in VBA that's driving me nuts. Here's a sample response:
{
"data": [
{
"type": "access_user",
"attributes": {
"name": "Me",
"email": null,
"phone": null,
"department": "",
"status": "current",
"source": null,
"guest_source": null,
"created_at": "2019-08-19T21:45:56Z",
"updated_at": "2019-08-22T03:38:33Z",
"pin": "77005",
"card_number": null
},
"id": "be66e054-5509-4cf6-a5c2-14b85eb25619",
"links": {
"self": "https://api"
}
},
{
"type": "access_user",
"attributes": {
"name": "Day Code",
"email": null,
"phone": null,
"department": null,
"status": "current",
"source": null,
"guest_source": null,
"created_at": "2019-08-21T19:49:29Z",
"updated_at": "2021-06-16T16:08:28Z",
"pin": "12345",
"card_number": null
},
"id": "3a615a3d-eb1c-4973-9e9f-ace5e29ca964",
"links": {
"self": "https://api"
}
}
],
"meta": {
"page": 1,
"per_page": 25,
"total_count": 2,
"total_pages": 1
}
}
Traversing "data" is fine, but when I traverse "attributes" I'm getting a "Type mismatch" error. Here's my code. I'm not seeing anything wrong. Any thoughts?
For Each Item In Response2.Data("data")
UserType = Item("type")
Id = Item("id")
If UserType = "access_user" Then
For Each Nested In Item("attributes")
Name = Nested("name")
status = Nested("status")
PIN = Nested("pin")
Next
End If
Next
Anyone's assistance is greatly appreciated!
The following two lines are equivalent . . .
For Each Nested In Item("attributes")
and
For Each Nested In Item("attributes").Keys()
So, in fact, you're looping through each key within the dictionary. And so your control variable Nested is assigned a string, hence the type mismatch.
Since Item("attributes") returns a dictionary object, you can eliminate the For Each/Next loop, and you can access your items as follows . . .
Name = Item("attributes")("name")
Status = Item("attributes")("status")
PIN = Item("attributes")("pin")

How to filter JSON array using JQ in shell (STRIPE)?

Hello internet people,
Apparently I had the same problem of many but after searching a lot even here I could not find a reasonable answer at all!
My problem:
On shell I did enter the command:
stripe invoices list --customer MY_CUSTOMER_ID_HERE
Then the following JSON appeared:
{
"object": "list",
"data": [
{
"id": "INVOICE_ID",
"object": "invoice",
"account_country": "COUNTRY",
"account_name": "MY_BUSYNESS_NAME",
"account_tax_ids": null,
"amount_due": 1000,
"amount_paid": 1000,
"amount_remaining": 0,
"application_fee_amount": null,
"attempt_count": 1,
"attempted": true,
"auto_advance": false,
"billing_reason": "subscription_create",
"charge": "CHARGE_ID",
"collection_method": "charge_automatically",
"created": SOME_UNIXTIME,
"currency": "usd",
"custom_fields": null,
"customer": "CUSTOMER_ID",
"customer_address": null,
"customer_email": "CUSTOMER_EMAIL",
"customer_name": null,
"customer_phone": "CUSTOMER_PHONE",
"customer_shipping": null,
"customer_tax_exempt": "none",
"customer_tax_ids": [
],
"default_payment_method": null,
"default_source": null,
"default_tax_rates": [
],
"description": null,
"discount": null,
"discounts": [
],
"due_date": null,
"ending_balance": 0,
"footer": null,
"hosted_invoice_url": "INVOICE_URL_GOES_HERE",
"invoice_pdf": "INVOICE_PDF_URL_GOES_HERE",
"last_finalization_error": null,
"lines": {
"object": "list",
"data": [
{
"id": "ID",
"object": "line_item",
"amount": 1000,
"currency": "usd",
"description": "PLAN_DESCRIPTION",
"discount_amounts": [
],
"discountable": true,
"discounts": [
],
"livemode": false,
"metadata": {
},
"period": {
"end": SOME_UNIXTIME,
"start": SOME_UNIXTIME
},
"plan": {
"id": "PRICE_ID_HERE",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 1000,
"amount_decimal": "1000",
"billing_scheme": "per_unit",
"created": SOME_UNIXTIME,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {
},
"nickname": null,
"product": "PRODUCT_ID_HERE",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "MORE_ID_HERE",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": SOME_UNIXTIME,
"currency": "usd",
"livemode": false,
"lookup_key": null,
"metadata": {
},
"nickname": null,
"product": "PRODUCT_ID_HERE",
"recurring": {
"aggregate_usage": null,
"interval": "month",
"interval_count": 1,
"trial_period_days": null,
"usage_type": "licensed"
},
"tiers_mode": null,
"transform_quantity": null,
"type": "recurring",
"unit_amount": 1000,
"unit_amount_decimal": "1000"
},
"proration": false,
"quantity": 1,
"subscription": "SUBSCRIPTION_ID_HERE",
"subscription_item": "SUBS_ITEM_ID_HERE",
"tax_amounts": [
],
"tax_rates": [
],
"type": "subscription"
}
],
"has_more": false,
"total_count": 1,
"url": "IV_URL_HERE"
},
"livemode": false,
"metadata": {
},
"next_payment_attempt": null,
"number": "NUMBER_IV_XXXX",
"on_behalf_of": null,
"paid": true,
"payment_intent": "PAYMENT_INTENT_ID_HERE",
"payment_settings": {
"payment_method_options": null,
"payment_method_types": null
},
"period_end": SOME_UNIXTIME,
"period_start": SOME_UNIXTIME,
"post_payment_credit_notes_amount": 0,
"pre_payment_credit_notes_amount": 0,
"receipt_number": null,
"starting_balance": 0,
"statement_descriptor": null,
"status": "paid",
"status_transitions": {
"finalized_at": SOME_UNIXTIME,
"marked_uncollectible_at": null,
"paid_at": SOME_UNIXTIME,
"voided_at": null
},
"subscription": "SUBSCRIPTION_ID_HERE",
"subtotal": 1000,
"tax": null,
"total": 1000,
"total_discount_amounts": [
],
"total_tax_amounts": [
],
"transfer_data": null,
"webhooks_delivered_at": SOME_UNIXTIME
}
],
"has_more": false,
"url": "/some_path"
}
I'm able to access the second level with this command:
stripe invoices list --customer MY_CUSTOMER_ID_HERE | [.id,.attempt_count,.billing_reason,.created,.customer,.customer_email,.lines.data[],.paid,.status]'
Which returns a nice response, but I do want to filter few values inside data (data.lines.data[]) like id, plan and inside plan the id, currency and product for example.
My question:
Does any one knows how to do that with this kind of commands?
Just in case, those are the questions and answer that I tried but couldn't figured how to do it at all!
How to filter array of objects by element property values using jq?
How to filter arrays in json based on specific value using jq
How to filter nested arrays in JSON while maintaining structure using jq
filtering from JSON output from curl using JQ
Filtering JSON by object name using jq
How to filter JSON using jq stream (duplicate)
How to filter and replace values in json with jq
Filtering JSON list in shell using jq
I did also try a lot attempts on my own, like:
stripe invoices list --customer MY_CUSTOMER_ID_HERE | [.id,.attempt_count,.billing_reason,.created,.customer,.customer_email,.lines.data[.id.plan[][.id,.plan[.id,.currency,.product]],.paid,.status]'
Response:
jq: error (at :179): Cannot index string with string "plan"
stripe invoices list --customer MY_CUSTOMER_ID_HERE | [.id,.attempt_count,.billing_reason,.created,.customer,.customer_email,.lines.data[][],.paid,.status]'
This one return a null value for the second data content.
Response:
[
"DESIRED_ID",
1,
"value_retrieved_ok",
unixtime_ok,
"SOME_ID_OK",
"customer_mail#ok.com",
null, <<< ???
true,
"paid"
]
EDIT:
Desired result should look like:
[
"DESIRED_ID",
1,
"value_retrieved_ok",
unixtime_ok,
"SOME_ID_OK",
"customer_mail#ok.com",
"id" // the id inside lines.data{}
["id", // the id inside lines.data.plan
"currency", // the currency inside lines.data.plan
"product" // the product inside lines.data.plan
]
true,
"paid"
]
Anyway I do hope that there's a good soul out there have mercy!
Your requirements are a bit difficult to follow, especially since the "expected output" does not correspond exactly to the given input, but the following is either what you're looking for, or very close to it:
.data[]
| [.id,.attempt_count,.billing_reason,.created,.customer,.customer_email]
+ (.lines.data[] | [.id, [.plan.id, .plan.currency, .plan.product]])
+ [.paid,.status]

Python 3 - Extracting value from key in nested dictionary

Hoping for some pointers here as I'm striking out with my attempts. Am working with python 3.8.5.
I'm querying a Car Park booking system that returns a json list of availability. The result has lots of nested dictionaries and I'm struggling to extract just the values i want.
This is what I've been doing:
import requests
import json
enquiry = requests.post(url.....) #queries api, this works fine
results = enquiry.text #extracts response data
dictionary = json.loads(results) #convert response to python dict
If there is one slot available, I get this output (apologies for length). If there are multiple slots available, i get the same output repeated:
{
"data": {
"services": [{
"id": null,
"name": null,
"services": [{
"id": null,
"name": "Car Park",
"met": true,
"filterCount": 1,
"primary": true,
"options": [{
"id": "9",
"images": [],
"available": true,
"calendarId": "AAAA",
"templateId": "BBBB",
"capacity": 0,
"name": "Car Park Slot 3",
"sessionId": "CCCC",
"functions": null,
"startDate": "2020-08-18T13:30:00Z", <--this is what I want to extract
"endDate": "2020-08-18T14:30:00Z",
"geo": {
"lat": 0.0,
"lng": 0.0
},
"selected": false,
"linkedServices": [],
"tiers": null
}]
}],
"currentBookingId": null,
"startDate": {
"ms": 1597757400000,
"year": 2020,
"month": 8,
"day": 18,
"dayOfWeek": 2,
"time": {
"seconds": 0,
"minutes": 30,
"hours": 14,
"days": 0
}
},
"endDate": {
"ms": 1597761000000,
"year": 2020,
"month": 8,
"day": 18,
"dayOfWeek": 2,
"time": {
"seconds": 0,
"minutes": 30,
"hours": 15,
"days": 0
}
},
"sessionId": "2222222",
"chargeType": 1,
"hasPrimaryBookable": false,
"hasBookable": false,
"hasDiscounts": false,
"hasMultipleTiers": false,
"isPreferred": false,
"primaryServiceAvailable": true,
"primaryServiceId": null,
"primaryServiceType": "undefined",
"unavailableAttendees": []
}],
"bookingLimit": null
},
"success": true,
"suppress": false,
"version": "2.3.293",
"message": null,
"result": null,
"errors": null,
"code": null,
"flags": 0,
"redirect": null
}
I want to extract:
"startDate": "2020-08-18T13:30:00Z"
from each key, value pair in any of the returned slots. However, I cant work it out.
Extracting the whole of this nested dictionary would also contain the same data, but would then involve more work to tidy it up after.
"startDate": {
"ms": 1597757400000,
"year": 2020,
"month": 8,
"day": 18,
"dayOfWeek": 2,
"time": {
"seconds": 0,
"minutes": 30,
"hours": 14,
"days": 0
}
I've tried loads of dictionary.get and dictionary.items variations, but cant seem to get anywhere.
I tried something like
key = ('startDate')
availability = dictionary.get(key)
print(availability)
this just returns 'none', so think im way off
Any pointers?
Thanks in advance!
Thanks for the full data. It makes testing easier :)
I had to replace null -> None, true -> True, false -> False
slot = {
"data": {
"services": [{
"id": None,
"name": None,
"services": [{
"id": None,
"name": "Car Park",
"met": True,
"filterCount": 1,
"primary": True,
"options": [{
"id": "9",
"images": [],
"available": True,
"calendarId": "AAAA",
"templateId": "BBBB",
"capacity": 0,
"name": "Car Park Slot 3",
"sessionId": "CCCC",
"functions": None,
"startDate": "2020-08-18T13:30:00Z", # <--this is what I want to extract
................
print("Start Date:", slot['data']['services'][0]['services'][0]['options'][0]['startDate'])
Output
Start Date: 2020-08-18T13:30:00Z

Google Apps Script receive webhooks from Stripe, failed to extract part of the received event information

Following up on another questions (the reply works!):
How to use Google Apps Script to get POST request from Stripe webhooks
I have revised my code as follow:
function doPost(e) {
var jsonString = e.postData.contents;
var event = JSON.parse(jsonString)
var ss = SpreadsheetApp.openById("xxx");
var sheet = ss.getSheetByName("xxx");
var timeStamp = new Date();
var time = Utilities.formatDate(timeStamp, "GMT+08:00", "MM/dd/yy, h:mm a");
var lastRow = sheet.getLastRow();
//Insert the data into the sheet
sheet.getRange(lastRow + 1, 1).setValue(time);
sheet.getRange(lastRow + 1, 2).setValue(event["data"]["object"]["id"]);
sheet.getRange(lastRow + 1, 3).setValue(event["data"]["object"]["amount_paid"]);
sheet.getRange(lastRow + 1, 4).setValue(event["data"]["object"]["customer_email"]);
sheet.getRange(lastRow + 1, 5).setValue(event["data"]["object"]["lines"]["data"]["description"]);
return HtmlService.createHtmlOutput(200);
}
For the last .setValue one, I can not get the string "description", the request information is as follow:
{
"created": 1326853478,
"livemode": false,
"id": "evt_00000000000000",
"type": "invoice.payment_succeeded",
"object": "event",
"request": null,
"pending_webhooks": 1,
"api_version": null,
"data": {
"object": {
"id": "in_00000000000000",
"object": "invoice",
"account_country": "HK",
"account_name": "xxx",
"amount_due": 400,
"amount_paid": 400,
"amount_remaining": 0,
"application_fee_amount": null,
"attempt_count": 1,
"attempted": true,
"auto_advance": false,
"billing_reason": "subscription_create",
"charge": "_00000000000000",
"collection_method": "charge_automatically",
"created": 1570506796,
"currency": "hkd",
"custom_fields": null,
"customer": "cus_00000000000000",
"customer_address": null,
"customer_email": "xxx",
"customer_name": null,
"customer_phone": null,
"customer_shipping": null,
"customer_tax_exempt": "none",
"customer_tax_ids": [
],
"default_payment_method": null,
"default_source": null,
"default_tax_rates": [
],
"description": null,
"discount": null,
"due_date": null,
"ending_balance": 0,
"footer": null,
"hosted_invoice_url": "xxx",
"invoice_pdf": "xxx",
"lines": {
"data": [
{
"id": "il_00000000000000",
"object": "line_item",
"amount": 0,
"currency": "hkd",
"description": "xxx",
"discountable": true,
"livemode": false,
"metadata": {
"code": "xxx",
"user_id": "xxx",
"hashId": "xxx"
},
"period": {
"end": 1607183999,
"start": 1591607019
},
"plan": {
"id": "plan_00000000000000",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 10000,
"amount_decimal": "10000",
"billing_scheme": "per_unit",
"created": 1591603592,
"currency": "hkd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {
},
"nickname": null,
"product": "prod_00000000000000",
"tiers": null,
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "plan_00000000000000",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1591603592,
"currency": "hkd",
"livemode": false,
"lookup_key": null,
"metadata": {
},
"nickname": null,
"product": "prod_00000000000000",
"recurring": {
"aggregate_usage": null,
"interval": "month",
"interval_count": 1,
"usage_type": "licensed"
},
"tiers_mode": null,
"transform_quantity": null,
"type": "recurring",
"unit_amount": 10000,
"unit_amount_decimal": "10000"
},
"proration": false,
"quantity": 1,
"subscription": "sub_00000000000000",
"subscription_item": "si_00000000000000",
"tax_amounts": [
],
"tax_rates": [
],
"type": "subscription"
}
],
"has_more": false,
"object": "list",
"url": "xxx"
},
"livemode": false,
"metadata": {
},
"next_payment_attempt": null,
"number": "xxx",
"paid": true,
"payment_intent": "pi_00000000000000",
"period_end": 1570506796,
"period_start": 1570506796,
"post_payment_credit_notes_amount": 0,
"pre_payment_credit_notes_amount": 0,
"receipt_number": null,
"starting_balance": 0,
"statement_descriptor": null,
"status": "paid",
"status_transitions": {
"finalized_at": xxx,
"marked_uncollectible_at": null,
"paid_at": xxx,
"voided_at": null
},
"subscription": "sub_00000000000000",
"subtotal": 400,
"tax": null,
"tax_percent": null,
"total": 400,
"total_tax_amounts": [
],
"transfer_data": null,
"webhooks_delivered_at": 1570506798,
"closed": true
}
}
}

How to extract multiple correlating variables from a JSon

I have to extract multiple correlating variables from a response (which is json) in JMeter. Part of the response is listed below:
[
{
"data": {
"id": "efaa6876-7a8d-4723-9d85-1ed99e822f06",
"type": "courses",
"attributes": {
"created-at": "2019-02-07T16:38:50.735Z",
"contents-count": 267,
"units": [
{
"id": "31b5fcb1-24ee-441e-a0ee-ca859fc9a89d",
"position": null,
"progress": 0,
"completed": false,
"show_name": false,
"node_id": "1",
"children": [
{
"id": "b8ed75a3-0390-4273-82c3-03ee6eba729c",
"position": null,
"image": null,
"progress": 0,
"completed": false,
"show_name": true,
"node_id": "2",
"children": [],
"contents": [
{
"id": "fa1bdc2f-4330-425c-9c10-3734d07125aa",
"link": {
"url": "#",
"target": "_blank",
"class": "learning-object-link",
"data": {
"id": "fa1bdc2f-4330-425c-9c10-3734d07125aa",
"user-role": "teacher",
"open-method-tablet": "newtab",
"open-method-desktop": "modal",
"content-open-method": null,
"modal-size-method": "fully_responsive",
"fixed-width": null,
"fixed-height": null,
"aspect-ratio": null
}
},
"is_work": false,
"is_fun": false,
"completed": false,
"total_activities": 2,
"completed_activities": 0,
"progress": 0,
"updated_at": false,
"attempts": 0,
"duration": null
},
{
"id": "ceceabfd-5151-4656-af5d-3392c5a4c04c",
"link": {
"url": "#",
"target": "_blank",
"class": "learning-object-link",
"data": {
"id": "ceceabfd-5151-4656-af5d-3392c5a4c04c",
"user-role": "teacher",
"open-method-tablet": "newtab",
"open-method-desktop": "modal",
"content-open-method": null,
"modal-size-method": "fully_responsive",
"fixed-width": null,
"fixed-height": null,
"aspect-ratio": null
}
},
"is_work": false,
"is_fun": false,
"completed": false,
"total_activities": 2,
"completed_activities": 0,
"progress": 0,
"updated_at": false,
"attempts": 0,
"duration": null
}
]
},
{
"id": "60639cbd-f872-492d-b8e9-db83f8789fcf",
"position": null,
"image": null,
"progress": 0,
"completed": false,
"show_name": true,
"node_id": "3",
"children": [],
"contents": [
{
"id": "1825f834-7099-4bb4-b7a2-fc634faffc86",
"link": {
"url": "#",
"target": "_blank",
"class": "learning-object-link",
"data": {
"id": "1825f834-7099-4bb4-b7a2-fc634faffc86",
"user-role": "teacher",
"open-method-tablet": "newtab",
"open-method-desktop": "modal",
"content-open-method": null,
"modal-size-method": "fully_responsive",
"fixed-width": null,
"fixed-height": null,
"aspect-ratio": null
}
},
To proceed with the next request, I have to extract unit id (e.g. 31b5fcb1-24ee-441e-a0ee-ca859fc9a89d), children id (e.g. b8ed75a3-0390-4273-82c3-03ee6eba729c) and contents id (e.g. fa1bdc2f-4330-425c-9c10-3734d07125aa). There are several units, each unit has several children and each children has several contents. Each content id matches just one children id and each children id matches just one unit id. Ids have to be selected on a random basis.
I've tried to extract all ids from the response and use them randomly, but it doesn't work this way.
To extract only Unit Ids, you can use following JSON Path Expressions:
$..data.attributes.units[?(#.id)].id
Random Value for Unit Id also can be extracted using JMeter JSON Extractor: