Anybody create the Excel VBA code for the following json file ?
"Version": "1.1",
"TranDtls": {
"TaxSch": "GST",
"SupTyp": "B2B",
"IgstOnIntra": "N",
"RegRev": "N",
"EcmGstin": null
},
"DocDtls": {
"Typ": "INV",
"No": "1",
"Dt": "21/10/2022"
Excel VBA code for the JSON file CREATION
We need some more information to help you find the correct answer. Do you want to read/import this json string or do you want to have Excel create such a string for export?
You can use the VBA-JSON module from github. A simple way to create the string is to copy paste it into a string variable and use the Replace function;
sTemp = "... "TaxSch": "[TaxSch]",....
sJson = Replace(sTemp,"[TaxSch]", Yourvalue)
Or you could use concatenation but that will be more difficult with all the quotes.
Related
I'm trying to validate some JSON files on VB.net.
However, Whenever I run my code it gets stuck on
Dim Schema As JsonSchema = JsonSchema.Parse(SchemaString)
The Error Says
An unhandled exception of type 'Newtonsoft.Json.JsonException' occurred in Newtonsoft.Json.dll.
There is also a warning that says that JSON validation moved to its own package. So, I'm pretty sure I'm just importing the wrong packages, but I'm not sure.
I would be grateful if anyone could point me in the correct direction,
Thank you.
Here is my VB.net code
Imports System
Imports Newtonsoft.Json.Schema
Imports Newtonsoft.Json.Linq
Public Function Validate_JSON()
Dim SplunkPath As String = "Z:\Database Project\Splunk Folder\Dropbox\Splunk_File.json"
Dim SchemaPath As String = "Z:\Database Project\Schema Folder\Schema_File.json"
Dim Schema_String As String = My.Computer.FileSystem.ReadAllText(SchemaPath)
Dim Schema As JsonSchema = JsonSchema.Parse(Schema_String)
Dim Data_String As String = My.Computer.FileSystem.ReadAllText(SplunkPath)
Dim Data_JSON As JObject = JObject.Parse(Data_String)
Dim Splunk_Status As Boolean = Data_JSON.IsValid(Schema)
Return 0
End Function
Here is Splunk_File.json
{
"Site": "USI",
"SN": "21165",
"MN": "F2C00W",
"DateTime": "05/18/2021"
}
Here is Schema_File.json
{
"$schema" :"http://json-schema.org/draft-04/schema",
"properties": {
"$schema": "#",
"Site": {
"type": "string"
},
"SN": {
"Type": "string"
},
"MN": {
"Type": "string"
}
}
}
$schema is only valid at the root, and properties values MUST be schemas.
You have a "$schema" : "#" inside properties. This means that you're trying to say that your JSON object should have a property called schema that can validate against the schema #. But # isn't a valid schema object, so the parse fails.
You need to remove the $schema from your properties.
I'd also suggest using a later draft of the schema spec (if you have control over the schema). Draft 6 is the oldest version that's compatible with the latest, 2020-12.
But for this you'll likely need to use a different validation package. There are several available. Mine is JsonSchema.Net.
In my Inno setup project, I need to parse a JSON. I Try to create a dll and parse JSON with the dll function but it gives me an access violation error after two days I could not find the reason for this access violation so I decide to parse JSON inside the Inno Setup.
I want to parse this JSON and Use the JsonParser library also here is two similar Questions
but I couldn't get it.
How to parse a JSON string in Inno Setup?
JSON arrays in inno setup
{
"Header":"Install FileType",
"Files":
[
{
"Filename":"SQL Server",
"FileType": 1,
"CheckExists":true,
},
{
"Filename":"Dot Net 3.5",
"FileType": 2,
"CheckExists":false,
}
]
}
I need this function
type
TJFile = record
FileName : string;
FileType : Integer;
CheckExists: Boolean;
GetFile(JSON: string; Index :Integer; var File: TJFile );
how can parse Json to get File with its index. Thank you for help and guid me.
I don't know Pascal Script, so I cannot really answer your question, but I notice that your JSON sample is not valid: there shouldn't be a comma after "true" (line 9), neither after "false" (line 14).
I am trying to convert JSON API response into a table in Excel using the Power Query functionality. I am currently getting the error below when I try to put the customerOrderHistory which is a list into a delimited list as I don't want to create extra rows for a list if I can avoid it.
If possible I would like to either
Just print the customerOrderHistory list in the JSON format that it is in already into the cell
OR
Create a delimited list of the values as the lists only contain one entry at the moment
The JSON test file looks like this:
{
"computerid": "1",
"total": 1,
"results": [
{
"computerid": "1",
"customerOrderHistory": [
{
"orderId": "1",
"channelId": null,
"agentId": null,
"orderItems": 1
}
]
}
]
}
Thanks
You can use Json.FromValue to convert the list into a Binary value, and then use Text.FromBinary to get back the string representation. If you can dig down deep enough to get to the customerOrderHistory field it would look like Text.FromBinary(Json.FromValue(result[customerOrderHistory])).
I'm asking this after some researchs on internet. There's a way to JSON type or XML type files to import Excel sheet but there's no clear descriptions found for my case. I would like to what is my goal;
I have a some type of string data like .txt file like this;
Example Data:
{"vehicle1": {
"title": "A super red vehicle",
"weight": "1500 kg",
"height": "2 m",
"vehicle1-center_of_gravity": [
{"x": "2 m"},
{"y": "-0.5 m"},
{"z": "1.5 m"}
"vehicle1-passenger_weights": [
{"p1": "2 m"},
{"p2": "-0.5 m"},
{"p3": "1.5 m"}
"color": "red",
]
}
}}
I would like to select this file with file dialog and then click to read button now my question comes in here. Is it possible to link/map them with cells for example i have cell named vehicle_title and i want to map this cell with .txt file's "title": "A super red vehicle".
Do you have any experience or idea how can i implement that to my macro.
Regards.
You can use this library
https://github.com/VBA-tools/VBA-JSON
to parse JSON in VBA, but you'll need to do the mapping in code: there's no built-in configuration/wizard for this.
Sub Tester()
Dim j As Object, json As String, v
'read from file
json = CreateObject("scripting.filesystemobject").OpenTextFile( _
ThisWorkbook.Path & "\example.txt").ReadAll()
'import module from: https://github.com/VBA-tools/VBA-JSON
Set j = JsonConverter.ParseJson(json)
Set v = j("vehicle1")
'some attributes...
Debug.Print v("title")
Debug.Print v("weight")
Debug.Print v("vehicle1-center_of_gravity")(1)("x")
End Sub
PS there are a few syntax errors in your JSON sample.
I have a function in scala that translates a value and produces a string.
strOut = translate(strIn)
Suppose the following JSON object:
{
"id": "c730433b-082c-4984-3d56-855c243265f0",
"standard": "stda",
"timestamp": "tsx000",
"stdparms" : {
"stdparam1": "a",
"stdparam2": "b"
}
}
and the following mapping provided by the translation function:
"stda" -> "stdb"
"tsx000" -> "tsy000"
"a" -> "f"
"b" -> "g"
What is the best way to translate the whole JSON object using the translate function? My goal is to obtain the following result:
{
"id": "c730433b-082c-4984-3d56-855c243265f0",
"standard": "stdb",
"timestamp": "tsy000",
"stdparms" : {
"stdparam1": "f",
"stdparam2": "g"
}
}
I must use the io.circe library due to project related matters.
If you know beforehand which fields you want to translate, or what translations apply to that field, you can use Cursors to traverse the JSON tree. Or if the fields themselves are fixed (you always know what fields to expect) Optics may require less code.
When you get to the right leaf, you apply the translation.
However, when you don't know what could apply when/where it might be easier to find/replace using string methods.
Note that the JSON you provided as an example is not valid JSON by the way.