Pregunta

I am trying to read data from JSON file and store the value in SAS dataset Here is my JSON file test.json

[
  {
    "id":1,
    "name":"Johnson, Smith and Jones Co.",
    "amount":345.33,
    "Remark":"Pays on time"
  },
  {
    "id":2,
    "name":"Sam, Smith",
    "amount":993.44,
    "Remark":""
  },
  {
    "id":3,
    "name":"Barney & Company",
    "amount":0,
    "Remark":"Great to work with and always pays with cash."
  },
  {
    "id":4,
    "name":"Johnson's Automotive",
    "amount":2344,
    "Remark":""
  }
]

Now I want the output like simple SAS dataset where I can fetch as a tabular form. If I use proc print dataset, it will look like :

id  name    amount  Remark
1   Johnson, Smith and Jones Co.    345.33  Pays on time
2   Sam, Smith  993.44  
3   Barney & Company    0   Great to work with and always pays with cash.
4   Johnson's Automotive    2344

Here is my approach but not getting proper output.

LIBNAME src  '/home/user/read_JSON';
filename data '/home/user/read_JSON/test.json';
data src.testdata;
    infile data lrecl = 32000 truncover scanover;
    input @'"id": "' id $255. @'"name": "' name $255. @'"amount": "' amount @'"Remark": "' Remark $255.;
    id = substr(id,1,index(id,'",')-2);
    name = substr( name,1,index( name,'",')-2);
    amount = substr(amount,1,index(amount,'",')-2);
    Remark = substr(Remark,1,index(Remark,'",')-2);
run;

proc print data=src.testdata;
RUN;

any idea how i do it???

¿Fue útil?

Solución

Set firstobs=3 to ignore the first two lines, then use the / input modifier to read the next line. The two trailing / ignore the subsequent JSON delimiters.

data testdata;
    infile data lrecl=32000 truncover dsd firstobs=3 ;

    input @'"id":' id $255.
        / @'"name":' name $255.
        / @'"amount":' amount
        / @'"Remark":' Remark $255.
        /
        /
    ;
run;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top