Parse Json values in Excel cells

manurockz007

New Member
Joined
Dec 12, 2016
Messages
30
How do i separate all the values from the following
{
"anonymousId": "213f9272-ebb4-4c07-8fb0-85f4ff0b3ad6",
"context": {
"library": {
"name": "analytics.js",
"version": "3.0.0"
},
"page": {
"path": "/prem/",
"referrer": "http://m.facebook.com/",
"search": "",
"title": "प्रेम",
"url": "http://hamroasa.com/prem/"
},
"userAgent": "Mozilla/5.0 (Linux; Android 5.0; Lenovo A1000 Build/S100; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/34.0.0.43.267;]",
"ip": "103.214.79.6"
},
"event": "Gospel View Depth",
"integrations": {},
"messageId": "ajs-bd3e7cbad4f5d0efe006c419b5f1e878",
"properties": {
"gospel_play_percentage": "0.926",
"gospel_video_duration": 148.95600907029478,
"gospel_video_position": 138,
"gospel_video_url": "https://www.youtube.com/watch?v=HrsImLMpP0c",
"site_type_id": 6
},
"receivedAt": "2016-12-09T09:25:22.484Z",
"sentAt": "2016-12-09T09:26:38.120Z",
"timestamp": "2016-12-09T09:25:22.475Z",
"type": "track",
"userId": null,
"originalTimestamp": "2016-12-09T09:26:38.111Z"
}
To get an output where all the values should be separate.

<tbody>
</tbody>
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
welcome to the board

What do you mean by "values"?

Looks like you want Data > "Text to columns" in order to split the values out in a worksheet. You could use " or : or { etc as a delimiter for example

But your requirement is unclear, if you can be more specific in terms of what results you want and how you want them laid out then I can give more helpful advice
 
Upvote 0
I want to get the values after double quotes this is an json format i am not able to get exact text from the below format. like 0.926 or 138 in a cell specific.
If i put this in a one cell it should separate automatically in all different cells.

"gospel_play_percentage": "0.926",
"gospel_video_duration": 148.95600907029478,
"gospel_video_position": 138,
"gospel_video_url": "https://www.youtube.com/watch?v=HrsImLMpP0c",
"site_type_id": 6
 
Upvote 0
There's many different ways we can extract the data you want. But I need you to be very clear on exactly what you want extracted and how

For example, if I paste the original data (post 1) into Excel, and then do Data > Text to columns, using ":" as a delimiter, then I can effectively split the data as highlighted in post 3. But I've already suggested this so I'm not convinced this is exactly what you want

We can further break it down - or indeed, the whole original text - by using formulas such as:
=LEN(string) gives the length of that string
=LEFT(string, x) or MID(string(x,y) or RIGHT(string,x) returns pieces of text from within a longer text string
=FIND(term, string) or SEARCH(term, string) give the locations within a text string of a specific search term

Or we can extract specific items in any way we want using VBA to go through the text and pull out whatever we want it to. We can use any combination of these approaches together if we want to. So, how many such text strings do you have, exactly which items do you want pulled out, and how do you want them presented?
 
Upvote 0
I want to get the values after double quotes this is an json format i am not able to get exact text from the below format. like 0.926 or 138 in a cell specific.
If i put this in a one cell it should separate automatically in all different cells.

"gospel_play_percentage": "0.926",
"gospel_video_duration": 148.95600907029478,
"gospel_video_position": 138,
"gospel_video_url": "https://www.youtube.com/watch?v=HrsImLMpP0c",
"site_type_id": 6
You need a JSON parser, for example https://www.codeproject.com/articles/720368/vb-json-parser-improved-performance.

Put the JSON parser code in one module:
Code:
'VBJSONDeserializer.bas downloaded from https://github.com/samgerene/VBJSONDeserializer/tree/master/VB6%20Source

'Changes
'1. Add 64-bit declaration for CopyMemory.
'2. Don't use ModLocale for regional settings.

'-----------------------------

' VBJSONDeserializer is a VB6 adaptation of the VB-JSON project @
' http://www.ediy.co.nz/vbjson-json-parser-library-in-vb6-xidc55680.html
' BSD Licensed

Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Integer, ByVal Source As Long, ByVal Length As Long)
#Else
    Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Integer, ByVal Source As Long, ByVal Length As Long)
#End If

Private Const A_CURLY_BRACKET_OPEN As Integer = 123     ' AscW("{")
Private Const A_CURLY_BRACKET_CLOSE As Integer = 125    ' AscW("}")
Private Const A_SQUARE_BRACKET_OPEN As Integer = 91     ' AscW("[")
Private Const A_SQUARE_BRACKET_CLOSE As Integer = 93    ' AscW("]")
Private Const A_BRACKET_OPEN As Integer = 40            ' AscW("(")
Private Const A_BRACKET_CLOSE As Integer = 41           ' AscW(")")
Private Const A_COMMA As Integer = 44                   ' AscW(",")
Private Const A_DOUBLE_QUOTE As Integer = 34            ' AscW("""")
Private Const A_SINGLE_QUOTE As Integer = 39            ' AscW("'")
Private Const A_BACKSLASH As Integer = 92               ' AscW("\")
Private Const A_FORWARDSLASH As Integer = 47            ' AscW("/")
Private Const A_COLON As Integer = 58                   ' AscW(":")
Private Const A_SPACE As Integer = 32                   ' AscW(" ")
Private Const A_ASTERIX As Integer = 42                 ' AscW("*")
Private Const A_VBCR As Integer = 13                    ' AscW("vbcr")
Private Const A_VBLF As Integer = 10                    ' AscW("vblf")
Private Const A_VBTAB As Integer = 9                    ' AscW("vbTab")
Private Const A_VBCRLF As Integer = 13                  ' AscW("vbcrlf")

Private Const A_b As Integer = 98                       ' AscW("b")
Private Const A_f As Integer = 102                      ' AscW("f")
Private Const A_n As Integer = 110                      ' AscW("n")
Private Const A_r As Integer = 114                      ' AscW("r"
Private Const A_t As Integer = 116                      ' AscW("t"))
Private Const A_u As Integer = 117                      ' AscW("u")

Private m_decSep As String
Private m_groupSep As String

Private m_parserrors As String
Private m_str() As Integer
Private m_length As Long

Public Function GetParserErrors() As String
   GetParserErrors = m_parserrors
End Function

Public Function parse(ByRef str As String) As Object

    m_decSep = "." 'ModLocale.GetRegionalSettings(LOCALE_SDECIMAL)
    m_groupSep = "," 'ModLocale.GetRegionalSettings(LOCALE_SGROUPING)
    
    Dim index As Long
    index = 1
    
    GenerateStringArray str
    
    m_parserrors = vbNullString
    On Error Resume Next
    
    Call skipChar(index)
    
    Select Case m_str(index)
    Case A_SQUARE_BRACKET_OPEN
        Set parse = parseArray(str, index)
    Case A_CURLY_BRACKET_OPEN
        Set parse = parseObject(str, index)
    Case Else
        m_parserrors = "Invalid JSON"
    End Select
    
    'clean array
    ReDim m_str(1)

End Function

'Private Sub GenerateStringArray(ByRef str As String)
'
'Dim i As Long
'Dim Length As Long
'Dim s As String
'
'm_length = Len(str)
'ReDim m_str(1 To m_length)
'
'For i = 1 To m_length
'    m_str(i) = AscW(Mid$(str, i, 1))
'Next i
'
'End Sub

Private Sub GenerateStringArray(ByRef str As String)

    m_length = Len(str)
    ReDim m_str(1 To m_length)
    CopyMemory m_str(1), StrPtr(str), m_length * 2
    
End Sub

Private Function parseObject(ByRef str As String, ByRef index As Long) As Dictionary

    Set parseObject = New Dictionary
    Dim sKey As String
    Dim charint As Integer
    
    Call skipChar(index)
    
    If m_str(index) <> A_CURLY_BRACKET_OPEN Then
       m_parserrors = m_parserrors & "Invalid Object at position " & index & " : " & Mid$(str, index) & vbCrLf
       Exit Function
    End If
    
    index = index + 1
    
    Do
        Call skipChar(index)
        
        charint = m_str(index)
        
        If charint = A_COMMA Then
            index = index + 1
            Call skipChar(index)
        ElseIf charint = A_CURLY_BRACKET_CLOSE Then
            index = index + 1
            Exit Do
        ElseIf index > m_length Then
             m_parserrors = m_parserrors & "Missing '}': " & Right(str, 20) & vbCrLf
             Exit Do
        End If
    
        ' add key/value pair
        sKey = parseKey(index)
        On Error Resume Next
    
        parseObject.Add sKey, parseValue(str, index)
        If Err.Number <> 0 Then
            m_parserrors = m_parserrors & Err.Description & ": " & sKey & vbCrLf
            Exit Do
        End If
    Loop

End Function

Private Function parseArray(ByRef str As String, ByRef index As Long) As Collection

    Dim charint As Integer
    
    Set parseArray = New Collection
    
    Call skipChar(index)
    
    If Mid$(str, index, 1) <> "[" Then
        m_parserrors = m_parserrors & "Invalid Array at position " & index & " : " + Mid$(str, index, 20) & vbCrLf
        Exit Function
    End If
       
    index = index + 1
    
    Do
        Call skipChar(index)
        
        charint = m_str(index)
        
        If charint = A_SQUARE_BRACKET_CLOSE Then
            index = index + 1
            Exit Do
        ElseIf charint = A_COMMA Then
            index = index + 1
            Call skipChar(index)
        ElseIf index > m_length Then
             m_parserrors = m_parserrors & "Missing ']': " & Right(str, 20) & vbCrLf
             Exit Do
        End If
        
        'add value
        On Error Resume Next
        parseArray.Add parseValue(str, index)
        If Err.Number <> 0 Then
            m_parserrors = m_parserrors & Err.Description & ": " & Mid$(str, index, 20) & vbCrLf
            Exit Do
        End If
    Loop
    
End Function

Private Function parseValue(ByRef str As String, ByRef index As Long)

   Call skipChar(index)

    Select Case m_str(index)
    Case A_DOUBLE_QUOTE, A_SINGLE_QUOTE
        parseValue = parseString(str, index)
        Exit Function
    Case A_SQUARE_BRACKET_OPEN
        Set parseValue = parseArray(str, index)
        Exit Function
    Case A_t, A_f
        parseValue = parseBoolean(str, index)
        Exit Function
    Case A_n
        parseValue = parseNull(str, index)
        Exit Function
    Case A_CURLY_BRACKET_OPEN
        Set parseValue = parseObject(str, index)
        Exit Function
    Case Else
        parseValue = parseNumber(str, index)
        Exit Function
    End Select

End Function

Private Function parseString(ByRef str As String, ByRef index As Long) As String

   Dim quoteint As Integer
   Dim charint As Integer
   Dim Code    As String
   
   Call skipChar(index)
   
   quoteint = m_str(index)
   
   index = index + 1
   
   Do While index > 0 And index <= m_length
   
      charint = m_str(index)
      
      Select Case charint
        Case A_BACKSLASH

            index = index + 1
            charint = m_str(index)

            Select Case charint
            Case A_DOUBLE_QUOTE, A_BACKSLASH, A_FORWARDSLASH, A_SINGLE_QUOTE
                parseString = parseString & ChrW$(charint)
                index = index + 1
            Case A_b
                parseString = parseString & vbBack
                index = index + 1
            Case A_f
                parseString = parseString & vbFormFeed
                index = index + 1
            Case A_n
                    parseString = parseString & vbLf
                  index = index + 1
            Case A_r
                parseString = parseString & vbCr
                index = index + 1
            Case A_t
                parseString = parseString & vbTab
                  index = index + 1
            Case A_u
                index = index + 1
                Code = Mid$(str, index, 4)

                parseString = parseString & ChrW$(Val("&h" + Code))
                index = index + 4
            End Select

        Case quoteint
        
            index = index + 1
            Exit Function

         Case Else
            parseString = parseString & ChrW$(charint)
            index = index + 1
      End Select
   Loop
   
End Function

Private Function parseNumber(ByRef str As String, ByRef index As Long)

Dim Value   As String
Dim Char    As String

Call skipChar(index)

Do While index > 0 And index <= m_length
    Char = Mid$(str, index, 1)
    If InStr("+-0123456789.eE", Char) Then
        Value = Value & Char
        index = index + 1
    Else
        'check what is the grouping seperator
        If Not m_decSep = "." Then
            Value = Replace(Value, ".", m_decSep)
        End If
     
        If m_groupSep = "." Then
            Value = Replace(Value, ".", m_decSep)
        End If
     
        parseNumber = CDec(Value)
        Exit Function
    End If
Loop
   
End Function

Private Function parseBoolean(ByRef str As String, ByRef index As Long) As Boolean

   Call skipChar(index)
   
   If Mid$(str, index, 4) = "true" Then
      parseBoolean = True
      index = index + 4
   ElseIf Mid$(str, index, 5) = "false" Then
      parseBoolean = False
      index = index + 5
   Else
      m_parserrors = m_parserrors & "Invalid Boolean at position " & index & " : " & Mid$(str, index) & vbCrLf
   End If

End Function

Private Function parseNull(ByRef str As String, ByRef index As Long)

   Call skipChar(index)
   
   If Mid$(str, index, 4) = "null" Then
      parseNull = Null
      index = index + 4
   Else
      m_parserrors = m_parserrors & "Invalid null value at position " & index & " : " & Mid$(str, index) & vbCrLf
   End If

End Function

Private Function parseKey(ByRef index As Long) As String

   Dim dquote  As Boolean
   Dim squote  As Boolean
   Dim charint As Integer
   
   Call skipChar(index)
   
    Do While index > 0 And index <= m_length
    
        charint = m_str(index)
        
        Select Case charint
        Case A_DOUBLE_QUOTE
            dquote = Not dquote
            index = index + 1
            If Not dquote Then
            
                Call skipChar(index)
                
                If m_str(index) <> A_COLON Then
                    m_parserrors = m_parserrors & "Invalid Key at position " & index & " : " & parseKey & vbCrLf
                    Exit Do
                End If
            End If
                

        Case A_SINGLE_QUOTE
            squote = Not squote
            index = index + 1
            If Not squote Then
                Call skipChar(index)
                
                If m_str(index) <> A_COLON Then
                    m_parserrors = m_parserrors & "Invalid Key at position " & index & " : " & parseKey & vbCrLf
                    Exit Do
                End If
                
            End If
        
        Case A_COLON
            index = index + 1
            If Not dquote And Not squote Then
                Exit Do
            Else
                parseKey = parseKey & ChrW$(charint)
            End If
        Case Else
            
            If A_VBCRLF = charint Then
            ElseIf A_VBCR = charint Then
            ElseIf A_VBLF = charint Then
            ElseIf A_VBTAB = charint Then
            ElseIf A_SPACE = charint Then
            Else
                parseKey = parseKey & ChrW$(charint)
            End If

            index = index + 1
        End Select
    Loop

End Function

Private Sub skipChar(ByRef index As Long)

    Dim bComment As Boolean
    Dim bStartComment As Boolean
    Dim bLongComment As Boolean
    
    Do While index > 0 And index <= m_length
        
        Select Case m_str(index)
        Case A_VBCR, A_VBLF
            If Not bLongComment Then
                bStartComment = False
                bComment = False
            End If
        
        Case A_VBTAB, A_SPACE, A_BRACKET_OPEN, A_BRACKET_CLOSE
            'do nothing
            
        Case A_FORWARDSLASH
            If Not bLongComment Then
                If bStartComment Then
                    bStartComment = False
                    bComment = True
                Else
                    bStartComment = True
                    bComment = False
                    bLongComment = False
                End If
            Else
                If bStartComment Then
                    bLongComment = False
                    bStartComment = False
                    bComment = False
                End If
            End If
        Case A_ASTERIX
            If bStartComment Then
                bStartComment = False
                bComment = True
                bLongComment = True
            Else
                bStartComment = True
            End If
        Case Else
            
            If Not bComment Then
                Exit Do
            End If
        End Select
    
        index = index + 1
    Loop

End Sub
And this main code in another module:
Code:
Public Sub Main_Test()

    Dim JsonString As String
    Dim JSONdict As Scripting.Dictionary
        
    JsonString = Worksheets("Sheet1").Range("A1").Value
    
    Set JSONdict = parse(JsonString)
    
    With JSONdict.item("properties")
        Worksheets("Sheet1").Range("A3").Value = .item("gospel_play_percentage")
        Worksheets("Sheet1").Range("A4").Value = .item("gospel_video_duration")
        Worksheets("Sheet1").Range("A5").Value = .item("gospel_video_position")
        Worksheets("Sheet1").Range("A6").Value = .item("gospel_video_url")
        Worksheets("Sheet1").Range("A7").Value = .item("site_type_id")
    End With
    
End Sub
You must set a reference to Microsoft Scripting Runtime (in Tools->References in VBA editor). The main code loads your JSON string from cell A1 on Sheet1 and outputs the property values to the cells below.
 
Upvote 0
How do i separate all the values from the following
{
"anonymousId": "12dhdhdhfds548521555",

"event": "sample",
"integrations": {},
"messageId": "1455gdhggdddddggg154",
"properties": {
"test_play_percentage": "0.926",
"test_video_duration": 148.95600907029478,
"test_video_position": 138,
"test_video_url": "https://www.youtube.com/watch?v=dghssdghfddgs",
"site_type_id": 10
},

To get an output where all the values should be separate.

<tbody>
</tbody>
 
Upvote 0
How do i get "event" and "anonymousId" and "time stamp" values and the put it in loop so that i can assign all data in one cell and the can separate it into other cell.
this case i can complete number data separation in one time
 
Upvote 0
How do i get "event" and "anonymousId" and "time stamp" values
Code:
    With JSONdict
        Worksheets("Sheet1").Range("A8").Value = .item("event")
        Worksheets("Sheet1").Range("A9").Value = .item("anonymousId")
        Worksheets("Sheet1").Range("A10").Value = .item("timestamp")
    End With
I don't understand the rest of your post.
 
Upvote 0

Forum statistics

Threads
1,214,818
Messages
6,121,725
Members
449,049
Latest member
MiguekHeka

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top