+ Reply to Thread
Results 1 to 15 of 15

Split name value pairs of JSON statements

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Split name value pairs of JSON statements

    Hello everyone

    In JSON the data is stored in name value paired separated by comma .. and I would like to split those data into adjacent columns
    The problem is that there are values containing comma and this is confusing for the split process
    How can I split the values to adjacent columns?

    I have attached sample workbook ..
    I prefer if possible dealing with arrays. That's to store the range("a1").currentregion in an array then to manipulate the process

    *another point: can I specify the desired keys in array. I mean something like that
    sKeys=Array("Title","Awards","Ratings" and so on)

    Thanks advanced for help
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Split name value pairs of JSON statements

    The problem is that there are values containing comma and this is confusing for the split process
    Could we say that the border is
    ","
    - Battle without fear gives no glory - Just try

  3. #3
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Split name value pairs of JSON statements

    If answer to previous question is YES then try next code
    Note: There is an empty row at the end ... delete it


    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Split name value pairs of JSON statements

    Thanks a lot for this great solution.
    The problem that the results are incorrect for two keys
    After testing your code I copied the range("B2:Z2") and pasted special as values in range("A6") and checked Transpose so as to see the results well
    and I got this result for that range
    Untitled.png

    Also to make sure of the keys ..Copy the value if A1 and paste it in the following link
    https://jsonlint.com/
    And click 'Validate JSON' to see the results

  5. #5
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Split name value pairs of JSON statements

    What is or are the keys to split the data, is it "," as used by last macro ???

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

    Re: Split name value pairs of JSON statements

    Why don’t you use the same JSON parser you’ve used in your other threads?

    @pc1 this is nested JSON which contains arrays, some data will be “key”:”value” there will be arrays of [“value”,”value”,”value”] or arrays of objects such as [{“key”:”value”,”key”:”value”},{“key”:”value”}] as its both nested and hierarchical it won’t fit into a grid pattern. The awkwardness of parsing it means it’s much easier to just use a JSON parser in VBA

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Split name value pairs of JSON statements

    Thanks a lot for reply Mr. Kyle
    I have no experience at dealing with JSON parser so I posted the thread so as to find a solution and to learn something new ..
    I would appreciate a lot if you help me
    Thanks a lot for replies excel masters

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

    Re: Split name value pairs of JSON statements

    What do you mean that you have no experience? You’ve been using it in our other threads and asking for help with it on Eileen’s Lounge where Hans has been helping you.

    That is is the best way of dealing with this kind of data, putting it onto a sheet just makes it harder as it’s not in a format that a table can represent. As I put in the post above, the data is not all in the format key:value

    Use the parser, stick at it, you’ll get there!
    Last edited by Kyle123; 10-14-2018 at 06:00 AM.

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Split name value pairs of JSON statements

    Thanks a lot
    I am new to those stuff and I am learning from posting such problems ..
    As a desired output I need to put each value in a column or field even if it was an object so as for 'Ratings' field as an example
    the desired output would be string
    Please Login or Register  to view this content.
    Also I need to get the value without the keys as the keys supposed to be headers .. and the order of the values preferred to be in an array order
    sHeader=Array("Title","Awards","DVD","Ratings") ..
    Hope it is clear now
    Thanks a lot for sharing

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

    Re: Split name value pairs of JSON statements

    It wasn’t unclear in the first instance, but you’re still going about this in the wrong way, use the parser to build the data you want into an array in vba and write it to the sheet.

    Dumping it on the sheet then trying to clean it up is 10x harder

    If you just want all the values, loop through all the dictionaries and collections in the parser JSON and write them directly to the sheet. This will remove all the keys for you

    What are you trying to do with this data once you have it on a sheet in the format you want?
    Last edited by Kyle123; 10-14-2018 at 06:13 AM.

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Split name value pairs of JSON statements

    In the main code I have already the array before dumping it in the sheet and it is dumped to see and check the results
    Please Login or Register  to view this content.
    What I am seeking for is to get the keys as headers and their related values in columns .. so in row 1 something like that
    Title - Awards - DVD - Ratings - and so on

    and in the rows below something like that
    Close-Up - 2 wins. - N/A - [{"Source":"Internet Movie Database","Value":"6.8/10"}] and so on
    So the final result would be in flat database...

  12. #12
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Split name value pairs of JSON statements

    I have solved it partially .. Put a JSON string in A1 and try this code
    Please Login or Register  to view this content.
    Now how can I arrange the data according to sHeader=Array("Title","Awards","DVD".... and so on)

  13. #13
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,004

    Re: Split name value pairs of JSON statements

    Hi, YasserKhalil
    Try this:

    Please Login or Register  to view this content.
    Attached Files Attached Files

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Split name value pairs of JSON statements

    For the data provided
    Please Login or Register  to view this content.
    Last edited by jindon; 10-14-2018 at 09:24 PM.

  15. #15
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Split name value pairs of JSON statements

    Thank you very much Jindon and Akuini
    Both solutions are perfect and working like charm
    Best and Kind Regards

+ 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] locate in their positions pairs and odd pairs
    By jorel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-21-2018, 06:35 AM
  2. [SOLVED] Trouble with VBA-JSON
    By stusic in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-07-2017, 06:48 AM
  3. [SOLVED] Split pairs of worksheets to separate workbooks
    By vhache in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-17-2016, 05:03 PM
  4. [SOLVED] Split SQL Statements on Keywords
    By xlBunny in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-27-2016, 01:57 PM
  5. Excel VBA-JSON Example
    By manofcheese in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-28-2016, 11:52 AM
  6. Selecting pairs of records from database with SQL statements
    By anrichards22 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-14-2015, 11:10 AM
  7. Replies: 10
    Last Post: 01-05-2011, 03:26 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