Edited - Read all nodes from a JSON VBA [closed] - arrays

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 days ago.
The community is reviewing whether to reopen this question as of 5 days ago.
Improve this question
Please, how to extract all nodes/leves from a JSON return of a httpRequest, for example: vendors values? It's about nested items and several levels of objects in JSON return. I spend two days off to search about this question, but cannot resolve this.
What I need is transpose this to a sheet in Excel.
Public Sub Quotations()
Dim response, Endpoint, QueryParams As String
Endpoint = "/vendors/quotations"
QueryParams = "?dateFrom=2001-01-01 12:00&modals=[1,2]&markets=[1,2]&openOnly=0"
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", URL + Endpoint + QueryParams, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Authorization", "Bearer " & JWTToken
.send
response = .responseText
End With
'Debug.Print response
Dim Json As Object
Set Json = JsonConverter.ParseJson(response)
'Debug.Print JsonConverter.ConvertToJson(Json, Whitespace:=2)
Dim sStatusProcesso As String
Dim uline, i As Long
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet
Set ws = wb.Sheets("Quotations")
'ws.Range("J" & 2).Value = JsonConverter.ConvertToJson(Json, Whitespace:=2)
Application.ScreenUpdating = False
sStatusProcesso = "Aguarde... O sistema está processando as informações. "
Application.StatusBar = sStatusProcesso
i = 1
'For i = 1 To uline
For Each Item In Json
'Application.StatusBar = sStatusProcesso & Format(ws.Row / uline, "0.0%") & " Concluído"
Debug.Print ArrayLen(Item("modals"))
i = i + 1
ws.Range("A" & i).Value = Item("id")
ws.Range("B" & i).Value = Item("operation")
ws.Range("C" & i).Value = Item("market")("code")
ws.Range("D" & i).Value = Item("modals")(1)("modalService")("code")
ws.Range("E" & i).Value = Item("vendors")(1)("vendor")("id")
ws.Range("F" & i).Value = Item("vendors")(1)("vendor")("vat")
ws.Range("G" & i).Value = Item("vendors")(1)("vendor")("commercialName")
ws.Range("H" & i).Value = Item("vendors")(1)("status")
ws.Range("I" & i).Value = Item("isCanceled")
Next
'Next i
Application.StatusBar = False
Application.ScreenUpdating = True
End Sub
and this is JSON returned:
[
{
""id"":143,
""operation"":2,
""market"":{
""code"":2
},
""modals"":[
{
""modalService"":{
""code"":1
}
}
],
""vendors"":[
{
""vendor"":{
""id"":75,
""vat"":""43.823.079/0005-97"",
""commercialName"":""DB SCHENKER""
},
""status"":""AW""
},
{
""vendor"":{
""id"":75,
""vat"":""43.823.079/0005-97"",
""commercialName"":""DB SCHENKER""
},
""status"":""AW""
}
],
""isCanceled"":null
},
{
""id"":199,
""operation"":2,
""market"":{
""code"":2
},
""modals"":[
{
""modalService"":{
""code"":1
}
}
],
""vendors"":[
{
""vendor"":{
""id"":75,
""vat"":""43.823.079/0005-97"",
""commercialName"":""DB SCHENKER""
},
""status"":""AW""
}
],
""isCanceled"":null
},
{
""id"":247,
""operation"":2,
""market"":{
""code"":2
},
""modals"":[
{
""modalService"":{
""code"":1
}
}
],
""vendors"":[
{
""vendor"":{
""id"":75,
""vat"":""43.823.079/0005-97"",
""commercialName"":""DB SCHENKER""
},
""status"":""AW""
}
],
""isCanceled"":null
},
{
""id"":258,
""operation"":2,
""market"":{
""code"":2
},
""modals"":[
{
""modalService"":{
""code"":1
}
}
],
""vendors"":[
{
""vendor"":{
""id"":75,
""vat"":""43.823.079/0005-97"",
""commercialName"":""DB SCHENKER""
},
""status"":""AW""
}
],
""isCanceled"":null
},
{
""id"":267,
""operation"":2,
""market"":{
""code"":2
},
""modals"":[
{
""modalService"":{
""code"":1
}
}
],
""vendors"":[
{
""vendor"":{
""id"":75,
""vat"":""43.823.079/0005-97"",
""commercialName"":""DB SCHENKER""
},
""status"":""AW""
}
],
""isCanceled"":null
},
{
""id"":268,
""operation"":2,
""market"":{
""code"":2
},
""modals"":[
{
""modalService"":{
""code"":1
}
}
],
""vendors"":[
{
""vendor"":{
""id"":75,
""vat"":""43.823.079/0005-97"",
""commercialName"":""DB SCHENKER""
},
""status"":""AW""
}
],
""isCanceled"":null
},
{
""id"":277,
""operation"":2,
""market"":{
""code"":2
},
""modals"":[
{
""modalService"":{
""code"":1
}
}
],
""vendors"":[
{
""vendor"":{
""id"":75,
""vat"":""43.823.079/0005-97"",
""commercialName"":""DB SCHENKER""
},
""status"":""AW""
}
],
""isCanceled"":null
},
{
""id"":284,
""operation"":2,
""market"":{
""code"":2
},
""modals"":[
{
""modalService"":{
""code"":1
}
}
],
""vendors"":[
{
""vendor"":{
""id"":75,
""vat"":""43.823.079/0005-97"",
""commercialName"":""DB SCHENKER""
},
""status"":""AW""
}
],
""isCanceled"":null
},
{
""id"":687,
""operation"":2,
""market"":{
""code"":2
},
""modals"":[
{
""modalService"":{
""code"":1
}
}
],
""vendors"":[
{
""vendor"":{
""id"":75,
""vat"":""43.823.079/0005-97"",
""commercialName"":""DB SCHENKER""
},
""status"":""AW""
},
{
""vendor"":{
""id"":75,
""vat"":""43.823.079/0005-97"",
""commercialName"":""DB SCHENKER""
},
""status"":""NS""
}
],
""isCanceled"":null
}"
]
Can you help me?

Related

adding rank based on sorting mongodb aggregate unwind

I am using includeArrayIndex to add the rank based on sorting
Here is the aggregation query
dbo.collection("funds").aggregate([
{
"$sort": {
"amount": -1
}
},
{
"$group": {
"_id": "",
"items": {
"$push": "$$ROOT"
}
}
},
{
"$unwind": {
"path": "$items",
"includeArrayIndex": "items.rank"
}
},
{
"$replaceRoot": {
"newRoot": "$items"
}
},
{
"$sort": {
"rank": -1
}
}
])
and this assign the rank field but starting from 0, is there any way to start it from 1
tried with set like this
[
{
'$sort': {
'amount': -1
}
}, {
'$group': {
'_id': '',
'items': {
'$push': '$$ROOT'
}
}
}, {
'$unwind': {
'path': '$items',
'includeArrayIndex': 'items.rank'
}
}, {
'$replaceRoot': {
'newRoot': '$items'
}
},
{
'$set': {
'rank': {
'$add': [
'$rank', 1
]
}
}
},
// {
// '$sort': {
// 'rank': 1
// }
// }
]
in the mongodb compass aggregation tab it shows that field got added but when i run this script using nodejs it does not add the rank field
even i tried with
const a = await dbo.collection("funds").aggregate(
[
{
'$sort': {
'amount': 1
}
}, {
'$group': {
'_id': '',
'items': {
'$push': '$$ROOT'
}
}
}, {
'$unwind': {
'path': '$items',
'includeArrayIndex': 'items.rank'
}
}, {
'$replaceRoot': {
'newRoot': '$items'
}
},
{
'$addFields': {
'rank': {
'$add': [
'$rank', 1
]
}
}
}
]
).toArray();
and this even prints on console as a
[
{ _id: new ObjectId("6220d2fe20e33d48c865b720"), amount: 1, rank: 1 },
{
_id: new ObjectId("6220d2cf20e33d48c865b71e"),
amount: 10,
rank: 2
},
{
_id: new ObjectId("6220d2f520e33d48c865b71f"),
amount: 12,
rank: 3
}
]
and then tried with $setWindowFields
dbo.collection("funds").aggregate( [
{
$setWindowFields: {
sortBy: { amount: -1 },
output: {
rank: {
$rank: {}
}
}
}
}
] )
but it shows
err MongoServerError: Unrecognized pipeline stage name: '$setWindowFields'
Sample document is like
[
{amount : 20, name :""},
{amount : 22, name :""}
]
Not sure if this was already answered. But you just have to add { $out: "funds" }
at the last of your code. PFB the working code ...
const a = await dbo.collection("funds").aggregate(
[
{
'$sort': {
'amount': 1
}
}, {
'$group': {
'_id': '',
'items': {
'$push': '$$ROOT'
}
}
}, {
'$unwind': {
'path': '$items',
'includeArrayIndex': 'items.rank'
}
}, {
'$replaceRoot': {
'newRoot': '$items'
}
},
{
'$addFields': {
'rank': {
'$add': [
'$rank', 1
]
}
}
},
{ $out: "funds" }
], {allowDiskUse:true}
).toArray();

Parsing JSON after conversion using XML2JSON

Calling an api and getting the response in xml. I am trying to convert the xml output to JSON using xml2json. How can now i read directly the temperature parameter value from the json output.
I am using following in my code,
var xml = response.body;
var result = convert.xml2json(xml, {compact: true, spaces: 4});
const jsonData = JSON.parse(result);
res.send(jsonData);
jsonData has all data but i just want to read e.g temperature value
Here is the json output after conversion,
{
"_declaration": {
"_attributes": {
"version": "1.0",
"encoding": "UTF-8"
}
},
"wfs:FeatureCollection": {
"_attributes": {
"timeStamp": "2019-05-02T17:21:05Z",
"numberReturned": "864",
"numberMatched": "864",
"xmlns:wfs": "http://www.opengis.net/wfs/2.0",
"xmlns:gml": "http://www.opengis.net/gml/3.2",
"xmlns:BsWfs": "http://xml.fmi.fi/schema/wfs/2.0",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xsi:schemaLocation": "http://www.opengis.net/wfs/2.0
http://schemas.opengis.net/wfs/2.0/wfs.xsd\n
http://xml.fmi.fi/schema/wfs/2.0
http://xml.fmi.fi/schema/wfs/2.0/fmi_wfs_simplefeature.xsd"
},
"wfs:member": [
{
"BsWfs:BsWfsElement": {
"_attributes": {
"gml:id": "BsWfsElement.1.1.1"
},
"BsWfs:Location": {
"gml:Point": {
"_attributes": {
"gml:id": "BsWfsElementP.1.1.1",
"srsDimension": "2",
"srsName": "http://www.opengis.net/def/crs/EPSG/0/4258"
},
"gml:pos": {
"_text": "60.20520 24.65220 "
}
}
},
"BsWfs:Time": {
"_text": "2019-05-02T18:00:00Z"
},
"BsWfs:ParameterName": {
"_text": "GeopHeight"
},
"BsWfs:ParameterValue": {
"_text": "36.57"
}
}
},
{
"BsWfs:BsWfsElement": {
"_attributes": {
"gml:id": "BsWfsElement.1.1.2"
},
"BsWfs:Location": {
"gml:Point": {
"_attributes": {
"gml:id": "BsWfsElementP.1.1.2",
"srsDimension": "2",
"srsName":
"http://www.opengis.net/def/crs/EPSG/0/4258"
},
"gml:pos": {
"_text": "60.20520 24.65220 "
}
}
},
"BsWfs:Time": {
"_text": "2019-05-02T18:00:00Z"
},
"BsWfs:ParameterName": {
"_text": "Temperature"
},
"BsWfs:ParameterValue": {
"_text": "2.97"
}
}
}
]
}
}
jsonData['wfs:FeatureCollection']['wfs:member'][0]['BsWfs:BsWfsElement']['BsWfs:ParameterName']['_text']
ex)
jsonData = { "key0": 34 }
jsonArray = [ 35,36,37 ]
=>
console.log(jsonData['key0"]); // 34
console.log(jsonArray[0]); // 35

how to create new json from existing json

//Existing Json that i have
[
{
"Id":1,
"Authors":[
{
"Id":10,
"Name":"Arun"
},
{
"Id":14,
"Name":"Arjun"
}
],
"tags":[
{
"Name":"Java"
}
]
},
{
"Id":5,
"Authors":[
{
"Id":7,
"Name":"Shyama"
}
],
"tags":[
{
"Name":"C#"
},
{
"Name":"C++"
}
]
}
]
//New Json (converted)
//Here i want to combine both the author
[
{
"Id":1,
"Authors":"Arun, Arjun",
"tags":"Java"
},
{
"Id":5,
"Authors":"Shyama",
"tags":"C#,C++"
}
]
//Controller that i have created
$scope.request = response.data;//request contain all json data
//---------- logic to create new json------------
}
var json = [
{
"Id":1,
"Authors":[
{
"Id":10,
"Name":"Arun"
},
{
"Id":14,
"Name":"Arjun"
}
],
"tags":[
{
"Name":"Java"
}
]
},
{
"Id":5,
"Authors":[
{
"Id":7,
"Name":"Shyama"
}
],
"tags":[
{
"Name":"C#"
},
{
"Name":"C++"
}
]
}
];
for (var i=0, j=json.length; i<j; i++) {
json[i].Authors = json[i].Authors.map(function(item) {return item.Name;}).join(',');
json[i].tags = json[i].tags.map(function(item) {return item.Name;}).join(',');
}
console.log(json);

How to change Array structure in angular 2

My Array is like
Data=[
{item:'A',depend:[
{dependitem:'B',flag:2},
{dependitem:'C',flag:3},
{dependitem:'D',flag:2},
{dependitem:'E',flag:3}
],group:[2,6]},
{item:'F',depend:[
{dependitem:'G',flag:1},
{dependitem:'H',flag:2},
{dependitem:'I',flag:1},
{dependitem:'J',flag:1}
],group:[1,2]},
];
I want to change Data Array to
Group=[
{group1:[
{dependitem:'G'},
{dependitem:'I'},
{dependitem:'J'}
]},
{group2:[
{dependitem:'B'},
{dependitem:'D'},
{dependitem:'H'}
]},
{group3:[
{dependitem:'C'},
{dependitem:'E'}
]}
];
The second array is group on the basis of flag on the first array ie Data array.
Thanks in advance.
PLUNKER
Code:
let groups = {};
this.data.forEach((d) => {
d.depend.forEach((r) => {
if(groups['group' + r.flag]) {
groups['group' + r.flag].push({dependitem: r.dependitem});
} else {
groups['group' + r.flag] = [{dependitem: r.dependitem}];
}
})
});
Object.keys(groups).forEach((k) => {
let o = {};
o[k] = groups[k];
this.group.push(o)
})
Output: is this OK ?
[
{
"group2": [
{
"dependitem": "B"
},
{
"dependitem": "D"
},
{
"dependitem": "H"
}
]
},
{
"group3": [
{
"dependitem": "C"
},
{
"dependitem": "E"
}
]
},
{
"group1": [
{
"dependitem": "G"
},
{
"dependitem": "I"
},
{
"dependitem": "J"
}
]
}
]

How access the nested json data using ng-repeat or angular.forEach

i have the following json file. i am unable to get the data dynamically. please help me to solve this issue.
my requirement is i need to view the url value based on the submodule ie; sname || id. if i am accessing the sname as chapter1 i need to get all the vid -> url values that are present for that sub module.
[
{
"name":"Introduction - About The PMP Credential",
"sub1":[
{
"sname":"Introduction to PMP",
"id":"1",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Chapter1",
"id":"2",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Justification",
"id":"3",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
}
]
},
{
"name":"Project Management Framework",
"sub1":[
{
"sname":"Project Seminar",
"id":"4",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Introduction to Communication Management",
"id":"5",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Final Communication Management",
"id":"6",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Modules",
"id":"7",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
}
]
},
{
"name":"Cost Management",
"sub1":[
{
"sname":"Introduction to Cost Management",
"id":"8",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
},
{
"sname":"Cost Management Introduction",
"id":"9",
"vid":[
{
"url":"www.abcd.co"
},
{
"url":"www.xyz.com"
}
]
}
]
}
]
<div ng-repeat="item_nm in test">{{item_nm.name}}
<div ng-repeat="sub1_dt in item_nm.sub1">
{{sub1_dt.sname}}
<div ng-repeat="vid in sub1_dt.vid">
{{vid.url}}
</div>
</div>
</div>
This will display name and corresponding sname and its url.

Resources