Gilbert Gabriel
Gilbert Gabriel

Reputation: 425

Reading and parsing raw data from JSON file

I am using QzTray to print receipt through my nodejs app. I have to create a json array that looks like this

[
    '\x1B'+'\x40',
    '\x1B'+'\x61'+'\x31',
    'Beverly Hills, CA  90210'+'\x0A',
    '\x0A',
    'www.qz.io'+'\x0A',
    '\x0A',
    '\x0A',
    'May 18, 2016 10:30 AM'+'\x0A',
    '\x0A',
    '\x0A',
    '\x0A',
    'Transaction # 123456 Register: 3'+'\x0A',
    '\x0A',
    '\x0A',
    '\x0A',
    '\x1B'+'\x61'+'\x30',
    'Baklava (Qty 4)       9.00'+'\x1B'+'\x74'+'\x13'+'\xAA',
    '\x0A',
    'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'+'\x0A',
    '\x1B'+'\x45'+'\x0D',
    'Here\'s some bold text!',
    '\x0A',
    '\x1B'+'\x45'+'\x0A',
    '\x1D'+'\x21'+'\x11',
    'Here\'s large text!',
    '\x0A',
    '\x1D'+'\x21'+'\x00',
    '\x1B'+'\x61'+'\x32',
    '\x1B'+'\x21'+'\x30',
    'DRINK ME',
    '\x1B'+'\x21'+'\x0A'+'\x1B'+'\x45'+'\x0A',
    '\x0A'+'\x0A',
    '\x1B'+'\x61'+'\x30',
    '------------------------------------------'+'\x0A',
    '\x1B'+'\x4D'+'\x31',
    'EAT ME'+'\x0A',
    '\x1B'+'\x4D'+'\x30',
    '------------------------------------------'+'\x0A',
    'normal text',
    '\x1B'+'\x61'+'\x30',
    '\x0A'+'\x0A'+'\x0A'+'\x0A'+'\x0A'+'\x0A'+'\x0A',
    '\x1B'+'\x69',
    '\x10'+'\x14'+'\x01'+'\x00'+'\x05',
  ]

The problem is that when I try to parse this file using JSON.parse() , I always get the error

Uncaught SyntaxError: Unexpected token ' in JSON at position 7
    at JSON.parse (<anonymous>)
    at print (electronAPI_1.0.js:41)
    at HTMLInputElement.onclick (index.html:15)

Here is the code I use if it can help

var jsonConfig1 = JSON.parse(fs.readFileSync('couponConfig1.json', 'utf8'));

Upvotes: 1

Views: 1852

Answers (1)

Sarah
Sarah

Reputation: 470

Your JSON contains single quotes, rather than double quotes, which will throw an error.
You are also using ANSI escape codes, versus UTF-16 codes. UTF-16 codes are what JSON use, therefore producing an error when your Javascript attempts to read the JSON.
It's also possible you were using UTF-8 escape codes, which share 127 identical characters to ANSI. In that case, you could use http://www.fileformat.info/info/unicode/ to convert your commands to UTF-16.

Upvotes: 2

Related Questions