dan
dan

Reputation: 493

How to restructure json content- convert it to jsonlines

I have a json file, it was generated using rest api call as below:

import json
resp = requests.get('https://....offset=0&limit=500&where=....', headers=headers)
json_data = json.loads(resp.text)

with open('strings.json') as f:
    d = json.load(f)
    print(d)

After reading it in variable d, I get following structure data:

d = [{}, {}, {},{}]

I want to convert above data structure into following format and store it in json file which is also called as jsonlines https://jsonlines.org/examples/:

required_format = {}{}{}{}

In a way, I want to remove outer square brackets and also space, comma between adjacent dictionaries and then again store it in json or any other file format or even without extension. this file it should have this structure after opening it in the chrome or any other browser.

 {}{}{}{}

Upvotes: 2

Views: 1248

Answers (3)

Tau n Ro
Tau n Ro

Reputation: 108

The simplest way of doing it:

import json

with open('strings.json', 'r') as f:
    d = json.load(f)

output_path = "your/output/path/result.jsonl" 
with open(output_path, 'w') as f:
    for line in d:
        f.write(json.dumps(line) + '\n')

Upvotes: 1

N1ngu
N1ngu

Reputation: 3854

A jsonlines python library exists, so you could go ahead and try to use that.

import json
import jsonlines

with open('strings.json', 'r') as f:
    d = json.load(f)

with jsonlines.open('strings.jsonl', 'w') as f:
    f.write_all(d)

See https://jsonlines.readthedocs.io/en/latest/#user-guide

Upvotes: 2

Luatic
Luatic

Reputation: 11201

[{}, {}, {},{}]

is valid JSON.

{}{}{}{}

is not. It is a different, JSON-based serialization format ("JSON lines" using lines as delimiters apparently). You should be able to simply generate this using a list comprehension and some JSON serialization for the objects:

"".join([json.dumps(obj) for obj in d])

This will yield a string. If you want to write directly to a file, use json.dump:

with f as io.open(..., "w"):
    for obj in d:
        json.dump(obj, f)

Edit: As the desired format is apparently the "JSON lines" format, just emit newlines when writing to a file or concat using them when stringifying (do you want a trailing newline BTW?). The code then becomes the following:

"\n".join([json.dumps(obj) for obj in d])

and

with f as io.open(..., "w"):
    for obj in d:
        json.dump(obj, f)
        f.write("\n")

respectively.

Upvotes: 6

Related Questions