1

I have a JSON file created as response by running multiple curl statements (GET requests to a service) in parallel.

Now when I try to parse the JSON file using python it fails as the file has multiple JSONs appended

Error:

Error: Parse error on line 1: ...CT_AGGREGATE"}]}]}]}{"serviceResponseTim -----------------------^ Expecting 'EOF', '}', ',', ']', got '{'

Example in the file:

{"serviceResponseTime":510, ........... .......:[{"categoryId":28162,"metricValue":97,"days":"NONE","metricMappedType":"PRODUCT_AGGREGATE"}]}]}]}{"serviceResponseTime":387,"products":[{"productReferenceId":...................

As you see these are 2 response obtained from 2 curl statements but as I ran them in parallel using xargs, it appended the output in 1 line.

Now my problem is parsing this file.

Can anyone suggest a way to either put the output of curl commands into separate lines in the file.

Or a crude way I thought was if I could extract the part from beginning of the file till "}]}]}]}{", cut from the current file and put it into another file and run the JSON parser on that file which would have just 1 JSON o/p. Repeat this in loop till the original file is empty after we have cut out each of the JSON from it. Now my problem is I am not able to extract from beginning of line to }]}]}]}{.

Also all JSON o/ps are appended in 1 line and are not on separate lines else I would have read line by line.

Can anyone help here?

1 Answer 1

3

Let's say you have this input file:

{"name": "foo"}{"name": "bar"}

You can use the jq command:

jq -s . input.file

to transfer input.file into:

[
  {
    "name": "foo"
  },
  {
    "name": "bar"
  }
]

I hope that helps

Sign up to request clarification or add additional context in comments.

1 Comment

I installed jq on my mac and run the command but it says -sh: jq: command not found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.