+ Reply to Thread
Results 1 to 4 of 4

Convert jscript JSON flattener to work with VBA ScriptControl

Hybrid View

JasperD Convert jscript JSON... 03-02-2018, 04:13 PM
AB33 Re: Convert jscript JSON... 03-02-2018, 04:49 PM
JasperD Re: Convert jscript JSON... 03-02-2018, 05:42 PM
JasperD Re: Convert jscript JSON... 03-03-2018, 11:49 AM
  1. #1
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Convert jscript JSON flattener to work with VBA ScriptControl

    Hi all,

    I set a scriptcontrol object in VBA to run jscript, and want to use the following javascript to flatten a nested JSON object, but when I run it on my parsed JSON object, it tells me "Run Time Error 5007 - object expected"

    Does anyone know why this happens?
    I can't post the javascript, but basically it's the first reply from SO/questions/19098797; Object.Flatten

    Any help is welcome!

    Thanks,

    Jaspper

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Convert jscript JSON flattener to work with VBA ScriptControl

    Hi Jaspper,
    you know about Parsing JSON more than I do since I did not use the object until I got excel 32 bit. If you are using HTTP request, do you get a JSON response? If not, that is where the problem lies.

    Set RetVal = MyScript.Eval("(" + .responseText + ")")
    flattens the JSON data.
    I know this is not a Python request, but it is a breath to flatten data using Python. A two lines of code will do the magic. You can export the output in to CVS format. Did you also tried the Third party parser?

  3. #3
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Convert jscript JSON flattener to work with VBA ScriptControl

    Hey AB33,

    eval just parses the JSON, it doesn't flatten it.
    You can remove all the pointless nesting with the javascript code on stackoverflow.com/questions/19098797/ (the 'object.flatten' part in the first reply with 150+ upvotes). However, if I just do ScriptC.AddCode and then that code, it will accept it, but then I don't know how to actually use it to flatten my json string.

    Trust me that I have the json string and all pulls correctly ;-)
    Thanks!

    Jaspper

  4. #4
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Convert jscript JSON flattener to work with VBA ScriptControl

    OK, I solved it by going through every related javascript code I could find until I found one that worked

    The below does the trick - after loading it with 'addcode', function 'f' will flatten the json string neatly. Please note it already has the double quotes for you to paste into vba, if you're using it as jscript, replace "" with ".

    "function f(r){var n={};for(var e in r)if(""object""==typeof r[e]&&null!==r[e]){var o=f(r[e]);for(var a in o)n[e+"".""+a]=o[a]}else n[e]=r[e];return n}"
    Resolving thread.
    Thanks,

    Jasper

+ 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] Trouble with VBA-JSON
    By stusic in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-07-2017, 06:48 AM
  2. Current work book .xlsm convert to xlsx work book then mail
    By Vcare in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-26-2016, 09:47 AM
  3. Convert Object to JSON String
    By amartinez988 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 12-01-2016, 03:41 PM
  4. How to automate Excel from an HTML Web page by using JScript
    By davesexcel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-18-2014, 06:22 PM
  5. Convert Work Hours, Days or Work Week to Months
    By mycon73 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-25-2014, 09:04 AM
  6. A good VB Script and JScript Newsgroup needed
    By Karen27 in forum Excel General
    Replies: 0
    Last Post: 04-14-2005, 04:06 AM
  7. [SOLVED] Excel - Rendering ASCII via ASP/JScript
    By DMoe in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 03-10-2005, 03:06 AM

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