Search code examples
rjsonimportnotepad++

Reading JSON with id outside curly brackets and no quotation marks


I am having some troubles importing a json file to R with the following format:

C135-HR2459 {"number_a": 1, "number_b":2} 
C156-HR2249 {"number_a": 1, "number_b":2} 

It would have worked if it had the following format:

{"id": C135-HR2459, "number_a": 1, "number_b":2} 
{"id": C156-HR2249, "number_a": 1, "number_b":2} 

Solution

  • Using Notepad++, you can do:

    • Ctrl+H
    • Find what: ^(\S+) {
    • Replace with: {"id": "$1",
    • CHECK Wrap around
    • CHECK Regular expression
    • UNCHECK . matches newline
    • Replace all

    Explanation:

    ^           # beginning of line
    (\S+)       # group 1, 1 or more non space character; you can use (.+?) if the string contains spaces
                # 1 space
    {           # open curly brace
    

    Replacement:

    {"id": "    # literally
    $1          # content of group 1
    ",          # literally, there is a space after the comma
    

    Screenshot (before):

    enter image description here

    Screenshot (after):

    enter image description here