> npm install json-to-ast
var parse = require('json-to-ast');
var settings = {
verbose: true, // Show additional information, like node’s location. Default is <true>
source: 'data.json' // Adds source information to node’s location. Default is <null>
};
parse('{"a": 1}', settings);
Output
{
type: 'object',
children: [
{
type: 'property',
key: {
type: 'identifier',
value: 'a',
loc: {
start: { line: 1, column: 2, offset: 1 },
end: { line: 1, column: 5, offset: 4 },
source: 'data.json'
}
},
value: {
type: 'literal',
value: 1,
rawValue: '1',
loc: {
start: { line: 1, column: 7, offset: 6 },
end: { line: 1, column: 8, offset: 7 },
source: 'data.json'
}
},
loc: {
start: { line: 1, column: 2, offset: 1 },
end: { line: 1, column: 8, offset: 7 },
source: 'data.json'
}
}
],
loc: {
start: { line: 1, column: 1, offset: 0 },
end: { line: 1, column: 9, offset: 8 },
source: 'data.json'
}
}
object:
{
type: 'object',
children: <property[]>,
loc: Object | null
}
property:
{
type: 'property',
key: <identifier>,
value: any,
loc: Object | null
}
identifier:
{
type: 'identifier',
value: String,
loc: Object | null
}
array:
{
type: 'array',
children: <any[]>,
loc: Object | null
}
literal:
{
type: 'literal',
value: String | Number | Boolean | null,
rawValue: String,
loc: Object | null
}
Try it online on astexplorer.net
MIT