
Research
Security News
Malicious PyPI Package Exploits Deezer API for Coordinated Music Piracy
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Get the text position [start, end] of a property in a JSON document.
Given the following JSON:
{
"foo": {
"bar": "baz"
^^^^^
}
}
The position of foo.bar
(or ["foo", "bar"]
if provided as an array), is:
{
start: { line: 3, column: 16, offset: 30 },
end: { line: 3, column: 21, offset: 35 }
}
where offset
is the character offset in the JSON string.
If the property "bar" is wanted, instead of the value, set markIdentifier
to true
, see Simple usage.
npm i jsonpos
or yarn add jsonpos
jsonpos( json, options: LocationOptions ): Location
where LocationOptions
is:
interface LocationOptions
dataPath: string | Array< string | number >;
markIdentifier?: boolean;
}
and Location
is:
interface Location
{
start: Position | undefined;
end: Position | undefined;
}
where Position
is:
interface Position
{
line: number;
column: number;
offset: number;
}
import { jsonpos } from 'jsonpos'
const loc = jsonpos(
'{ "foo": { "bar": "baz" } }',
{ dataPath: 'foo.bar' }
);
import { jsonpos } from 'jsonpos'
const loc = jsonpos(
'{ "foo": { "bar": "baz" } }',
{ dataPath: [ 'foo', 'bar' ] }
);
The jsonpos
function is a shorthand for getLocation( getAstByString( json ), options )
Extract the AST (using json-to-ast) with getAstByString
or getAstByObject
. The result is an object of type ParsedJson
:
interface ParsedJson
{
json: any;
jsonString: string;
jsonAST: ValueNode; // ValueNode is a json-to-ast type
}
import { getAstByString } from 'jsonpos'
const ast = getAstByString( '{ "foo": "bar" }' );
const { json, jsonString, jsonAST } = ast;
getAstByObject
will stringify the JSON using JSON.stringify(obj, null, 4)
and use that to parse the AST.
import { getAstByObject } from 'jsonpos'
const ast = getAstByObject( { foo: "bar" } );
const { json, jsonString, jsonAST } = ast;
getAstByObject
takes an optional second argument indent
which can be set to something else than 4
if necessary, e.g. 2
:
const ast = getAstByObject( { foo: "bar" }, 2 );
The getLocation
takes an ast object as returned by getAstByString
or getAstByObject
and returns a Location
object.
getLocation( ast: ParsedJson, options: LocationOptions ): Location
import { getAstByString, getLocation } from 'jsonpos'
const ast = getAstByString( '{ "foo": "bar" }' );
const loc = getLocation( ast, { dataPath: 'foo' } );
FAQs
Get the textual position of a property in a JSON text
The npm package jsonpos receives a total of 24,486 weekly downloads. As such, jsonpos popularity was classified as popular.
We found that jsonpos demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.
Security News
Newly introduced telemetry in devenv 1.4 sparked a backlash over privacy concerns, leading to the removal of its AI-powered feature after strong community pushback.