Skip to content
\n

where the \"dbJson\" is imported from a valid Json data schema file.
\nThe CapacitorSQLite.isJsonValid gives error \"Error: IsJsonValid: Stringify Json Object not Valid\"

\n

I tried another Json schema file which works well in my another project with plugin version 4.1.0, but version 4.6.0 gives the error.

\n

Then I tried using the sample data which is jsonSQLite object in \"https://github.com/capacitor-community/sqlite/blob/master/docs/ImportExportJson.md\", then tested in the code below which works ok:

\n

Test 2:

\n
 const jsonstring = JSON.stringify(dataToImport);.\n const isValid = await CapacitorSQLite.isJsonValid({ jsonstring: jsonstring});\n\n
\n

where the \"dataToImport\" is the sample data (jsonSQLite object) provided in the above link.

\n

Not finished yet.

\n

Then I replaced the Json file with the string generated from JSON.stringify(dataToImport) and then retried from my code in \"Test 1\",
\nInteresting thing is that CapacitorSQLite.isJsonValid still gives error \"Error: IsJsonValid: Stringify Json Object not Valid\".

\n

Can some please help this?

\n

Cheers,
\nQiuwei

","upvoteCount":1,"answerCount":9,"acceptedAnswer":{"@type":"Answer","text":"

@storynana Thank for waiting
\ni did that

\n\n
    \"resolveJsonModule\": true,\n    \"allowSyntheticDefaultImports\": true,\n
\n\n
   ...\nimport { of } from 'rxjs';\nimport dbJson from 'src/assets/databases/testData-schema.db.json';\nimport { JsonSQLite, CapacitorSQLite } from '@capacitor-community/sqlite';\n  ...\n\nngOnInit() {\n  ...\n    of(dbJson).subscribe(\n      async (jsonExport: JsonSQLite) => {\n      const jsonstring = JSON.stringify(jsonExport);\n      console.log(jsonstring);\n      const isValid = await CapacitorSQLite.isJsonValid({ jsonstring: jsonstring });\n      console.log(`-- after is JsonValid isValid.result: ${isValid.result}`);\n      if (isValid.result) {\n        console.log(\"--------before-------\");\n        await CapacitorSQLite.importFromJson({ jsonstring });\n        console.log(\"--------after-------\");\n      }\n    });\n  ...\n  }\n
\n

and i got in the console

\n
home.page.ts:35 {\"database\":\"db-from-json\",\"version\":1,\"encrypted\":false,\"mode\":\"full\",\"tables\":[{\"name\":\"users\",\"schema\":[{\"column\":\"id\",\"value\":\"INTEGER PRIMARY KEY NOT NULL\"},{\"column\":\"email\",\"value\":\"TEXT UNIQUE NOT NULL\"},{\"column\":\"name\",\"value\":\"TEXT\"},{\"column\":\"age\",\"value\":\"INTEGER\"},{\"column\":\"sql_deleted\",\"value\":\"BOOLEAN DEFAULT 0 CHECK (sql_deleted IN (0, 1))\"},{\"column\":\"last_modified\",\"value\":\"INTEGER DEFAULT (strftime('%s', 'now'))\"}],\"indexes\":[{\"name\":\"index_user_on_name\",\"value\":\"name\"},{\"name\":\"index_user_on_last_modified\",\"value\":\"last_modified DESC\"},{\"name\":\"index_user_on_email_name\",\"value\":\"email ASC, name\",\"mode\":\"UNIQUE\"}],\"values\":[[1,\"Whiteley.com\",\"Whiteley\",30,0,1582536810],[2,\"Jones.com\",\"Jones\",44,0,1582812800],[3,\"[email protected]\",\"Simpson\",69,0,1583570630],[4,\"[email protected]\",\"Brown\",15,0,1590383895]]},{\"name\":\"messages\",\"schema\":[{\"column\":\"id\",\"value\":\"INTEGER PRIMARY KEY NOT NULL\"},{\"column\":\"userid\",\"value\":\"INTEGER\"},{\"column\":\"title\",\"value\":\"TEXT NOT NULL\"},{\"column\":\"body\",\"value\":\"TEXT NOT NULL\"},{\"column\":\"sql_deleted\",\"value\":\"BOOLEAN DEFAULT 0 CHECK (sql_deleted IN (0, 1))\"},{\"column\":\"last_modified\",\"value\":\"INTEGER DEFAULT (strftime('%s', 'now'))\"},{\"foreignkey\":\"userid\",\"value\":\"REFERENCES users(id) ON DELETE CASCADE\"}],\"indexes\":[{\"name\":\"index_messages_on_title\",\"value\":\"title\"},{\"name\":\"index_messages_on_last_modified\",\"value\":\"last_modified DESC\"}],\"values\":[[1,1,\"test post 1\",\"content test post 1\",0,1587310030],[2,2,\"test post 2\",\"content test post 2\",0,1590388125],[3,1,\"test post 3\",\"content test post 3\",0,1590383895]]},{\"name\":\"images\",\"schema\":[{\"column\":\"id\",\"value\":\"INTEGER PRIMARY KEY NOT NULL\"},{\"column\":\"name\",\"value\":\"TEXT UNIQUE NOT NULL\"},{\"column\":\"type\",\"value\":\"TEXT NOT NULL\"},{\"column\":\"size\",\"value\":\"INTEGER\"},{\"column\":\"img\",\"value\":\"BLOB\"},{\"column\":\"sql_deleted\",\"value\":\"BOOLEAN DEFAULT 0 CHECK (sql_deleted IN (0, 1))\"},{\"column\":\"last_modified\",\"value\":\"INTEGER DEFAULT (strftime('%s', 'now'))\"}],\"indexes\":[{\"name\":\"index_images_on_last_modified\",\"value\":\"last_modified DESC\"}],\"values\":[[1,\"meowth\",\"png\",\"NULL\",\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAkCAYAAAD7PHgWAAAEcElEQVRYR8WYP2hTQRzHfx10aQchi0JcLGpBSBcrlTrpIjoFiy6FDipOHVz8Q0HrUGxdg1N1KBRBackiVoQ6FMVIuzQgpEpdjOiSLUXQIfK9976X37t3l6RNxVuS3Hvv7nPf3+/3vcvraTQaDdlFK4z3yMT8rh7d0Ww97QAzfX12wFq9br4buOk7UpicaQm5F4toCajh9LKnLm23Bex0Ee3k7ArwS/mVvH5elqEzzWmGr0dhDwGGFs3ouMAdA7491y+Dhw5KZuG9UEEA1r6XZfhUPOxgQ0pzPQJIDTi11NtOKOkKkHCcpfDrjQlxaXnGdFE1fAcg2to7sWmgAfVYWCzbPwO06imNHt0Tyd/IyfDlrYRy7kI3fvyUsyvRPbsCxIPIGQ6MAdFWD5RbKnjxZhTSWn0+AqyuS2agEPWNjZhPjrUngBgQkABDQ3hNOJdnmvkXa5UZ6W2CxXBaRoBiLLR2cLgnUSRIbOSLlptVx8LQk7k5iHutah44Pks12+VfApBVh04YsAbV1yR7sslYXU+oSPUK46NWZWPmseJdATLfTJ5UJsxYBNXqoc+EeX7RgpbmRmX1pcjsSq95VkP5AM1czMl63ViS27iNen2QYSUoH+bWVq1WpTh5OAFp1ekbtz7JRVJBPH/+Sk6O5i4YQCxc57Sbq0i1loA2R6hKfDho7rFLqZWzYvXiqCKgSi/6LSC+o7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kpPW19j8vLy7u7vvsDC9PT1cHR3Oj9Eo6WnxsjJR0tQOD1Bj5KVgYSHTVFWtri50dLUtLa4YmZqOT5D8vPzRUpOkZOWc3Z64uPjr7Gzuru95+jpX2NnaGxwPkNHp6mrioyPlZeadXh8Q0hNPEBFyszNh4qNc3d6eHx/OD1Cw8XGXGBkfoGEra+xxcbIgoaJu72/m52ggoWIZ2tu8/P0wcLE+vr7kZSXgIOGP0NIvr/BvL6/QUZKP0RJkpWYpKaoqKqtVVldmJqdl5qcZWhstbe5bHB0bnJ1UVVZwsTF5ubnT1RYcHN3oaSm3N3e3NzdQkdLnJ+h9fX1TlNX+Pj47/DwwsPFVFhcEpC44wAAAShJREFUeNq8k0VvxDAQhZOXDS52mRnKzLRlZmZm+v/HxmnUOlFaSz3su4xm/BkGzLn4P+XimOJZyw0FKufelfbfAe89dMmBBdUZ8G1eCJMba69Al+AABOOm/7j0DDGXtQP9bXjYN2tWGQfyA1Yg1kSu95x9GKHiIOBXLcAwUD1JJSBVfUbwGGi2AIvoneK4bCblSS8b0RwwRAPbCHx52kH60K1b9zQUjQKiULbMDbulEjGha/RQQFDE0/ezW8kR3C3kOJXmFcSyrcQR7FDAi55nuGABZkT5hqpk3xughDN7FOHHHd0LLU9qtV7r7uhsuRwt6pEJJFVLN4V5CT+SErpXt81DbHautkpBeHeaqNDRqUA0Uo5GkgXGyI3xDZ/q/wJMsb7/pwADAGqZHDyWkHd1AAAAAElFTkSuQmCC\",0,1590389895]]}]}\nhome.page.ts:37 -- after is JsonValid isValid.result: true\nhome.page.ts:39 --------before-------\nhome.page.ts:41 --------after-------\n
\n

meaning that it works fine.
\ni also checked that the database db-from-jsonSQLite.db was created

","upvoteCount":1,"url":"https://github.com/capacitor-community/sqlite/discussions/369#discussioncomment-4700108"}}}
Discussion options

You must be logged in to vote

@storynana Thank for waiting
i did that

  • copy the testData-schema.db.txt in the assets/databases folder of one of my app and rename it as .json file
  • add in the tsconfig.json inside the compilerOptions
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
  • edit my home page with
   ...
import { of } from 'rxjs';
import dbJson from 'src/assets/databases/testData-schema.db.json';
import { JsonSQLite, CapacitorSQLite } from '@capacitor-community/sqlite';
  ...

ngOnInit() {
  ...
    of(dbJson).subscribe(
      async (jsonExport: JsonSQLite) => {
      const jsonstring = JSON.stringify(jsonExport);
      console.log(jsonstring);
      const isValid = await CapacitorSQLit…

Replies: 9 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@storynana
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@whitnessme
Comment options

Answer selected by jepiqueau
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants