VBA JSON Parsing with VBA-Web Assistance - json

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")

Related

Add a new element/field to every JSON array/record in an existing JSON file - python 3

I have JSON files that consists of 10+ records, like the one below.
Example:
[
{
"ID": null,
"entity": "xxx",
"Name": "Abc",
},
{
"ID": null,
"entity": "yyy",
"Name": "efg",
}
]
I want to add a new element "Date": "xx/xx/xx" to every record, like this:
[
{
"ID": null,
"entity": "xxx",
"Name": "Abc",
"Date": "xx/xx/xx"
},
{
"ID": null,
"entity": "yyy",
"Name": "efg",
"Date": "xx/xx/xx"
}
]
The order does not matter.
So far I have something like this:
date = 'xx-xx-xx'
def file(data, json):
with pathlib.Path(json).open('r+') as upfile:
fdata = json.load(upfile)
fdata.append(data)
upfile.seek(0)
json.dump(fdata, updfile)
...
new_ele = {'Date: ': date}
file(new_ele, file.json)
But my result is like this:
[
{
"ID": null,
"entity": "xxx",
"Name": "Abc",
},
{
"ID": null,
"entity": "yyy",
"Name": "efg",
},
{
"Date": "xx-xx-xx"
}
]
How can I get the Date into every single record?
Thank you
Once imported, your json is a list of dictionaries. So go through all list elements (which are dictionaries) and update them with the update method:
def update_json(data, json_path):
with pathlib.Path(json_path).open('r+') as upfile:
fdata = json.load(upfile)
for elt in fdata:
elt.update(data)
upfile.seek(0)
json.dump(fdata, upfile)

How check the null items in object and return empty string

This is my object
$User = DB::table('user_app')
->where('id',$request->get('token'))
->select('account_type','address','card_bank','code_melli','shahr as city','family','id','mobile','name','phone','role','ostan as state','zip_code')
->first();
This is my json respons
"user_data": {
"account_type": "real",
"address": "سناباد",
"city": null,
"family": "حسینی",
"id": 180,
"mobile": "09371521106",
"name": "الهام",
"phone": "0513845555",
"role": "usual",
"state": {
"id": 11,
"name": "خراسان رضوی"
},
"zip_code": "91387"
}
Now i want to put null property empty string for example instead of city put "" value for APIs...
How could i do it

How would you reduce a json value

I have a Graphql returning values that look like this
"{\"date\":\"2020-05-21\",\"time\":null,\"changed_at\":\"2020-05-25T16:16:33.201Z\"}"
How would you target just the date to return "2020-05-21"?
Here is a larger picture of the retuned.
"column_values": [
{
"value": null,
"id": "check8",
"title": "Approved?"
},
{
"value": "{\"date\":\"2020-05-21\",\"time\":null,\"changed_at\":\"2020-05-25T16:16:33.201Z\"}",
"id": "due_date2",
"title": "Due Date"
},
{
"value": null,
"id": "qtr",
"title": "QTR"
},
{
"value": null,
"id": "status5",
"title": "Priority"
},
{
"value": "{\"index\":2,\"post_id\":null,\"changed_at\":\"2020-05-22T17:56:59.936Z\"}",
"id": "status",
"title": "Status"
},
{
"value": null,
"id": "progress",
"title": "Progress"
},
{
"value": null,
"id": "link",
"title": "Video Link"
},
{
"value": null,
"id": "formula8",
"title": "DATE_API"
}
]
}
Do you have to parse the JSON or Stringify it first? perhaps Graphql isn't returning usable json. Thank you for any help or bump in the correct direction.
JSON.stringify() is the method used to generate a JSON string. If you apply it to something that's already a JSON string then you'll get a double-encoded JSON string.
What you need to use is the JSON.parse() method:
const jsonObject= '{"foo": "bar"}';
const decoded = JSON.parse(jsonObject);
console.log(decode, typeof decoded);
Result:
{ foo: 'bar' } 'object'

Using groupby in sequelize to get an array of the grouped items

I am using sequelize(4.42.0) along with express. I have two tables categories and main_categories which has the following association:
db.main_categories.hasMany(db.categories);
db.categories.belongsTo(db.main_categories);
I tried to get list of all the categories grouped under their main category using following sequelize command:
var _categoryList = await models.main_categories.findAll({include: [{model: models.categories}], group:['main_categories.id']});
res.send({category_list: _categoryList});
The output of the above sequelize code is :
{
"category_list": [
{
"id": 1,
"name": "Physics",
"created_at": null,
"updated_at": null,
"categories": [
{
"id": 1,
"name": "Mechanics",
"created_at": "2019-03-21T03:39:48.000Z",
"updated_at": "2019-03-21T03:39:48.000Z",
"main_category_id": 1
}
]
},
{
"id": 2,
"name": "Chemistry",
"created_at": null,
"updated_at": null,
"categories": [
{
"id": 6,
"name": "General and Physical Chemistry",
"created_at": "2019-03-21T03:42:54.000Z",
"updated_at": "2019-03-21T03:42:54.000Z",
"main_category_id": 2
}
]
},
{
"id": 3,
"name": "Zoology",
"created_at": null,
"updated_at": null,
"categories": []
},
{
"id": 4,
"name": "Botany",
"created_at": null,
"updated_at": null,
"categories": []
}
]
}
The expected output for me was that the categories array inside each main_category would contain details of all the categories falling under the same main category instead of just one as shown in the output above.
My main_categories table with data:
my categories table with data:
So I later found out that as the associations had already been declared we just need to include the model without any groupby clause. So the final code that worked for me was :
var _categoryList = await models.main_categories.findAll({include: [{model: models.categories}]});

iteration with ng-repeat on nested JSON

How do I use ng-repeat if my JSON looks like
[
{
"id": "1",
"shop_name": "Shop Name",
"user_id": "2",
"shop_email": "",
"shop_address": "",
"shop_phone": "",
"company_name": "",
"description": "",
"shop_provision": null,
"created_at": "2014-11-05 10:32:32",
"updated_at": "2014-11-06 21:02:00",
"banners": [
{
"id": "18",
"disk_name": "545be1dfa2011452107756.png",
"file_name": "Screenshot from 2014-11-06 09:50:48.png",
"file_size": "135441",
"content_type": "image/png",
"title": null,
"description": null,
"field": "banners",
"sort_order": "18",
"created_at": "2014-11-06 21:02:23",
"updated_at": "2014-11-06 21:02:34",
"path": "/uploads/public/545/be1/dfa/545be1dfa2011452107756.png",
"extension": "png"
},
{
"id": "20",
"disk_name": "545be1e6b5048131391528.png",
"file_name": "Screenshot from 2014-11-06 09:50:48.png",
"file_size": "135441",
"content_type": "image/png",
"title": null,
"description": null,
"field": "banners",
"sort_order": "20",
"created_at": "2014-11-06 21:02:30",
"updated_at": "2014-11-06 21:02:34",
"path": "/uploads/public/545/be1/e6b/545be1e6b5048131391528.png",
"extension": "png"
}
]
}
]
on banners I would like iterate again
On controller I get a JSON object or array which contains a subObject (responseJSON)
october.controllers['dashboard/wm'] = function ($scope, $request) {
$scope.banners = $request('onGetBanners'); //the $request is an ajax function
console.log($scope.banners);
}
the subObject
responseJSON looks like as the result in firebug console
Object { result="[{"id":"1","shop_name":"...","extension":"png"}]}]"}
from all this I want the result=
than I try to iterate the scope like
<div ng-repeat="banner in banners.responseJSON.result track by $index"></div>
Update
I changed the way as I initiate the scope in controller
october.controllers['publishers/wm'] = function ($scope, $request) {
$request('onGetBanners', {success: function(data){
this.success(data).done(function() {
$scope.response = data.result;
});
}})
}
than again trying iterate
{% verbatim %}
{{response}} //outputs
[
{
"id": "2",
"shop_name": "Test Shop",
"user_id": "1",
"shop_email": null,
"shop_address": null,
"shop_phone": null,
"company_name": null,
"description": "",
"shop_provision": null,
"created_at": "2014-11-05 11:31:15",
"updated_at": "2014-11-05 11:31:15",
"banners": []
}
]
<div ng-repeat="shop in response track by $index">
{{ shop.shop_name }} //no data
</div>
{% endverbatim %}
if I remove track by $index I get
Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: shop in response, Duplicate key: string:", Duplicate value: "\""
Got it and I never gonna forget this pain
angular.fromJson(jsondata)
Have you tried something like this?
HTML
<div ng-repeat="shop in responseJson">
{{shop.id}}
<div ng-repeat="banner in shop.banners">
<img ng-src="{{banner.path}}>
</div>
</div>
See on jsFiddle