.AddNew
!fruit = item("fruit")
!Size = item("size")
!Color = item("color")
.Update
.AddNew
!fruit = json("fruit")
!Size = json("size")
!Color = json("color")
.Update
magicYou have these lines:
Try changing them into these:Code:.AddNew !fruit = item("fruit") !Size = item("size") !Color = item("color") .Update
Code:.AddNew !fruit = json("fruit") !Size = json("size") !Color = json("color") .Update
{
"firstName": "Rack",
"lastName": "Jackon",
"gender": "man",
"age": 24,
"address": {
"streetAddress": "126",
"city": "San Jone",
"state": "CA",
"postalCode": "394221"
},
"phoneNumbers": [
{ "type": "home", "number": "7383627627" }
]
}
This is how you would go though them:for address and phoneNumbers
What should I do to convert to an access table?
firstName,lastname, age is very easy same design
Private Sub xxx()
Dim str As String
str = _
"{" & _
"'firstName': 'Rack'," & _
"'lastName': 'Jackon'," & _
"'gender': 'man'," & _
"'age': 24," & _
"'address': {" & _
" 'streetAddress': '126'," & _
" 'city': 'San Jone'," & _
" 'state': 'CA'," & _
" 'postalCode': '394221'" & _
"}," & _
"'phoneNumbers': [" & _
" { 'type': 'home', 'number': '7383627627' }" & _
"]" & _
"}"
Dim json As Object
Set json = JsonConverter.ParseJson(str)
Dim item As Variant
Dim subItem As Variant
' address is a key with an embedded object
For Each item In json("address")
Debug.Print item, json("address")(item)
Next item
' phoneNumbers is a key with an embedded array of objects
For Each item In json("phoneNumbers")
For Each subItem In item
Debug.Print subItem, item(subItem)
Next subItem
Next item
End Sub
This is how you would go though them:
Code:Private Sub xxx() Dim str As String str = _ "{" & _ "'firstName': 'Rack'," & _ "'lastName': 'Jackon'," & _ "'gender': 'man'," & _ "'age': 24," & _ "'address': {" & _ " 'streetAddress': '126'," & _ " 'city': 'San Jone'," & _ " 'state': 'CA'," & _ " 'postalCode': '394221'" & _ "}," & _ "'phoneNumbers': [" & _ " { 'type': 'home', 'number': '7383627627' }" & _ "]" & _ "}" Dim json As Object Set json = JsonConverter.ParseJson(str) Dim item As Variant Dim subItem As Variant ' address is a key with an embedded object For Each item In json("address") Debug.Print item, json("address")(item) Next item ' phoneNumbers is a key with an embedded array of objects For Each item In json("phoneNumbers") For Each subItem In item Debug.Print subItem, item(subItem) Next subItem Next item End Sub
I don't know your table structure.
CREATE TABLE Persons (
person_id AUTOINCREMENT PRIMARY KEY,
first_name TEXT,
last_name TEXT,
gender TEXT,
age INT
);
CREATE TABLE Addresses (
address_id AUTOINCREMENT PRIMARY KEY,
person_id INT,
street_address TEXT,
city TEXT,
state TEXT,
postal_code TEXT,
FOREIGN KEY (person_id) REFERENCES Persons(person_id)
);
CREATE TABLE PhoneNumbers (
phone_number_id AUTOINCREMENT PRIMARY KEY,
person_id INT,
type TEXT,
number TEXT,
FOREIGN KEY (person_id) REFERENCES Persons(person_id)
);
CREATE TABLE Persons (
person_id AUTOINCREMENT PRIMARY KEY,
first_name TEXT,
last_name TEXT,
gender TEXT,
age INT,
street_address TEXT,
city TEXT,
state TEXT,
postal_code TEXT
);
CREATE TABLE PhoneNumbers (
phone_number_id AUTOINCREMENT PRIMARY KEY,
person_id INT,
type TEXT,
number TEXT,
FOREIGN KEY (person_id) REFERENCES Persons(person_id)
);
thanksI see what you mean. I think you have 2 options:
1. Schema with Separate "Persons," "Addresses," and "PhoneNumbers" Tables:
Code:CREATE TABLE Persons ( person_id AUTOINCREMENT PRIMARY KEY, first_name TEXT, last_name TEXT, gender TEXT, age INT ); CREATE TABLE Addresses ( address_id AUTOINCREMENT PRIMARY KEY, person_id INT, street_address TEXT, city TEXT, state TEXT, postal_code TEXT, FOREIGN KEY (person_id) REFERENCES Persons(person_id) ); CREATE TABLE PhoneNumbers ( phone_number_id AUTOINCREMENT PRIMARY KEY, person_id INT, type TEXT, number TEXT, FOREIGN KEY (person_id) REFERENCES Persons(person_id) );
2. Schema with Address Fields Included in "Persons" Table:
Code:CREATE TABLE Persons ( person_id AUTOINCREMENT PRIMARY KEY, first_name TEXT, last_name TEXT, gender TEXT, age INT, street_address TEXT, city TEXT, state TEXT, postal_code TEXT ); CREATE TABLE PhoneNumbers ( phone_number_id AUTOINCREMENT PRIMARY KEY, person_id INT, type TEXT, number TEXT, FOREIGN KEY (person_id) REFERENCES Persons(person_id) );
As you can see, TEXT should be used to store those fields.
If you're getting this error,Can you check attached db and very simple JSON file
here is a very simple file but an error appears
If you're getting this error,
View attachment 110009
then try using the attached updated json file instead.
However, your code still needs to be modified, because you will eventually get this error next.
View attachment 110011
I used to use that JSON parser too but changed to a different one, since I had issues with it earlier as well.Thanks for your efforts
I don't know how to solve type mismatch
I think converting JSON is not easy
I used to use that JSON parser too but changed to a different one, since I had issues with it earlier as well.
Where are you getting your JSON files? They don't seem to be well-formed.
I think converting JSON is not easy
Sub readmyfile()
Dim FilePath As String
Dim RawContent As String
Dim FileNumber As Integer
FilePath = CurrentProject.Path & "\myjson3.json"
' get available number of file
FileNumber = FreeFile
Open FilePath For Input As FileNumber
' read 100 first chars
RawContent = Input$(100, FileNumber)
Close FileNumber
Debug.Print RawContent
End Sub
{
"Sheet1": [
{
"Serial Number": "9788189999599",
"Company Name": "TALES OF SHIVA",
"Emplo
Option Compare Database
Option Explicit
Private Sub Command0_Click()
ImportJSON
End Sub
Sub ImportJSON()
Dim FilePath As String
Dim RawContent As String
Dim FinalContent As String
Dim FileNumber As Long
Dim JsonStart As Long
FilePath = CurrentProject.Path & "\myjson3.json"
FileNumber = FreeFile
Open FilePath For Input As FileNumber
' read all
RawContent = Input$(LOF(1), FileNumber)
Close FileNumber
' find the first curly bracket
JsonStart = InStr(1, RawContent, "{")
' the real start is at the curly bracket
FinalContent = Mid(RawContent, JsonStart)
Dim json As Object
Dim arr As Variant
Dim obj As Variant
Set json = JsonConverter.ParseJson(FinalContent)
' import
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("Table2", dbOpenTable)
For Each arr In json
For Each obj In json(arr)
rs.AddNew
rs![Serial Number] = obj("Serial Number")
rs![Company Name] = obj("Company Name")
rs![Employee Markme] = obj("Employee Markme")
rs![Description] = obj("Description")
rs![Leave] = obj("Leave")
rs.Update
Next obj
Next arr
End Sub