1

I have been trying to parse this JSON that I am getting from an API. It just doesn't seem to work. It inserts the whole response into a single object or array when I try to pass it into an object or array. I'm just posting the API response and not my code that I tried:

{"canonical_url": "http://magento2-demo.nexcess.net/"}{"shop_properties": {"is_hypernode": false, "hypernode_without_magento": false, "loadtime_milliseconds": 638}}{"admin_url": null}{"security.supee5344": {"magento2": false, "resultString": "backend url required", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.supee5994": {"magento2": false, "resultString": "installed", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.ransomware": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.cart2quoterce": {"magento2": false, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.ajaxcartrce": {"magento2": false, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.opendev": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.openversioncontrol": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.sslcheck": {"magento2": true, "resultString": "misconfigured", "riskRating": "medium", "result": "fail", "magento1": true, "cloudflareCacheHit": null}}{"security.guruincinfection": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.creditcardhijack": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.visbot": {"magento2": false, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.cryptojacking": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.outdatedserverversion": {"magento2": true, "resultString": "unknown", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.amastyopendownload": {"magento2": false, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.cacheleak": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.openmagmi": {"magento2": false, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.webforms": {"magento2": false, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.bruteforce": {"magento2": false, "resultString": "protected", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.magversion": {"magento2": true, "resultString": "CE 2.1.0 - 2.1.2", "riskRating": "high", "result": "fail", "magento1": true, "cloudflareCacheHit": null}}{"security.supee6788": {"magento2": false, "resultString": "installed", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.supee8788": {"magento2": false, "resultString": "unknown", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.supee9652": {"magento2": false, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.supee10570": {"magento2": false, "resultString": "patched", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.supee10752": {"magento2": false, "resultString": "patched", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.supee6285": {"magento2": false, "resultString": "unknown", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.supee6482": {"magento2": false, "resultString": "unknown", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.exposedapi": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": false, "cloudflareCacheHit": null}}{"security.supee10266": {"magento2": false, "resultString": "backend url required", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.supee9767": {"magento2": false, "resultString": "unknown", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.supee10415": {"magento2": false, "resultString": "unknown", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.supee7405": {"magento2": false, "resultString": "unknown", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}
6
  • 14
    It isn't JSON. Fix the API so it is JSON. (Or write a custom parser to attempt to separate out the different objects … which is an awful idea). Commented Sep 23, 2018 at 8:18
  • I do not have the access to the API. This is an external API. Commented Sep 23, 2018 at 8:24
  • What is the desired result? Commented Sep 23, 2018 at 8:36
  • @Quentin relaxedjson.org Commented Sep 23, 2018 at 8:44
  • 2
    you should request the developer of the api your using to check this this is an invalid json response. Commented Sep 23, 2018 at 8:50

5 Answers 5

4

https://www.npmjs.com/package/really-relaxed-json can help you out here. Unfortunately for some reason StackOverflow snippets cannot run it when included as an external resource (from CDN https://cdn.jsdelivr.net/npm/[email protected]/relaxed-json.js), so I created a Stackblitz demo here:

https://js-ei2iz2.stackblitz.io <-- check the console

Here's the editable example:

https://stackblitz.com/edit/js-ei2iz2

let response = `{  
       "canonical_url":"http://magento2-demo.nexcess.net/"
    }{  
       "shop_properties":{  
          "is_hypernode":false,
          "hypernode_without_magento":false,
          "loadtime_milliseconds":535
       }
    }{  
       "admin_url":null
    }{  
       "security.supee5344":{  
          "magento2":false,
          "resultString":"backend url required",
          "riskRating":"low",
          "result":"unkn",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.supee5994":{  
          "magento2":false,
          "resultString":"installed",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.cart2quoterce":{  
          "magento2":false,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.ransomware":{  
          "magento2":true,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.ajaxcartrce":{  
          "magento2":false,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.openversioncontrol":{  
          "magento2":true,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.opendev":{  
          "magento2":true,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.guruincinfection":{  
          "magento2":true,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.creditcardhijack":{  
          "magento2":true,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.visbot":{  
          "magento2":false,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.cryptojacking":{  
          "magento2":true,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.outdatedserverversion":{  
          "magento2":true,
          "resultString":"unknown",
          "riskRating":"low",
          "result":"unkn",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.cacheleak":{  
          "magento2":true,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.amastyopendownload":{  
          "magento2":false,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.sslcheck":{  
          "magento2":true,
          "resultString":"misconfigured",
          "riskRating":"medium",
          "result":"fail",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.webforms":{  
          "magento2":false,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.openmagmi":{  
          "magento2":false,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.magversion":{  
          "magento2":true,
          "resultString":"CE 2.1.0 - 2.1.2",
          "riskRating":"high",
          "result":"fail",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.supee6788":{  
          "magento2":false,
          "resultString":"installed",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.supee8788":{  
          "magento2":false,
          "resultString":"unknown",
          "riskRating":"low",
          "result":"unkn",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.supee9652":{  
          "magento2":false,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.supee10570":{  
          "magento2":false,
          "resultString":"patched",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.supee10752":{  
          "magento2":false,
          "resultString":"patched",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.supee6285":{  
          "magento2":false,
          "resultString":"unknown",
          "riskRating":"low",
          "result":"unkn",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.supee6482":{  
          "magento2":false,
          "resultString":"unknown",
          "riskRating":"low",
          "result":"unkn",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.exposedapi":{  
          "magento2":true,
          "resultString":"safe",
          "riskRating":"low",
          "result":"ok",
          "magento1":false,
          "cloudflareCacheHit":null
       }
    }{  
       "security.bruteforce":{  
          "magento2":false,
          "resultString":"protected",
          "riskRating":"low",
          "result":"ok",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.supee10266":{  
          "magento2":false,
          "resultString":"backend url required",
          "riskRating":"low",
          "result":"unkn",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.supee9767":{  
          "magento2":false,
          "resultString":"unknown",
          "riskRating":"low",
          "result":"unkn",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.supee10415":{  
          "magento2":false,
          "resultString":"unknown",
          "riskRating":"low",
          "result":"unkn",
          "magento1":true,
          "cloudflareCacheHit":null
       }
    }{  
       "security.supee7405":{  
          "magento2":false,
          "resultString":"unknown",
          "riskRating":"low",
          "result":"unkn",
          "magento1":true,
          "cloudflareCacheHit":nulla
       }
    }`

    response = "["+response+"]"

    console.log(RJSON.parse(response))
Sign up to request clarification or add additional context in comments.

3 Comments

Nice answer! I would use this library in the future!
I tried this approach but still cannot traverse the response. stackblitz.com/edit/js-uwv4ar
@MahirRawal You are actually correct. Somehow on Stackblitz the result of toJS(fresponse) is still considered a string - but if you copy and paste the console.log output to a Chrome console and prepend let a = you'll see it is a proper Javascript Array.
0
function parse(str) {
{
  let splitted = str.split('}{')

  let json = [];

  splitted.forEach((s, i) => {
    let fullObjectString = i == 0 ? s + '}' : i == splitted.length - 1 ? '{' + s : '{' + s + '}'
    json.push(
      JSON.parse(fullObjectString)
    )
  })

 return json
}

Demo

3 Comments

This could work if you can guarantee that }{ does not occur within a string inside any of the objects.
Yes, but as I see in the example response, there is not reason why there should be }{ in an object.
That's for the questioner to decide. I'm just pointing out that this solution is not 100% bulletproof. }{ could be part of a unit test. :-)
0

I converted your json to array of objects this way, this would parse in array of objects

var x=`{  
   "canonical_url":"http://magento2-demo.nexcess.net/"
}{  
   "shop_properties":{  
      "is_hypernode":false,
      "hypernode_without_magento":false,
      "loadtime_milliseconds":535
   }
}{  
   "admin_url":null
}{  
   "security.supee5344":{  
      "magento2":false,
      "resultString":"backend url required",
      "riskRating":"low",
      "result":"unkn",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.supee5994":{  
      "magento2":false,
      "resultString":"installed",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.cart2quoterce":{  
      "magento2":false,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.ransomware":{  
      "magento2":true,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.ajaxcartrce":{  
      "magento2":false,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.openversioncontrol":{  
      "magento2":true,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.opendev":{  
      "magento2":true,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.guruincinfection":{  
      "magento2":true,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.creditcardhijack":{  
      "magento2":true,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.visbot":{  
      "magento2":false,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.cryptojacking":{  
      "magento2":true,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.outdatedserverversion":{  
      "magento2":true,
      "resultString":"unknown",
      "riskRating":"low",
      "result":"unkn",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.cacheleak":{  
      "magento2":true,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.amastyopendownload":{  
      "magento2":false,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.sslcheck":{  
      "magento2":true,
      "resultString":"misconfigured",
      "riskRating":"medium",
      "result":"fail",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.webforms":{  
      "magento2":false,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.openmagmi":{  
      "magento2":false,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.magversion":{  
      "magento2":true,
      "resultString":"CE 2.1.0 - 2.1.2",
      "riskRating":"high",
      "result":"fail",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.supee6788":{  
      "magento2":false,
      "resultString":"installed",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.supee8788":{  
      "magento2":false,
      "resultString":"unknown",
      "riskRating":"low",
      "result":"unkn",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.supee9652":{  
      "magento2":false,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.supee10570":{  
      "magento2":false,
      "resultString":"patched",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.supee10752":{  
      "magento2":false,
      "resultString":"patched",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.supee6285":{  
      "magento2":false,
      "resultString":"unknown",
      "riskRating":"low",
      "result":"unkn",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.supee6482":{  
      "magento2":false,
      "resultString":"unknown",
      "riskRating":"low",
      "result":"unkn",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.exposedapi":{  
      "magento2":true,
      "resultString":"safe",
      "riskRating":"low",
      "result":"ok",
      "magento1":false,
      "cloudflareCacheHit":null
   }
}{  
   "security.bruteforce":{  
      "magento2":false,
      "resultString":"protected",
      "riskRating":"low",
      "result":"ok",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.supee10266":{  
      "magento2":false,
      "resultString":"backend url required",
      "riskRating":"low",
      "result":"unkn",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.supee9767":{  
      "magento2":false,
      "resultString":"unknown",
      "riskRating":"low",
      "result":"unkn",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.supee10415":{  
      "magento2":false,
      "resultString":"unknown",
      "riskRating":"low",
      "result":"unkn",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}{  
   "security.supee7405":{  
      "magento2":false,
      "resultString":"unknown",
      "riskRating":"low",
      "result":"unkn",
      "magento1":true,
      "cloudflareCacheHit":null
   }
}`;
anotherString = x.replace(/}{/g, '}\,{');
anotherString = '{ "arrayOfObjects":['+anotherString+  ']}';
console.log(anotherString);

Comments

0

Since you know this is badly formatted JSON anyway, it is probably well acceptable to do string operations.

E.g., replace "\n}{\n" with "\n},{\n" (try to use the longest possible pattern to a reduce the risk of false positives, such as }{ in some string - there shouldn't be a non-encoded newline in JSON strings.)

2 Comments

There are no new line characters in the response. Its just one continuous piece of data.
Choose whatever is fit to work around the bad data. Use }{"security then, for example. You know the data. If you can't get the API fixed, you need to do a workaround.
0

If you use Python, you can try this half json fixer

    from half_json.core import JSONFixer

    s = '''{}{}{}'''  # your half json
    f = JSONFixer(100)
    result = f.fix('['+ s.strip() + ']')
    # result.success == True
    datas = json.loads(result.line)
    # [{},{},{}]

Comments

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.