Difficulties parsing json data in vb.net using Newtonsoft.Json - json

I have serialized a json stream using Newtonsoft.Json.
Here is what the data looks like:
{
"id": "55115654340-8d43-440c-8683-c3aa299e39cd",
"timestamp": "2018-02-09T13:57:34.143Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "HELP",
"action": "",
"actionIncomplete": false,
"parameters": {},
"contexts": [],
"metadata": {
"intentId": "63b7b4a5-3f7f-41b1-81ec-fcc6f70fb173",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"webhookResponseTime": 5000,
"intentName": "HELP"
},
"fulfillment": {
"speech": "",
"messages": [
{
"type": 0,
"speech": ""
}
]
},
"score": 1
},
"status": {
"code": 206,
"errorType": "partial_content",
"errorDetails": "Webhook call failed. Error: Request timeout.",
"webhookTimedOut": true
},
"sessionId": "f761206e-76d4-4a08-b95d-57a39694cb00"
}
using
Dim jsonResulttodict = JsonConvert.DeserializeObject(Of Dictionary(Of
String, Object))(decodedString)
I can get the id value by: var_id = jsonResulttodict.Item("id")
how do I get the values that are deeper, like the value of resolvedQuery that is below result ?
and also the value of intentName under metadata which is under result....
Thank you

Related

How do I sort a json using a time stamp field within the json file in Postman?

I am new to Postman, I have a API call which gives me the output as a json. In the "Test", I get the JSON by "bodyData = JSON.parse(responseBody);"
The JSON file has got timestamp fields example "executedDate": "2020-11-26T09:45:27.000Z". I would need help in sorting the JSON file in descending order based on the executedDate field.
{
"dataflowJobs": [
{
"createdDate": "2020-11-26T09:45:03.000Z",
"duration": 48,
"executedDate": "2020-11-26T09:45:27.000Z",
"id": "03C2w000002Urb1EAC",
"jobType": "user",
"label": "Dataflow_2",
"progress": 1.0,
"startDate": "2020-11-26T09:45:03.000Z",
"status": "Success",
"type": "dataflowjob",
"url": "/services/data/v47.0/wave/dataflowjobs/03C2w000002Urb1EAC"
},
{
"createdDate": "2020-11-26T09:45:01.000Z",
"duration": 34,
"executedDate": "2020-11-26T09:45:02.000Z",
"id": "03C2w000002UracEAC",
"jobType": "user",
"label": "Adv_Dataflow_Exercises",
"progress": 1.0,
"startDate": "2020-11-26T09:45:01.000Z",
"status": "Success",
"type": "dataflowjob",
"url": "/services/data/v47.0/wave/dataflowjobs/03C2w000002UracEAC"
}, {
"createdDate": "2020-11-20T09:45:01.000Z",
"duration": 58,
"id": "0eP2w000000MhszEAC",
"jobType": "recipe",
"label": "Feb_and_Jan_S2_Modified_Recipe2_recipe",
"progress": 1.0,
"startDate": "2020-11-20T09:45:01.000Z",
"status": "Success",
"type": "dataflowjob",
"url": "/services/data/v47.0/wave/dataflowjobs/0eP2w000000MhszEAC"
}
],
"url": "/services/data/v47.0/wave/dataflowjobs"
}
let moment = require('moment')
let jsonData = pm.response.json()
console.log(jsonData.dataflowJobs.sort(function (a, b) { return moment(b.executedDate).diff(moment(a.executedDate), "seconds") }))
This will sort according to executed time , if there is no executed time it will be at the top

Cannot get array property by using Parse JSON groovy

I'm new to Groovy. But, I can try with some simple code lines to extract the data.
I have read the from Parse JSON using groovy script (using JsonSlurper). But it didn't help in my case.
My code line:
{
"errors": false,
"address_data": [
{
"address_id": "567",
"township": {
"id": "41079",
"name": "Test Data"
},
"city": {
"id": "1622",
"name": "Test City"
},
"region": {
"id": "663",
"name": "Metro Test"
},
"stock_source_code": "Test",
"is_default_address": false
},
{
"address_id": "45444",
"township": {
"id": "41079",
"name": "Test Test"
},
"city": {
"id": "1622",
"name": "Test City"
},
"region": {
"id": "663",
"name": "Metro Test Taguig"
},
"is_default_address": true
},
{
"address_id": "45444",
"township": {
"id": "888888",
"name": "Apas"
},
"city": {
"id": "432",
"name": "Test City"
},
"region": {
"id": "591",
"name": "Test Cebu"
},
"stock_source_code": "testce",
"is_default_address": false
}
]
}
My code lines:
def response = "data_above";
def object = new JsonSlurper().parseText(response);
def errors = object.errors
if (errors == false) { //could access object.errors property
log.info "Checking condition"
def addressData = object.address_data // Cannot get the data
}
How can I get the address_data property?

Parse Complex Json Data with VB.net and Newtonsoft.Json

I'm building a windows application using vb.net to take a Json file that will be downloaded daily and parse it and enter the values into a SQL database. The only part I'm getting stuck on is being about to parse the Json data to get the values I need.
Public Sub json_parse(ByVal result_json As String)
Try
Dim json As String = result_json
Dim ser As JObject = JObject.Parse(json)
Dim data As List(Of JToken) = ser.Children().ToList
For Each item As JProperty In data
item.CreateReader()
Select Case item.Name
Case "data"
For Each msg As JObject In item.Values
MsgBox(msg.ToString)
Next
End Select
Next
Catch __unusedException1__ As Exception
'MsgBox(__unusedException1__.ToString)
End Try
End Sub
{
"data": {
"wsj_mdstrip_na,us": {
"set": 1562756496071,
"ttl": 300000,
"data": {
"id": "na,us",
"type": "wsj_mdstrip",
"data": [
{
"timeZone": "CDT",
"localTimeZoneTimestamp": "5:51 AM CDT 07/10/19",
"localTimestamp": "5:51 AM 07/10/19",
"etTimestamp": "6:51 AM ET 07/10/19",
"mdTimestamp": "2019-07-10T06:51:35-04:00",
"timestamp": "2019-07-10T05:51:35.365",
"chartingSymbol": "Future/US/XCBT/YM00",
"updatedTimestamp": "2019-07-10T07:01:36-04:00"
},
{
"deltaBarPosNeg": "deltaBar-neg",
"etTimestamp": "6:51 AM ET 07/10/19",
"chartingSymbol": "Future/US/XCME/ES00",
"pastCloses": [
{
"range": "P1Y",
"price": 2775.75
},
{
"range": "P3Y",
"price": 2145.75
}
]
}
]
}
},
"wsj_nav_na,us": {
"set": 1562754843231,
"ttl": 1800000,
"isStale": false
},
"mdc_cashprices_": {
"set": 1562756402351,
"ttl": 90000,
"data": {
"id": "{\"requestedCommodities\":\"all\",\"groupIntoMapBy\":\"type\",\"groupIntoSetsBy\":\"subType\"}",
"type": "mdc_cashprices",
"data": {
"instruments": [
{
"code": null,
"djShortName": "Gold-EnglehardFab",
"instrumentID": "511498",
"name": "Engelhard fabricated products",
"subType": "Gold, per troy oz",
"type": "Precious metals",
"ask": "-",
"bid": "-",
"last": "1500.72",
"previousDay": "1509.33",
"previousYear": "1351.58"
},
{
"code": null,
"djShortName": "Silver-EngelhrdFab",
"instrumentID": "511558",
"name": "Engelhard fabricated products",
"subType": "Silver, troy oz.",
"type": "Precious metals",
"ask": "-",
"bid": "-",
"last": "18.0600",
"previousDay": "18.0960",
"previousYear": "19.3200"
},
{
"code": null,
"djShortName": "SilverHandy&HarmB",
"instrumentID": "511560",
"name": "Handy & Harman base price",
"subType": "Silver, troy oz.",
"type": "Precious metals",
"ask": "-",
"bid": "-",
"last": "15.1070",
"previousDay": "15.0420",
"previousYear": "16.1000"
},
{
"code": null,
"djShortName": "Platinum-EngelhFab",
"instrumentID": "511554",
"name": "Platinum, Engelhard fabricated products",
"subType": "Other precious metals",
"type": "Precious metals",
"ask": "-",
"bid": "-",
"last": "912.0",
"previousDay": "914.0",
"previousYear": "946.0"
},
{
"code": null,
"djShortName": "Palladium-EngelInd",
"instrumentID": "511553",
"name": "Palladium, Engelhard industrial bullion",
"subType": "Other precious metals",
"type": "Precious metals",
"ask": "-",
"bid": "-",
"last": "1561.0",
"previousDay": "1573.0",
"previousYear": "955.0"
},
{
"code": null,
"djShortName": "Palladium-EngelFab",
"instrumentID": "511552",
"name": "Palladium, Engelhard fabricated products",
"subType": "Other precious metals",
"type": "Precious metals",
"ask": "-",
"bid": "-",
"last": "1661.0",
"previousDay": "1673.0",
"previousYear": "1055.0"
}
],
"timestamp": "7/09/19"
}
}
},
"consumer_corphat_corphat": {
"set": 1562756495973,
"ttl": 90000,
"data": {
"id": "corphat",
"type": "consumer_corphat",
"data": [
{
"ncLinks": [
{
"title": "Big Decisions",
"url": "http://www.bigdecisions.com/",
"nofollow": "false"
},
{
"title": "Business Spectator",
"url": "https://www.businessspectator.com.au/",
"nofollow": "false"
}
]
},
{
"djLinks": [
{
"title": "Barron's",
"url": "https://www.barrons.com",
"nofollow": "false"
},
{
"title": "BigCharts",
"url": "http://bigcharts.marketwatch.com",
"nofollow": "false"
}
]
}
]
},
"isStale": false
}
},
"currentState": {
"data": [
"0.0.3.0.0",
"0.0.3.1.0.0.2"
],
"components": {
"code___decoratedComponent___275181c7-8620-4df3-a008-d0cd9937db22___WSJTheme---WSJBase---WSJForms---WSJTables": {
"id": "275181c7-8620-4df3-a008-d0cd9937db22",
"decorators": [
"WSJTheme",
"WSJBase",
"WSJForms",
"WSJTables"
]
},
"code___decoratedComponent___c8882c9c-15d3-4d1f-9b0e-81b6f321365d___WSJTheme---WSJBase---WSJForms---WSJTables": {
"id": "c8882c9c-15d3-4d1f-9b0e-81b6f321365d",
"decorators": [
"WSJTheme",
"WSJBase",
"WSJForms",
"WSJTables"
]
}
}
}
}
I'm trying to get the values for "last", "previousDay" and "previousYear" from:
data > mdc_cashprices_ > data > instruments > (where djShortName = "Gold-EnglehardFab" and djShortName = "Silver-EngelhrdFab" and djShortName = "Platinum-EngelhFab" and djShortName = "Palladium-EngelFab")
I'm also trying to get the value for "timestamp" from:
data > mdc_cashprices_ > data > timestamp
In my current code I can only get to the 1st "data" tier.
Any help would be greatly appreciated.
Use something like https://jsonformatter.curiousconcept.com/ to view it more easily.
Then simply
Dim Something as JObject = JObject.Parse(<your JSON>)
Then get what you want with
Something1 = Something("data")("mdc_cashprices")("data")("data")("instruments")(0)("last") 'basically you just traverse the levels, you use the id/text for regular stuff (inside {}) and numbers for arrays (inside [])
Since it's an array you would probably want to loop it
For Each item In Something("data")("mdc_cashprices")("data")("data")("instruments")
Something2 = item("last")
Next
Btw there are some formatting issues here >>> mdc_cashprices_{\"req... <<< that _ and \ shouldn't be there"

Parsing JSON data using GSON api

I have a JSON data and able to get the JSON data into a string.
I wish to get a json array from the root object one by one (using a loop if many are there)
The returned object must be of type "com.google.gson.JsonObject" as another method takes this object as input to get me the attribute values.
my json looks like this:
I would be needing the json array in "Resources" -
{
"totalResults": 2,
"startIndex": 1,
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:ListResponse"
],
"Resources": [
{
"owner": {},
"application": {
"displayName": "Non Auth App",
"value": "4028b88168fa3d38016900d1759405a6",
"$ref": "http://localhost:8080/identityiq/scim/v2/Applications/4028b88168fa3d38016900d1759405a6"
},
"meta": {
"created": "2019-03-22T18:40:30.505+05:30",
"location": "http://localhost:8080/identityiq/scim/v2/Entitlements/297eff8a699f8b3a0169a5863769008b",
"version": "W/\"1553260230505\"",
"resourceType": "Entitlement"
},
"schemas": [
"urn:ietf:params:scim:schemas:sailpoint:1.0:Entitlement"
],
"displayableName": "reader",
"aggregated": false,
"id": "297eff8a699f8b3a0169a5863769008b",
"requestable": true,
"attribute": "Privileges",
"type": "Entitlement",
"descriptions": [],
"value": "reader"
},
{
"owner": {},
"application": {
"displayName": "Non Auth App",
"value": "4028b88168fa3d38016900d1759405a6",
"$ref": "http://localhost:8080/identityiq/scim/v2/Applications/4028b88168fa3d38016900d1759405a6"
},
"meta": {
"created": "2019-03-18T19:11:47.912+05:30",
"location": "http://localhost:8080/identityiq/scim/v2/Entitlements/297eff8a6990fc49016991096d08001e",
"version": "W/\"1552916507912\"",
"resourceType": "Entitlement"
},
"schemas": [
"urn:ietf:params:scim:schemas:sailpoint:1.0:Entitlement"
],
"displayableName": "admin",
"aggregated": false,
"id": "297eff8a6990fc49016991096d08001e",
"requestable": true,
"attribute": "Privileges",
"type": "Entitlement",
"descriptions": [],
"value": "admin"
}
]
}
Any help is appreciated.

get a node value from json file

I am working on a facebook graph api project and have this json file:
{
"data": [
{
"id": "407356746023254_569534706472123",
"from": {
"category": "Furniture",
"name": "`enter code here`",
"id": "407356746023254"
},
"message": "message",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/407356746023254/posts/569534706472123"
},
{
"name": "Like",
"link": "https://www.facebook.com/407356746023254/posts/569534706472123"
}
],
"privacy": {
"value": ""
},
"type": "photo",
"status_type": "added_photos",
"object_id": "569534676472126",
"created_time": "2013-12-03T22:54:45+0000",
"updated_time": "2013-12-04T15:29:03+0000",
"shares": {
"count": 1
},
I am working with Newtonsoft.Json.Linq to parse this file
my code:
For i = 0 To o("data").Count - 1
Dim o As JObject = JObject.Parse(data)
Response.Write(o("data")(i)("shares"))
next
the code result is {"count": 1 }
I want to get only the value "1" from
"shares": {
"count": 1
},
but I can't
I think I missed the concept in someway . Any body can help?
For i = 0 To o("data").Count - 1
Dim o As JObject = JObject.Parse(data)
Response.Write(o("data")(i)("shares")("count"))
next
This may help you... :) ;) :P