Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • JSONIO Updated on SSC

    For anyone needing to work with JSON data in either direction (e.g., reading/writing), the most current version of jsonio is now available. You can see examples of how to use jsonio on the project website : https://wbuchanan.github.io/StataJSON. The syntax has changed slightly since the first version released on SSC.

  • #2
    I used the following 'jsonio' command to import JSON file into Stata:

    jsonio kv, filenm("SAMPLE_FILE_NAME.json") nourl

    I got the correct list of variables, however, for an unknown reason, only the first JSON observation actually get imported.

    I would appreciate your help here.

    Thank you.

    Best,

    Husain

    Comment


    • #3
      Husain Alshaikh
      Can you provide an example of the JSON file and/or potentially add it as an issue to the project's GitHub repository: https://github.com/wbuchanan/StataJSON?

      Comment


      • #4
        wbuchanan

        I have the following data stored in a json file named EX3.json, which I am trying to read using jsonio, yet, I keep getting the error below, appreciate your kind advice.

        Thank you,

        Code:
        {
          "Root" : [ {
         "date" : "January-2022",
            "value" : 44
          }, { 
            "date" : "April-2022",
            "value" : 43 
           }, { 
            "date" : "July-2022",
            "value" : 44 
         }]
        }
        I am using the following code:

        Code:
         jsonio kv, file("~/Desktop/ex3.json") nourl w("data")

        Error received

        java.lang.IndexOutOfBoundsException: Index 2 out of bounds for length 2
        at java.base/jdk.internal.util.Preconditions.outOfBounds(Precon ditions.java:64)
        at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIn dex(Preconditions.java:70)
        at java.base/jdk.internal.util.Preconditions.checkIndex(Precond itions.java:248)
        at java.base/java.util.Objects.checkIndex(Objects.java:372)
        at java.base/java.util.ArrayList.get(ArrayList.java:459)
        at org.paces.Stata.Input.FlatJSON.flatten(FlatJSON.ja va:205)
        at org.paces.Stata.Input.FlatJSON.flatten(FlatJSON.ja va:194)
        at org.paces.Stata.Input.FlatJSON.flatten(FlatJSON.ja va:116)
        at org.paces.Stata.Input.InJSON.insheetFileToVars(InJ SON.java:180)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invo ke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invo ke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl. invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at com.stata.Javacall.load(Javacall.java:273)
        at com.stata.Javacall.load(Javacall.java:92)

        Comment


        • #5
          In the meanwhile, you may give insheetjson (form ssc) a try.

          Code:
          . clear all
          
          . local columnSelectors "date value"
          
          . tokenize `columnSelectors'
          
          . foreach v in `columnSelectors'{
            2. gen str240 `v'=""
            3. }
          
          
          . insheetjson using file.json, table(Root) col(`columnSelectors')
           3 observations updated/written.
          
          . list
          
               +----------------------+
               |         date   value |
               |----------------------|
            1. | January-2022      44 |
            2. |   April-2022      43 |
            3. |    July-2022      44 |
               +----------------------+

          Comment


          • #6
            Rabih El Habta
            Try:

            Code:
            jsonio kv, file("~/Desktop/ex3.json") nourl
            The what option is used when you are serializing data in Stata to create a JSON file.

            Comment


            • #7
              wbuchanan

              I also tried

              Code:
               
               jsonio kv, file("~/Desktop/ex3.json") nourl
              Same error, although the JSON file is valid, I m not sure what is the problem.

              Thank you,

              Comment


              • #8
                same error here.

                actually I was never able to use jsonio before, due to this presistent error. Then I moved to insheetjson , which is pretty slow on large json files.

                Code:
                . jsonio kv, file("file.json") nourl
                java.lang.IndexOutOfBoundsException: Index 2 out of bounds for length 2
                        at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
                        at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
                        at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
                        at java.base/java.util.Objects.checkIndex(Objects.java:359)
                        at java.base/java.util.ArrayList.get(ArrayList.java:427)
                        at org.paces.Stata.Input.FlatJSON.flatten(FlatJSON.java:205)
                        at org.paces.Stata.Input.FlatJSON.flatten(FlatJSON.java:194)
                        at org.paces.Stata.Input.FlatJSON.flatten(FlatJSON.java:116)
                        at org.paces.Stata.Input.InJSON.insheetFile(InJSON.java:144)
                        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
                        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
                        at com.stata.Javacall.load(Javacall.java:273)
                        at com.stata.Javacall.load(Javacall.java:92)

                Comment


                • #9
                  Which version of the program are you using?

                  Comment


                  • #10
                    Code:
                    which jsonio
                    C:\ado\plus\j\jsonio.ado
                    *! jsonio
                    *! 27APR2017
                    *! v 0.0.7
                    .

                    Comment


                    • #11
                      Luis Pecht
                      Your issue could be using an older version of the program:

                      Code:
                      . which jsonio
                      ./jsonio.ado
                      *! jsonio
                      *! 22FEB2018
                      *! v 0.0.8
                      Do this:

                      Code:
                      ado, f("jsonio")
                      ado uninstall [# returned from command above]
                      net inst jsonio, from("https://wbuchanan.github.io/StataJSON")
                      And you should have the current version of jsonio.

                      Comment


                      • #12
                        thank you, now it is working.

                        Code:
                        . ado, f("jsonio")
                        
                        [92] package jsonio from https://wbuchanan.github.io/StataJSON
                              'JSONIO': A Stata plugin for I/O operations on JSON data
                        
                        [833] package jsonio from http://fmwww.bc.edu/repec/bocode/j
                              'JSONIO': module for I/O operations on JSON data
                        
                        . ado uninstall [92]
                        
                        package jsonio from https://wbuchanan.github.io/StataJSON
                              'JSONIO': A Stata plugin for I/O operations on JSON data
                        
                        (package uninstalled)
                        
                        . ado uninstall [833]
                        
                        package matrixtools from http://fmwww.bc.edu/repec/bocode/m
                              'MATRIXTOOLS': module to build, present and style Stata matrices
                        
                        (package uninstalled)
                        
                        . net inst jsonio, from("https://wbuchanan.github.io/StataJSON") replace
                        checking jsonio consistency and verifying not already installed...
                        
                        installing into C:\ado\plus\...
                        installation complete.
                        
                        . which jsonio
                        C:\ado\plus\j\jsonio.ado
                        *! jsonio
                        *! 22FEB2018
                        *! v 0.0.8
                        
                        . jsonio kv, file("file.json") nourl
                        
                        . list
                        
                             +---------------------------------+
                             |              key          value |
                             |---------------------------------|
                          1. |  /Root/id_1/date   January-2022 |
                          2. | /Root/id_1/value             44 |
                          3. |  /Root/id_2/date     April-2022 |
                          4. | /Root/id_2/value             43 |
                          5. |  /Root/id_3/date      July-2022 |
                             |---------------------------------|
                          6. | /Root/id_3/value             44 |
                             +---------------------------------+

                        Comment


                        • #13
                          wbuchanan

                          I wonder if it is possible to serialize the data below with JSONIO where the object name is "Root" and the variables name is its labels -example below of the desired output.

                          Code:
                          input str20 date no_data not_sufficient sufficient
                          "December 2022" 25 35 40
                          end
                          la var no_data "no data"
                          la var not_sufficient" "not sufficient"

                          Desired JSON output

                          Code:
                          {
                            "Root" : [ {
                              "date" : "December 2022",
                              "sufficient" : "40",
                              "not sufficient" : "35",
                              "no data" : "25"
                            } ],
                          }
                          Any help or suggestion is highly appreciated.

                          Thank you,

                          Comment


                          • #14
                            Rabih El Habta
                            If you wanted to submit a change to https://github.com/wbuchanan/StataJS...AllToJSON.java and the other relevant class definitions that would allow users to supply their own names for the objects I would have no objections to accepting/collaborating with others to make those changes. That said, you can see at the URL referenced that the object names are hardcoded currently. At the moment trying to make those changes to the underlying source code would take more time than I could invest for a change requested by one user. That said, you could always use -filefilter- or command line tools like sed/awk to change the object names from the generated JSON. And if that single node is the only node you'd want to change, you could just hand edit the file since it would only be a single change.

                            Comment


                            • #15
                              wbuchanan

                              Thank you for your feedback.

                              I use your package a lot in my work, however, some outputs require the node to be written without spaces, as I am trying to automate these tasks, therefore, I can't hand edit these nodes every time I run the do file.

                              The reason for my query is that I find the syntax structure of serializing the JSON file is similar to exporting excel which has the option of exporting the file using the label of variables and naming the sheet.

                              Thus, I wondered if it can be done in JSONIO, to upgrade it and make it more practical not only for me but also for others.

                              Having said that, I would like to thank you for your collaboration offer, the code was very clear, but unfortunately, I am not very familiar with STATA programming, hence I can't help much.

                              If you have any suggestions to edit these nodes with windows shell it would be highly appreciated, I did some research on using filfilter or sed/awk, but it was not clear to me how to do it.

                              Thank you for your time.


                              Comment

                              Working...
                              X