diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cee1840 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# windsurf rules +.windsurfrules diff --git a/LICENSE b/LICENSE index 231f56f..c3a6562 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 kake26 +Copyright (c) 2024 Paul Malcher Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/README.md b/README.md index bc9839c..7d408d5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,25 @@ -# pbjsonlib +## Work in progress -A JSON handling library for purebasic \ No newline at end of file +Works pretty well at current. Not 100%, but I'm managing to use the code in other projects just fine. Use at your own risk. + +## What it does + +Converts JSON to data in a SQLite database. Makes it way easier to search and manipulate. You are responsible for of course doing the seraches and what not yourself. + +## Database layout + +``` + +CREATE TABLE "json_data" ( + "id" INTEGER, + "key" TEXT, + "parent_key" INTEGER, + "type" TEXT, + "value" TEXT, + PRIMARY KEY("id") +); +``` + +## Notes + +You need to either use the blank database provide ore create your own off the SQL above. It currently doesn't auto generate it. diff --git a/json.db b/json.db new file mode 100755 index 0000000..1fa51c4 Binary files /dev/null and b/json.db differ diff --git a/jsonlib.pb b/jsonlib.pb new file mode 100755 index 0000000..0f10fbb --- /dev/null +++ b/jsonlib.pb @@ -0,0 +1,164 @@ +UseSQLiteDatabase() +OpenDatabase(0,"json.db","","",#PB_Database_SQLite) +;TODO:Make sure the database exists + +Procedure InsertIntoDatabase(key$, parentKey$, type$, value$) + query$ = "INSERT INTO json_data (key, parent_key, type, value) VALUES ('" + key$ + "', '" + parentKey$ + "', '" + type$ + "', '" + value$ + "')" + If DatabaseUpdate(0, query$) = 0 + Debug "Error inserting data: " + DatabaseError() + EndIf +EndProcedure + +; Nested object handling +Procedure HandleNestedObject(JSONValue, parentKey$) + If ExamineJSONMembers(JSONValue) + While NextJSONMember(JSONValue) + key$ = JSONMemberKey(JSONValue) + MemberValue = JSONMemberValue(JSONValue) + Debug MemberValue + ; Process the member value as before + ; Now extract the actual value based on its type + Select JSONType(MemberValue) + Case #PB_JSON_String + value$ = GetJSONString(MemberValue) + type$ = "String" + + Case #PB_JSON_Number + value$ = StrD(GetJSONDouble(MemberValue)) + type$ = "Number" + + Case #PB_JSON_Boolean + value$ = Str(Bool(GetJSONBoolean(MemberValue))) ;Feels cheaty, but it workssss + type$ = "Boolean" + + Case #PB_JSON_Object + value$ = "(nested object)" + ;Debug key$ + parentKey$ = key$ + type$ = "object" + ; You could further examine this nested object if needed. + ;HandleNestedObject(MemberValue, key$) + + Case #PB_JSON_Array + value$ = "(nested array)" + type$ = "Array" + ; Handle array values if needed. + ;HandleNestedArray(MemberValue, key$) + + Default + value$ = "(unknown type)" + type$ = "Unknown" + + EndSelect + ;InsertIntoDatabase(key$, parentKey$, "Object", "(nested object)") ; Adjust type as necessary + InsertIntoDatabase(key$,parentKey$,type$,value$) + Wend + EndIf +EndProcedure + +; Nested array handling +Procedure HandleNestedArray(JSONValue, parentKey$) + arraySize = JSONArraySize(JSONValue) + + For arrayIndex = 0 To arraySize - 1 + elementValue = GetJSONElement(JSONValue, arrayIndex) + arrayItemKey$ = parentKey$ + "[" + Str(arrayIndex) + "]" + + Select JSONType(elementValue) + Case #PB_JSON_String + value$ = GetJSONString(elementValue) + InsertIntoDatabase(arrayItemKey$, parentKey$, "String", value$) + + Case #PB_JSON_Number + value$ = StrD(GetJSONDouble(elementValue)) + InsertIntoDatabase(arrayItemKey$, parentKey$, "Number", value$) + + Case #PB_JSON_Boolean + value$ = Str(GetJSONBoolean(elementValue)) + InsertIntoDatabase(arrayItemKey$, parentKey$, "Boolean", value$) + + Case #PB_JSON_Object + InsertIntoDatabase(arrayItemKey$, parentKey$, "Object", "(nested object)") + HandleNestedObject(elementValue, arrayItemKey$) + + Case #PB_JSON_Array + InsertIntoDatabase(arrayItemKey$, parentKey$, "Array", "(nested array)") + HandleNestedArray(elementValue, arrayItemKey$) + + Default + InsertIntoDatabase(arrayItemKey$, parentKey$, "Unknown", "(unknown type)") + EndSelect + Next +EndProcedure + + +Procedure parse(json.s,filename.s,parentKey$) +; worry about the database init later + + If json.s + ParseJSON(1,json.s) + EndIf + + If filename.s + LoadJSON(1,filename.s) + EndIf + + ;LoadJSON(1,"test2.json") ; returns garbage without the PB ascii there not sure why + + + ObjectValue = JSONValue(1) + + If ExamineJSONMembers(ObjectValue) + While NextJSONMember(ObjectValue) + + key$ = JSONMemberKey(ObjectValue) + MemberValue = JSONMemberValue(ObjectValue) + ;parentKey$ = "" ; Assuming no parent key information is available in the current code. + + ; Now extract the actual value based on its type + Select JSONType(MemberValue) + Case #PB_JSON_String + value$ = GetJSONString(MemberValue) + type$ = "String" + + Case #PB_JSON_Number + value$ = StrD(GetJSONDouble(MemberValue)) + type$ = "Number" + + Case #PB_JSON_Boolean + value$ = Str(Bool(GetJSONBoolean(MemberValue))) ;Feels cheaty, but it workssss + type$ = "Boolean" + + Case #PB_JSON_Object + value$ = "(nested object)" + ;Debug key$ + parentKey$ = key$ + type$ = "object" + ; You could further examine this nested object if needed. + HandleNestedObject(MemberValue, key$) + + Case #PB_JSON_Array + value$ = "(nested array)" + type$ = "Array" + ; Handle array values if needed. + HandleNestedArray(MemberValue, key$) + + Default + value$ = "(unknown type)" + type$ = "Unknown" + + EndSelect + + + InsertIntoDatabase(key$,parentKey$,type$,value$) + + Wend +EndIf +EndProcedure +; IDE Options = PureBasic 6.12 LTS (Linux - x64) +; CursorPosition = 115 +; FirstLine = 83 +; Folding = - +; EnableXP +; DPIAware +; Executable = jsonlib \ No newline at end of file diff --git a/test files/generated.json b/test files/generated.json new file mode 100644 index 0000000..af0c536 --- /dev/null +++ b/test files/generated.json @@ -0,0 +1,227 @@ +[ + { + "_id": "6759053cfd9bb8b889d25e97", + "index": 0, + "guid": "2484bb50-2b68-41ad-9eb9-40f828b80ad1", + "isActive": true, + "balance": "$2,009.35", + "picture": "http://placehold.it/32x32", + "age": 35, + "eyeColor": "green", + "name": "Barrera Dean", + "gender": "male", + "company": "PHOTOBIN", + "email": "barreradean@photobin.com", + "phone": "+1 (808) 414-2010", + "address": "844 Woodbine Street, Galesville, North Carolina, 1698", + "about": "Et nisi dolor ad ea dolor laboris do pariatur. Reprehenderit esse pariatur velit duis non nostrud culpa eu quis. Id in duis ullamco cupidatat id mollit in incididunt duis cupidatat.\r\n", + "registered": "2018-11-05T05:22:00 +06:00", + "latitude": 26.643538, + "longitude": 176.415317, + "tags": [ + "eu", + "labore", + "laboris", + "excepteur", + "ex", + "quis", + "elit" + ], + "friends": [ + { + "id": 0, + "name": "Trina Cannon" + }, + { + "id": 1, + "name": "Lara Washington" + }, + { + "id": 2, + "name": "Jarvis Allen" + } + ], + "greeting": "Hello, Barrera Dean! You have 10 unread messages.", + "favoriteFruit": "apple" + }, + { + "_id": "6759053c6c1c5418e2f38a10", + "index": 1, + "guid": "ab775ba0-2153-4d8f-86e1-308529258b74", + "isActive": false, + "balance": "$3,088.84", + "picture": "http://placehold.it/32x32", + "age": 22, + "eyeColor": "green", + "name": "Britt Blevins", + "gender": "male", + "company": "INTERLOO", + "email": "brittblevins@interloo.com", + "phone": "+1 (908) 468-2278", + "address": "135 Amity Street, Watrous, Oklahoma, 4368", + "about": "Ipsum laboris dolor laboris cillum aliquip sit exercitation quis adipisicing elit ea magna reprehenderit. Excepteur commodo aliquip do ea voluptate id dolore ex. Ad dolor occaecat ea culpa proident esse dolor eiusmod adipisicing ullamco do nulla aliqua. Elit reprehenderit cupidatat qui culpa reprehenderit aliquip minim elit ad cillum. Laborum enim et eiusmod Lorem. Id sunt in et ea deserunt est tempor id nisi. Qui est incididunt est mollit reprehenderit aute incididunt reprehenderit anim mollit consequat reprehenderit nisi officia.\r\n", + "registered": "2015-10-12T01:24:27 +05:00", + "latitude": 79.881303, + "longitude": -26.350194, + "tags": [ + "sit", + "in", + "aliquip", + "qui", + "id", + "est", + "occaecat" + ], + "friends": [ + { + "id": 0, + "name": "Angelique Steele" + }, + { + "id": 1, + "name": "Cynthia Manning" + }, + { + "id": 2, + "name": "Teri Bates" + } + ], + "greeting": "Hello, Britt Blevins! You have 8 unread messages.", + "favoriteFruit": "apple" + }, + { + "_id": "6759053cd64cbda886c6ab89", + "index": 2, + "guid": "403c3dd2-cad2-4273-a865-6fc5f2754ef5", + "isActive": true, + "balance": "$2,078.44", + "picture": "http://placehold.it/32x32", + "age": 37, + "eyeColor": "blue", + "name": "Lena Thompson", + "gender": "female", + "company": "XUMONK", + "email": "lenathompson@xumonk.com", + "phone": "+1 (817) 597-2083", + "address": "688 Mermaid Avenue, Robbins, Virgin Islands, 6242", + "about": "Laborum nisi consectetur proident dolor incididunt sint irure fugiat ipsum proident reprehenderit proident velit dolore. Exercitation cupidatat ea amet deserunt incididunt minim proident reprehenderit Lorem qui nulla in consectetur. Exercitation sit aliquip ad reprehenderit minim Lorem Lorem nulla commodo aliqua officia dolor. Lorem ex adipisicing nisi aliqua reprehenderit occaecat esse ad adipisicing eiusmod Lorem.\r\n", + "registered": "2021-06-10T07:55:03 +05:00", + "latitude": -43.756374, + "longitude": -10.446363, + "tags": [ + "excepteur", + "commodo", + "enim", + "nostrud", + "anim", + "in", + "ut" + ], + "friends": [ + { + "id": 0, + "name": "Bell Short" + }, + { + "id": 1, + "name": "Florence Keller" + }, + { + "id": 2, + "name": "Hester Brooks" + } + ], + "greeting": "Hello, Lena Thompson! You have 8 unread messages.", + "favoriteFruit": "strawberry" + }, + { + "_id": "6759053c0383979f437dacc4", + "index": 3, + "guid": "6cb78d37-e4d5-42f8-98a9-e77df540b2ad", + "isActive": false, + "balance": "$1,091.13", + "picture": "http://placehold.it/32x32", + "age": 20, + "eyeColor": "blue", + "name": "Eula Snider", + "gender": "female", + "company": "ZOARERE", + "email": "eulasnider@zoarere.com", + "phone": "+1 (968) 560-2719", + "address": "898 Harman Street, Cowiche, Utah, 3136", + "about": "In ullamco commodo aliqua sunt sit eiusmod veniam cupidatat laborum consectetur. Fugiat sit nulla commodo aliqua laboris. Eu pariatur officia nisi dolor sit qui ad minim pariatur do eu non pariatur. Dolore incididunt magna minim sit occaecat magna est amet eu consequat esse. Do qui sunt ipsum reprehenderit sunt non ad esse magna deserunt velit dolor. Nostrud veniam ex enim amet nisi ipsum do commodo sunt consequat. Ea enim voluptate sit reprehenderit sint nostrud laborum et aliquip cupidatat reprehenderit qui.\r\n", + "registered": "2023-08-01T10:45:44 +05:00", + "latitude": 26.11414, + "longitude": 55.526199, + "tags": [ + "commodo", + "elit", + "reprehenderit", + "consectetur", + "esse", + "occaecat", + "esse" + ], + "friends": [ + { + "id": 0, + "name": "Robertson Holman" + }, + { + "id": 1, + "name": "Dennis Gallagher" + }, + { + "id": 2, + "name": "Sasha Rich" + } + ], + "greeting": "Hello, Eula Snider! You have 5 unread messages.", + "favoriteFruit": "banana" + }, + { + "_id": "6759053cf65531799c078ed9", + "index": 4, + "guid": "bcfe6552-066d-4c02-8df3-2518af71d8bc", + "isActive": false, + "balance": "$3,321.31", + "picture": "http://placehold.it/32x32", + "age": 26, + "eyeColor": "brown", + "name": "Mendez Hooper", + "gender": "male", + "company": "MAROPTIC", + "email": "mendezhooper@maroptic.com", + "phone": "+1 (893) 410-3668", + "address": "756 Dobbin Street, Imperial, Federated States Of Micronesia, 1605", + "about": "Incididunt ex mollit qui anim tempor irure dolor. Aliquip veniam irure ea enim sunt incididunt labore consequat. Laborum Lorem nulla laborum proident ipsum. Cupidatat qui cillum do excepteur et aute excepteur.\r\n", + "registered": "2023-09-25T11:12:44 +05:00", + "latitude": 18.757584, + "longitude": 92.109014, + "tags": [ + "reprehenderit", + "quis", + "incididunt", + "consequat", + "voluptate", + "eiusmod", + "occaecat" + ], + "friends": [ + { + "id": 0, + "name": "Keisha Guthrie" + }, + { + "id": 1, + "name": "Janelle Mills" + }, + { + "id": 2, + "name": "Noelle Solomon" + } + ], + "greeting": "Hello, Mendez Hooper! You have 4 unread messages.", + "favoriteFruit": "banana" + } +] \ No newline at end of file diff --git a/test files/hardtest.json b/test files/hardtest.json new file mode 100644 index 0000000..d59d4c6 --- /dev/null +++ b/test files/hardtest.json @@ -0,0 +1,78 @@ +[ + { + "id": "pbc_2040538604", + "listRule": null, + "viewRule": null, + "createRule": null, + "updateRule": null, + "deleteRule": null, + "name": "testing", + "type": "base", + "fields": [ + { + "autogeneratePattern": "[a-z0-9]{15}", + "hidden": false, + "id": "text3208210256", + "max": 15, + "min": 15, + "name": "id", + "pattern": "^[a-z0-9]+$", + "presentable": false, + "primaryKey": true, + "required": true, + "system": true, + "type": "text" + }, + { + "autogeneratePattern": "", + "hidden": false, + "id": "text724990059", + "max": 0, + "min": 0, + "name": "title", + "pattern": "", + "presentable": false, + "primaryKey": false, + "required": false, + "system": false, + "type": "text" + }, + { + "autogeneratePattern": "", + "hidden": false, + "id": "text494360628", + "max": 0, + "min": 0, + "name": "value", + "pattern": "", + "presentable": false, + "primaryKey": false, + "required": false, + "system": false, + "type": "text" + }, + { + "hidden": false, + "id": "autodate2990389176", + "name": "created", + "onCreate": true, + "onUpdate": false, + "presentable": false, + "system": false, + "type": "autodate" + }, + { + "hidden": false, + "id": "autodate3332085495", + "name": "updated", + "onCreate": true, + "onUpdate": true, + "presentable": false, + "system": false, + "type": "autodate" + } + ], + "indexes": [], + "system": false + } +] \ No newline at end of file diff --git a/test files/results.json b/test files/results.json new file mode 100755 index 0000000..52f90d6 --- /dev/null +++ b/test files/results.json @@ -0,0 +1 @@ +{"admin":{"email":"offal@pngpst.net","created":"2024-07-23 01:32:58.983Z","avatar":0,"updated":"2024-07-23 01:32:58.983Z","id":"xf9w05ts29nik6z"},"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzIzNDUwMjIsImlkIjoieGY5dzA1dHMyOW5pazZ6IiwidHlwZSI6ImFkbWluIn0.UM_QP4NVBdioMLU8mPJ9ZL0bq_ylLPQ6Jdn3AzNKjK0"} \ No newline at end of file diff --git a/test files/test2.json b/test files/test2.json new file mode 100644 index 0000000..151d6ba --- /dev/null +++ b/test files/test2.json @@ -0,0 +1,92 @@ +{ + "result": [ + { + "message": "Hello, Stephen! Your order number is: #78", + "phoneNumber": "1-331-957-3477", + "phoneVariation": "+90 341 719 10 46", + "status": "disabled", + "name": { + "first": "Carolina", + "middle": "Billie", + "last": "Macejkovic" + }, + "username": "Carolina-Macejkovic", + "password": "v5TPnbf09jOoOoj", + "emails": [ + "Katharina70@example.com", + "Hilda95@example.com" + ], + "location": { + "street": "65619 Bruen Valleys", + "city": "Waelchihaven", + "state": "Vermont", + "country": "Colombia", + "zip": "28035-5646", + "coordinates": { + "latitude": "-5.3148", + "longitude": "-57.1354" + } + }, + "website": "https://expensive-handful.biz/", + "domain": "dutiful-webinar.info", + "job": { + "title": "Chief Accountability Administrator", + "descriptor": "Corporate", + "area": "Tactics", + "type": "Designer", + "company": "Heidenreich, Feeney and Simonis" + }, + "creditCard": { + "number": "3528-1267-1942-1349", + "cvv": "481", + "issuer": "jcb" + }, + "uuid": "54690949-9af9-4d9d-b9c6-fbaec3a3e2bf", + "objectId": "675907a0b7b2381b643ed27e" + }, + { + "message": "Hello, Lavern! Your order number is: #97", + "phoneNumber": "681.884.7996 x9465", + "phoneVariation": "+90 306 528 10 72", + "status": "disabled", + "name": { + "first": "Kasey", + "middle": "Kennedy", + "last": "Kuhic" + }, + "username": "Kasey-Kuhic", + "password": "fwcMVNQbdcZTi56", + "emails": [ + "Freeman_Lowe@example.com", + "Eduardo.Hauck@example.com" + ], + "location": { + "street": "155 Deckow River", + "city": "East Claudeview", + "state": "Massachusetts", + "country": "Greenland", + "zip": "81479-7331", + "coordinates": { + "latitude": "4.8424", + "longitude": "-33.7905" + } + }, + "website": "https://gifted-humanity.net", + "domain": "giving-cd.com", + "job": { + "title": "Central Metrics Executive", + "descriptor": "Principal", + "area": "Data", + "type": "Analyst", + "company": "Kemmer LLC" + }, + "creditCard": { + "number": "3686-229621-2039", + "cvv": "034", + "issuer": "mastercard" + }, + "uuid": "e1a066bc-7242-4217-bda9-b65554aa3cb2", + "objectId": "675907a0b7b2381b643ed27f" + } + ] +} diff --git a/test files/test3.json b/test files/test3.json new file mode 100644 index 0000000..dfeb310 --- /dev/null +++ b/test files/test3.json @@ -0,0 +1,22 @@ +[ + [ + "speed", + { + "fur": -825722984.7808421, + "shout": "sound", + "look": -1186004478.4914062, + "lonely": 2015750539.230037, + "outside": "afternoon", + "dot": false + }, + "rocket", + 587090806, + "butter", + "sport" + ], + 1778572520.5735526, + "weigh", + false, + 1090297490, + -272114585 +] diff --git a/test files/test4.json b/test files/test4.json new file mode 100644 index 0000000..8a6abab --- /dev/null +++ b/test files/test4.json @@ -0,0 +1,6 @@ +{ + "test": { + "test 1": "", + "test 2": "cow" + } +} diff --git a/test files/test5.json b/test files/test5.json new file mode 100644 index 0000000..842f113 --- /dev/null +++ b/test files/test5.json @@ -0,0 +1,50 @@ +{ + "id": "67590fb582b5b741e2c310de", + "children": [ + { + "name": "Jude Battle", + "age": 9 + }, + { + "name": "Tandy Christensen", + "age": 1 + }, + { + "name": "Taylor Bright", + "age": 5 + }, + { + "name": "Jesse Austin", + "age": 6 + }, + { + "name": "Nico Gould", + "age": 7 + } + ], + "currentJob": { + "title": "Developer", + "salary": "mask;" + }, + "jobs": [ + { + "title": "developer", + "salary": "R$ 0.441,41" + }, + { + "title": "CEO", + "salary": "R$ 0.826,12" + } + ], + "maxRunDistance": 9.9, + "cpf": "301.233.354-18", + "cnpj": "34.272.366/0001-31", + "pretendSalary": "R$ 6.174,80", + "age": 35, + "gender": "female", + "firstName": "Mildred", + "lastName": "Kline", + "phone": "+55 (83) 95834-3087", + "address": "559 Norwood Avenue - Elbert, North Carolina, Latvia.", + "hairColor": "orange" +} diff --git a/variants/jsonlib copy.pb b/variants/jsonlib copy.pb new file mode 100755 index 0000000..d419660 --- /dev/null +++ b/variants/jsonlib copy.pb @@ -0,0 +1,66 @@ +UseSQLiteDatabase() + +; worry about the database init later + +OpenDatabase(0,"json.db","","",#PB_Database_SQLite) + + LoadJSON(1,"results.json");ParseJSON(1, json$) ; returns garbage without the PB ascii there not sure why + + ParseJSON(1, json$) ; returns garbage without the PB ascii there not sure why + ObjectValue = JSONValue(1) + + If ExamineJSONMembers(ObjectValue) + While NextJSONMember(ObjectValue) + + Debug "Key: " + JSONMemberKey(ObjectValue) + + MemberValue = JSONMemberValue(ObjectValue) + + + ; Now extract the actual value based on its type + Select JSONType(MemberValue) + Case #PB_JSON_String + Debug "Value (String): " + GetJSONString(MemberValue) + + + Case #PB_JSON_Number + Debug "Value (Number): " + StrD(GetJSONDouble(MemberValue)) + + + Case #PB_JSON_Boolean + Debug "Value (Boolean): " + Bool(GetJSONBoolean(MemberValue)) + + + Case #PB_JSON_Object + Debug "Value (Object): (nested object)" + ; You could further examine this nested object if needed. + + + Case #PB_JSON_Array + Debug "Value (Array): (nested array)" + ; Handle array values if needed. + + + Default + Debug "Value (Unknown type)" + + EndSelect + + + Wend + +endif + + + + +; IDE Options = PureBasic 6.12 LTS (Linux - x64) +; CursorPosition = 43 +; FirstLine = 33 +; Folding = - +; EnableXP +; DPIAware +; IDE Options = PureBasic 6.12 LTS (Linux - x64) +; CursorPosition = 9 +; EnableXP +; DPIAware \ No newline at end of file diff --git a/variants/jsonlib2.pb b/variants/jsonlib2.pb new file mode 100755 index 0000000..248de36 --- /dev/null +++ b/variants/jsonlib2.pb @@ -0,0 +1,97 @@ +UseSQLiteDatabase() + +Procedure searchkey() +searchKey$ = "your.key.path" +query$ = "SELECT * FROM json_data WHERE key = '" + searchKey$ + "'" + +If DatabaseQuery(0, query$) + While NextDatabaseRow(0) + Debug "Key: " + GetDatabaseString(0, 0) + Debug "Parent Key: " + GetDatabaseString(0, 1) + Debug "Type: " + GetDatabaseString(0, 2) + Debug "Value: " + GetDatabaseString(0, 3) + Wend + FinishDatabaseQuery(0) +Else + Debug "Error executing query: " + DatabaseError() +EndIf + +EndProcedure + +Procedure searchval() + searchValue$ = "specific value" +query$ = "SELECT * FROM json_data WHERE value = '" + searchValue$ + "'" + +If DatabaseQuery(0, query$) + While NextDatabaseRow(0) + Debug "Key: " + GetDatabaseString(0, 0) + Debug "Parent Key: " + GetDatabaseString(0, 1) + Debug "Type: " + GetDatabaseString(0, 2) + Debug "Value: " + GetDatabaseString(0, 3) + Wend + FinishDatabaseQuery(0) +Else + Debug "Error executing query: " + DatabaseError() +EndIf + EndProcedure + + +Procedure ProcessJSONObject(ObjectValue, parentKey$) + If ExamineJSONMembers(ObjectValue) + While NextJSONMember(ObjectValue) + key$ = JSONMemberKey(ObjectValue) + MemberValue = JSONMemberValue(ObjectValue) + fullKey$ = parentKey$ + key$ + + Select JSONType(MemberValue) + Case #PB_JSON_String + value$ = GetJSONString(MemberValue) + type$ = "String" + + Case #PB_JSON_Number + value$ = StrD(GetJSONDouble(MemberValue)) + type$ = "Number" + + Case #PB_JSON_Boolean + value$ = Str(Bool(GetJSONBoolean(MemberValue))) + type$ = "Boolean" + + Case #PB_JSON_Object + value$ = "(nested object)" + type$ = "Object" + ; Recursively process the nested object + ProcessJSONObject(MemberValue, fullKey$ + ".") + + Case #PB_JSON_Array + value$ = "(nested array)" + type$ = "Array" + ; Handle array values if needed. + + Default + value$ = "(unknown type)" + type$ = "Unknown" + + EndSelect + + ; Insert the parsed data into the SQLite database + query$ = "INSERT INTO json_data (key, parent_key, type, value) VALUES ('" + fullKey$ + "', '" + parentKey$ + "', '" + type$ + "', '" + value$ + "')" + If DatabaseUpdate(0, query$) = 0 + Debug "Error inserting data: " + DatabaseError() + EndIf + Wend + EndIf +EndProcedure + +OpenDatabase(0, "json.db", "", "", #PB_Database_SQLite) + +LoadJSON(1, "results.json") +ParseJSON(1, json$) ; returns garbage without the PB ascii there not sure why +ObjectValue = JSONValue(1) + +ProcessJSONObject(ObjectValue, "") +; IDE Options = PureBasic 6.12 LTS (Linux - x64) +; CursorPosition = 37 +; FirstLine = 11 +; Folding = - +; EnableXP +; DPIAware \ No newline at end of file diff --git a/variants/jsontest.pb b/variants/jsontest.pb new file mode 100644 index 0000000..3ccaf82 --- /dev/null +++ b/variants/jsontest.pb @@ -0,0 +1,11 @@ +; library test code + +XIncludeFile "jsonlib.pb" + +parse("","test2.json") + + +; IDE Options = PureBasic 6.12 LTS (Linux - x64) +; CursorPosition = 6 +; EnableXP +; DPIAware \ No newline at end of file