+ Reply to Thread
Results 1 to 13 of 13

Add key <> value pair to JSON object through scriptcontrol

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

    Add key <> value pair to JSON object through scriptcontrol

    Hi all,

    Take the following code example:

    Please Login or Register  to view this content.
    This will set object J as a JSON object, parsing the json string found on https://jsonplaceholder.typicode.com/users
    For field '0', the name is "Leanne Graham" - I can get this by saying:

    Please Login or Register  to view this content.
    I can change this using the VbSet argument of CallByName, for example like this:
    Please Login or Register  to view this content.
    Which changes the name from "Leanne Graham" to "Excel Forum".

    This is all great, but what if I want to *add* a field? How could I add a new key/value pair?
    For example - adding in "gender" as key, and "female" as a value - is that possible using CallByName?
    If not, is it possible through JScript code added by "addcode"?

    Essentially, this example code gives 10 names, but I'd want to be flexible to add a) key/values to each of the 10, and b) add additional name fields, with all subfields to the 10, increasing the total.

    Please let me know if you have any bright ideas.
    Thanks!

    Jasper

    ps: I know 'eval' isn't safe, I use a json parse emca 3 jscript function instead, but for ease of this example, I used 'eval'
    ps ps: I know scriptcontrol isn't available in 64 bit - In reality I use a class module that sets it up either through MSHTA or straight to scriptcontrol, depending on the environment, but again, for ease of this example I just set the object straight to 'scriptcontrol'

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Add key <> value pair to JSON object through scriptcontrol

    I believe you'd need to .AddCode to your ScriptControl object and do it using java script.

    I can't test it since I have Excel installed as x64 and can't easily access ScriptControl. But read below links.
    http://ramblings.mcpher.com/Home/exc.../scriptcontrol
    https://www.sitepoint.com/community/...y-in-json/8361

    You should be able to use JsArray.Push("item") or JsArray[JsArray.Length] = "item" to add item end of array in Javascript.

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

    Re: Add key <> value pair to JSON object through scriptcontrol

    Actually, this works:

    Please Login or Register  to view this content.
    But it only works to add a straight key/value pair - not if it's a new nested object.
    For that I tried this (where the 10 is arbitrary to this sample)
    Please Login or Register  to view this content.
    but for some weird reason, the y is set correctly as value, but the keyname comes up as "x", instead of the variable I put as x.
    What's up with that...?
    Please click the * below if this helps

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Add key <> value pair to JSON object through scriptcontrol

    Your example Json is nested array (i.e. values in an array is array).

    Have a read of...
    https://www.w3schools.com/js/js_json_arrays.asp
    https://stackoverflow.com/questions/...for-everything

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

    Re: Add key <> value pair to JSON object through scriptcontrol

    Hey CK76,

    thanks for the reply - I know it's nested, and it should be So adding through the tuples code (S.AddCode "function a(r, x, y){r[10] = {x:y} ;return r;}), it adds another array (10) and adds key "x", with value "y". The problem is that the value is taken correctly from the variable "y" I feed it, but the key name is put as "x" rather than the contents of the variable "x".
    For instance:

    Please Login or Register  to view this content.
    This adds a new array (nest) as object '10', with one key/value pair. This should be J.10.thisisa = 'test'; instead it is J.10.x = 'test'.
    I don't understand why it puts x as "x", but y as "test" (the value assigned to variable "y").
    Do you have any idea?

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

    Re: Add key <> value pair to JSON object through scriptcontrol

    It should be:
    Please Login or Register  to view this content.
    If you want to use a variable string as a key, it needs to be in brackets.

    Honestly though, why not just use VBA-Web JsonConverter? That just uses dictionaries/collections (I don't recall which) so adding to them is very easy
    Last edited by Kyle123; 05-25-2018 at 12:20 PM.

  7. #7
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Add key <> value pair to JSON object through scriptcontrol

    Hmm, let me think on it.

    Normally I just load JSON to dictionary object, do string manipulation, or interact with it using Python.
    Have not used ScriptControl before and am weak in Javascript array manipulation. But I suspect there's something wrong with syntax.

    Edit: Ah, thanks Kyle!

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

    Re: Add key <> value pair to JSON object through scriptcontrol

    Sorry Kyle, but that doesn't work - gives some error, I tried it too... (it's ES6, while jscript is ES3)

    And this would be one line of code vs dozens, to get something easy done. I much prefer that - and after all, I like a puzzle
    Last edited by JasperD; 05-25-2018 at 05:56 PM.

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

    Re: Add key <> value pair to JSON object through scriptcontrol

    What's the error?

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

    Re: Add key <> value pair to JSON object through scriptcontrol

    Run-Time error '1028': Application-defined or object-defined error.

  11. #11
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Add key <> value pair to JSON object through scriptcontrol

    @CK76 :
    There is a custom made 64 bit Script Control here : https://tablacus.github.io/scriptcontrol_en.html

    @Jasper :
    You need to download my attachment
    Please Login or Register  to view this content.
    Attached Files Attached Files
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

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

    Re: Add key <> value pair to JSON object through scriptcontrol

    Thank you Karedog - I wasn't aware that I can just leave it in the scriptcontrol object and set it via var, instead of having to put it in an object in VBA.
    I will still have to put it into an object if I want to see the treeview (or can that be done simpler), but setting new key/value pairs and new nested objects is much easier now. Many thanks!

  13. #13
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Add key <> value pair to JSON object through scriptcontrol

    You are welcome Jasper, thanks for reps.

    I don't quite catch what 'treeview' is, if you examine the result on column C, you will see all the data in indented style (sound like a treeview to me).

    But if what you want is the 'key' only of the JSON, then I make JSON_GetStruct() function to convert the JSON string to an array, check it out :
    Please Login or Register  to view this content.

+ 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] Convert jscript JSON flattener to work with VBA ScriptControl
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-03-2018, 11:49 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] Convert Single Pair of Columns to 3 Column (Pair) List
    By kencoburn in forum Excel General
    Replies: 7
    Last Post: 03-04-2017, 01:43 PM
  4. Convert Object to JSON String
    By amartinez988 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 12-01-2016, 03:41 PM
  5. Get all elements from a json object at once
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-10-2015, 02:34 AM
  6. Is there a native way to encode an object as a JSON string?
    By XmisterIS in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-01-2014, 05:32 AM
  7. Replies: 3
    Last Post: 07-20-2012, 07:29 PM

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