Search code examples
jsonjsonlines

Is it problematic to have a JSON Lines file that has mixed JSON structures?


I would like to know whether, if a JSON Lines file is structured like this:

{"structure1":"some kind of file header"}
{"structure2": [ {"key1":"aaa", "key2":"bbb"}, {"key1":"one", "key2":"two"}]
{"structure2": [ {"key1":"xxx", "key2":"yyy"}, {"key1":"first", "key2":"second"}]
{"structure3":"maybe some kind of file footer"}

Is it considered a non-valid JSONLines format? I looked at the standard at http://jsonlines.org/ and I couldn't see anything one way or the other. Thank you.


Solution

  • Your lines are each one valid ( but missing a '}' and the end of the second and third). but if you want to put them all in one file it will be not valid. They have to be in array and separated with , or an object with key/value ( where the value can be array or object )

    example of array :

    [
     {"structure1":"some kind of file header"},
     {"structure2": [ {"key1":"aaa", "key2":"bbb"}, {"key1":"one", "key2":"two"}]},
     {"structure2": [ {"key1":"xxx", "key2":"yyy"}, {"key1":"first", "key2":"second"}]},
     {"structure3":"maybe some kind of file footer"}
    ]
    

    or an object :

    {
    "structure1": "some kind of file header",
    "structure2": [{
        "key1": "aaa",
        "key2": "bbb"
    }, {
        "key1": "one",
        "key2": "two"
    }],
    "structure2": [{
        "key1": "xxx",
        "key2": "yyy"
    }, {
        "key1": "first",
        "key2": "second"
    }],
    "structure3": "maybe some kind of file footer"
    

    }

    You can test your json on this site to see if valid or not : http://www.jslint.com/ or http://jsonlint.com/