Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map JSON-pointer to the {line, column} in a text JSON file

I need to map a location in JSON (or JavaScript object) defined by JSON-pointer to the position in JSON text file as {line,column}. Is there any existing JavaScript library that would do it? Writing this code is going to be a bit tedious...

For example, if I have a JSON file (text):

{
  "foo": [
    {
      "bar": 1
    }
  ]
}

then given JSON-pointer /foo/0/bar I need to get {line: 4, column: 7} as the result.

If an equivalent JSON value is stored in this JSON file:

{"foo":[{"bar":1}]}

then the result for the same JSON pointer should be {line: 1, column: 10}.

like image 451
esp Avatar asked Jan 08 '17 18:01

esp


People also ask

Why do we need to convert JSON data into map?

We often need to convert JSON responses into a map to work with the returned JSON data easily. We can easily convert JSON data into a map because the JSON format is essentially a key-value pair grouping and the map also stores data in key-value pairs.

What is @JSON lines?

JSON Lines handles tabular data and clearly identifies data types without ambiguity. This allows records to be processed one at a time, which makes the format very useful for exporting and sending data.

What is a JSON pointer?

Although it uses JavaScript syntax, it's language-independent, since the resultant is plain text. JSON Pointer ( RFC 6901) is a feature from JSON Processing 1.1 API ( JSR 374 ). It defines a String that can be used for accessing values on a JSON document. It can be related to what XPath does for an XML document.

How do I export JSON from the command line?

To do so, simply select “File > Export As” and select the JSON file type. You can export json from the command line with the --export-map option. The fields found in the JSON format differ slightly from those in the TMX Map Format, but the meanings should remain the same.


1 Answers

This library has the equivalents of JSON.parse/stringify that also return mappings: https://github.com/epoberezkin/json-source-map

like image 120
esp Avatar answered Sep 22 '22 10:09

esp