+ Reply to Thread
Results 1 to 12 of 12

Edit JSON parser code to add function to write JSON file

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Edit JSON parser code to add function to write JSON file

    Background information (Can be skipped if not required)

    I am interested in using VBA on JSON files.

    Since I am very weak on Java I searched the net for existing code. After testing & discarding several VBA projects, I finally found code that properly parsed the contents of JSONs. (Unfortunately I've forgotten where I found this code. If anybody recognises the code below, please let me know the website and the creator so I can add acknowledgement)

    This was working great - except for one minor issue that I soon solved (Footnote: This issue is because the code failed to return certain values. It would return them as [object]. The code would then retry fetching the data through use of an alternative function 'pfnobjGetObjectProperty'. This did not work for me. Through trial and error, I wrote a replacement function 'pfnvarGetObjectProperty_EXPERIMENTAL'. My new function successfully returns these types of values as a 2d array)

    Now the reason I am opening this thread.



    The Thread Question

    Using this modified JSON parsing code, I can read the contents of multiple JSON files into a global array. But I want to write some changes I have made in this array back to the JSON file(s) and I have no function for this.

    I need a function that will replace the entire contents of a single JSON file with my updated data for that file.

    The input data for this desired function will be in the same format/layout as what I am using in avarDbValues in the function 'fnavarReadMultipleJSONsIntoAnArray'

    (I plan to extract the edited JSON array from the global array and then pass this to the 'write JSON' function.)


    Please Login or Register  to view this content.
    Last edited by mc84excel; 01-28-2020 at 09:25 PM. Reason: add 2 lines to code to explain how to test it
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Edit JSON parser code to add function to write JSON file

    You’d be better off using this: https://github.com/VBA-tools/-JSON/b...nConverter.bas

    It works both ways so will parse and convert objects and arrays into JSON. The library you’re using requires you to write JavaScript (note that this isn’t Java - they’re completely different programming languages despite the name), it’s slow and it’s not available on 64-bit excel (it’s also not proper JavaScript, but that’s a bit outside the scope of this!)

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Edit JSON parser code to add function to write JSON file

    Quote Originally Posted by Kyle123 View Post
    Sounds promising. The link gives me a 404 error though?


    Quote Originally Posted by Kyle123 View Post
    The library you’re using requires you to write JavaScript
    I never learnt JavaScript so I didn't realise that this library was using JS! I just tried different VBA code on the web until I found one that returned the contents of JSON files as an array of fields and values.


    Quote Originally Posted by Kyle123 View Post
    (note that this isn’t Java - they’re completely different programming languages despite the name)
    Heh. That was one of the first things I was taught when I tried to learn Java 2 years ago! (Java Good, JavaScript Bad! Tried to ride on the success of Java by using similar name )

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Edit JSON parser code to add function to write JSON file

    Dunno what happened to the link, try this: https://github.com/VBA-tools/VBA-JSON

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Edit JSON parser code to add function to write JSON file

    Quote Originally Posted by Kyle123 View Post
    Dunno what happened to the link, try this: https://github.com/VBA-tools/VBA-JSON
    Thanks Kyle. I will see if I can integrate this with my project near the end of this week and see how it goes.

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Edit JSON parser code to add function to write JSON file

    So I downloaded the module, imported into the project, enabled the reference library & edited the project to use the functions in this module.

    Like the previous code I was using, this new code also runs into problems when trying to read certain JSON values. To be specific - If the datatype of the returned value is a non-scalar value such as an array or a dictionary/collection, the code runs into an error. (The previous code returned a useless string of "[object]". This one triggers a 450 error.)

    On the previous code I added code that managed to work around this read issue. Relevant extract below:
    Please Login or Register  to view this content.
    On the JsonConverter code I am not finding it so easy. I edited the main code to this:
    Please Login or Register  to view this content.
    I can convert these values to a 1d array using the function below:
    Please Login or Register  to view this content.
    But if the value I am trying to read from the JSON is in the format of a 2d array (Well a Dictionary if you like. The value is a 2 column 2d array of (sub-)fields and their associated (sub-)values) then I cant read it.

    I am not an expert on JSONs but I'm puzzled that none of the VBA JSON code I have tried supports reading non-scalar values from JSONs?

    What scares me is that if the code can't handle reading these values then it wont support writing these either.

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Edit JSON parser code to add function to write JSON file

    I think we need to see some sample JSON that doesn’t parse correctly. I have used both the options you are using extensively and have never seen the issue you are describing even with deep hierarchies - it sounds like it’s malformed. But I can’t really comment without seeing it

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Edit JSON parser code to add function to write JSON file

    Quote Originally Posted by Kyle123 View Post
    I think we need to see some sample JSON that doesn’t parse correctly
    Fair enough. I have placed some JSON contents into the code below for testing. (All confidential information removed. Most of the pairs removed to speed up testing.)

    Please Login or Register  to view this content.

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Edit JSON parser code to add function to write JSON file

    I'm afraid I don't see your issue, using your json:
    Please Login or Register  to view this content.

  10. #10
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Edit JSON parser code to add function to write JSON file

    Well it's probably due to my ignorance of Dictionaries in VBA.

    I'm reading multiple JSON files into an array (as description (and code) in the opening post). My objective is to bulk edit data in the array (that will be easy) and then ultimately writing those changes back into the JSON files.

    I changed my project code to use the JSON Converter module. But when I try to read the functions values into my 2d array, it encounters a 450 error on anything that isn't a scalar value.

    Please Login or Register  to view this content.

  11. #11
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Edit JSON parser code to add function to write JSON file

    Well it will do, you need to use Set to assign objects, you’re also trying to assign hierarchical data to a flat array, that’s not going to work unless you walk the tree - you’ve got nested objects in arrays without keys.

    You’re working with dictionaries of dictionaries, on possible multiple levels - you can’t represent that in a flat array, you’d end up with multiple keys per value and in the case of an array in the json a single key with multiple nested objects.

    Why don’t you just work with the data in the dictionary that’s returned, there’s no reason you can’t have a dictionary of dictionaries where the file path is the key and the value is the parsed object. That way you’d know where to write the files back to
    Last edited by Kyle123; 02-12-2020 at 01:42 AM.

  12. #12
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Edit JSON parser code to add function to write JSON file

    Thanks Kyle. I will look into your suggestion. It will be difficult though since I am used to arrays and not Dictionaries. I have tried-and-tested functions for working with (editing, filtering & sorting) arrays which I don't have for working with Dictionaries - I will have to create equivalents for Dictionaries which will mean starting from square one.
    Last edited by mc84excel; 02-17-2020 at 09:12 PM. Reason: removed notes intended for own use

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Store part of JSON file in a variable
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-14-2018, 12:12 AM
  2. Macro JSON file object extract
    By yousufj56 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-16-2017, 07:30 PM
  3. [SOLVED] seperate json file into different columns by key
    By excelmymind in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-27-2017, 02:37 AM
  4. seperate json file into different columns by key
    By excelmymind in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-27-2017, 02:35 AM
  5. Json file from URL to Excel
    By KK33317 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-21-2014, 10:50 AM
  6. Json file from URL to Excel
    By KK33317 in forum Tips and Tutorials
    Replies: 0
    Last Post: 03-13-2014, 07:39 AM
  7. [SOLVED] produce JSON code and POST to website
    By jefjef in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-10-2014, 01:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1