PROC GROOVY Parsing JSON

Source: Internet
Author: User

Reference: http://saslist.com/blog/category/json/

Download JDK and Groovy
1. Install JDK default installation
2 Decompression Groovy: to C:\Program Files (x86) \
With reference to the http://saslist.com/blog/category/json/about groovy, configure groovy and set system variables:
**********
(1) JDK configuration:
Set the environment variable%java_home%\bin value to the directory where the extracted files are placed, such as C:\Program files (x86) \java\jdk1.8.0_141
Add environment variable path, point to%groovy_home%\bin
(2) Groovy Configuration
Set the environment variable Groovy_home value to the directory where the extracted files are placed, such as C:\Program files (x86) \groovy-2.4.12
Add environment variable path, point to%groovy_home%\bin
(3) Find the path to the SAS execution program, such as C:\Program files\sashome\sasfoundation\9.4
The path under the \nls\en sasv9.cfg, find jreoptions, add
-dtkj.app.class.path=c:\program Files (x86) \groovy\groovy-2.4.12\embeddable\groovy-all-2.4.12.jar

3 Put the parsing results in the CSV, you need to put opencsv-3.10 under the groovy installation package embeddable below

4. Next you can write a script in the groovy console, enter the Groovyconsole command in the Windows Console (CMD) to enter the groovy console, enter the code inside the println "HelloWorld" carriage return, press the key combination CTRL + R to see the results of the operation.

Analytical:

Json:

{
"ACCT_NBR": 1234,
"FirstName": "John",
"LastName": "Smith",
"Age": 25,
"Address": {
"StreetAddress": "2nd Street",
"City": "New York",
"State": "NY",
"PostalCode": "10021"
}
}
,
{
"ACCT_NBR": 3456,
"FirstName": "Sam",
"LastName": "Jones",
"Age": 32,
"Address": {
"StreetAddress": "2nd Street",
"City": "New jersy",
"State": "NJ",
"PostalCode": "10081"
}
}

Parsing code:


%let Groovydir=c:\program Files (x86) \groovy-2.4.12\embeddable; *the Location The Groovy JARs is located at;

%let Sourcefile=.\data\test_1.txt;
%let outfile=.\data\test_1.csv;

Proc Groovy Classpath= "&groovydir.\groovy-all-2.4.12.jar;&groovydir.\opencsv-3.10.jar";

Submit "&sourcefile" "&outfile";
Import groovy.json.*/* Load JSON package */
Import com.opencsv.*

def input = new File (Args[0]). Text
def output = new Jsonslurper (). ParseText (Input)
def csvoutput = new FileWriter (args[1])

Csvwriter writer = new Csvwriter (csvoutput);

String[] Header = new string[2];
Header[0] = "User_basic";
HEADER[1] = "User_blacklist";
Writer.writenext (header);

Output.statuses.each {
string[] User_basic_content1 = new String[8];
Content[0] = it.user_basic.user_age.toString ();
CONTENT[1] = it.user_basic.user_city.toString ();
CONTENT[2] = it.user_basic.user_gender.toString ();
CONTENT[3] = it.user_basic.user_idcard.toString ();
CONTENT[4] = it.user_basic.user_idcard_valid.toString ();
CONTENT[5] = it.user_basic.user_name.toString ();
CONTENT[6] = it.user_basic.user_phone.toString ();
CONTENT[7] = it.user_basic.user_phone_city.toString ();
Writer.writenext (USER_BASIC_CONTENT1);
}

Writer.close ();

Endsubmit;
Quit

PROC GROOVY Parsing JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.