How do I parse this crap Json?



  • {
    "status": "success",
    "error": "",
    "response": "[\"{\\\"ip_id\\\":\\\"202\\\",\\\"ip_name\\\":\\\"P b \\\",\\\"small_desc\\\":\\\"Growth Hacker\\\",\\\"large_desc\\\":\\\"Sample description\\\",\\\"join_date\\\":\\\"\\\",\\\"vid_url\\\":\\\"https:\\\/\\\/www.theug.app\\\/user_videos\\\/pr_202.mp4\\\",\\\"img_url\\\":\\\"https:\\\/\\\/www.thnug.app\\\/user_thumbs\\\/s.jpg\\\",\\\"current_status\\\":\\\"0\\\",\\\"rate_per_hour\\\":\\\"1300\\\",\\\"currency\\\":\\\"\\u20b9\\\"}\",\"{\\\"ip_id\\\":\\\"217\\\",\\\"ip_name\\\":\\\"ss dd\\\",\\\"small_desc\\\":\\\"Talented\\\",\\\"large_desc\\\":\\\"Sample description2 \\\",\\\"join_date\\\":\\\"\\\",\\\"vid_url\\\":\\\"https:\\\/\\\/www.thug.app\\\/user_videos\\\/d.mp4\\\",\\\"img_url\\\":\\\"https:\\\/\\\/www.tsnug.app\\\/user_thumbs\\\/d.jpg\\\",\\\"current_status\\\":\\\"0\\\",\\\"rate_per_hour\\\":\\\"3850\\\",\\\"currency\\\":\\\"\\u20b9\\\"}\"]"}
    
    

    Following is my code and the error I am getting

    Screenshot 2020-04-27 at 12.34.06 PM.png Screenshot 2020-04-27 at 1.58.11 PM.png

    Can Anyone please help?
    Also, On StackOverflow

    EDIT : Json File in Code Block : I am new to this website and am overwhelmed by the responses.
 Also because I got Downvoted, lost Reputation for the same on StackOverflow and got very little help :P
    Thanks a lot guys!


  • Discourse touched me in a no-no place

    6dd5d029-52fc-44ac-833f-4506bc951349-image.png

    936f0db3-1270-4882-a2f4-9d5ae995525f-image.png


  • Notification Spam Recipient

    @Sameeh-Abdul said in How do I parse this crap Json?:

    Can Anyone please help?

    Your "Response" string is fuckled. Are you escaping it by hand? Please don't do that...


  • Banned

    @Sameeh-Abdul oh shit. Oh shit oh shit oh shit.

    The root is an object with three fields. The last one, "response", is a string that contains yes another JSON, where the root is an array of strings. Each of the strings in the array is yet another JSON.

    Basically - you have to recursively deserialize JSONs. First you deserialize the root to an object. Then you read the object's "response" field as string, which you then deserialize as JSON again. You get an array of strings, which you have to loop through and deserialize each element separately as yet another JSON.

    I hope it makes sense.


  • Notification Spam Recipient

    @Gąska said in How do I parse this crap Json?:

    I hope it makes sense.

    I think that was the intent, but the first level is broken. Either that or the paste wasn't the actual raw JSON...


  • Banned

    @Tsaukpaetra I don't see anything broken here. An unnecessary serialization-within-serialization and the ugly triple backslashes, but nothing that would be actually ill-formed. The root root is an object with 3 string properties.


  • Notification Spam Recipient

    @Gąska said in How do I parse this crap Json?:

    @Tsaukpaetra I don't see anything broken here. An unnecessary serialization-within-serialization and the ugly triple backslashes, but nothing that would be actually ill-formed. The root root is an object with 3 string properties.

    So you see nothing wrong with this part?

    @Sameeh-Abdul said in How do I parse this crap Json?:

    ,\\"{..."}\\"\]

    I'll give you a hint: There's something weird about the character after the three periods...

    Edit: Fix select-to-quote fuckup

    Edit edit: But yeah, once you get the actual JSON string correctly, then it will require a recursive parse.



  • @Gąska Here's the problem.

    "response": "\[\\"

    means that response should contain [\

    Presumably the " was supposed to be escaped, because it keeps on going. But the " wasn't escaped, so the part that follows it is a syntax error. In other words, it should presumably be \" instead of \\".


  • Notification Spam Recipient

    @brie said in How do I parse this crap Json?:

    Here's the problem.

    I'm pondering on the order of escape substitution. Does \\ get processed before or after \"? 🤔

    either way, this JSON is fucky wucky.



  • @Tsaukpaetra It's parsed from left to right.



  • @Sameeh-Abdul post the code that's producing the JSON, because somehow it's being escaped incorrectly.



  • Holy fuck, someone is doing it very wrong there.
    This JSON is best fixed by a gratuitous use of a cluebat or banhammer.

    But

    ",\"{...\\\"}\"]"
    

    at the end would make the topmost level of the JSON parse.
    I have not looked at making sense out of the horrible hack of a jsonstringjsonstring that is in response though, I wouldn't be surprised to see that it's also similarly broken. Though I'm guessing from the ellipsis right there in that bit of "json" that you or someone else, have excluded significant amounts of JSON here.
    Have fun unpacking this particular nugget of :doing_it_wrong:



  • @dkf said in How do I parse this crap Json?:

    936f0db3-1270-4882-a2f4-9d5ae995525f-image.png

    This is not only json, but it also has some jgrandson.


  • Banned

    @Tsaukpaetra said in How do I parse this crap Json?:

    I'll give you a hint: There's something weird about the character after the three periods...

    The three periods means text omitted for brevity. I bet $10 that there's actually a triple backslash before the quote there.

    @brie said in How do I parse this crap Json?:

    @Gąska Here's the problem.

    "response": "\[\\"

    means that response should contain [\

    There's no double backslash there, only single backslash. At least on my device.

    eb585ea3-0ef2-44c9-8eed-0f321a90a91d-image.png

    Edit: inb4 my own screenshot shows differently to other people. :tinfoil-hat:



  • @Gąska said in How do I parse this crap Json?:

    There's no double backslash there, only single backslash. At least on my device.

    eb585ea3-0ef2-44c9-8eed-0f321a90a91d-image.png

    Edit: inb4 my own screenshot shows differently to other people. :tinfoil-hat:

    I went to the raw view, because the baked JSON is certainly wrong and I didn't really figure that OP had tried to Markdown-escape it. Although now that I think about it, it does look like he tried to escape it, and the errors could have been entirely introduced in the process of trying to escape it by hand for Markdown.

    @Sameeh-Abdul can you please repost the JSON, but this time, post it in a code block? Just hit the button in the editor and it should insert a template code block; it'll start and end with ``` lines and then you can just replace the code text inside it with your JSON. This will completely disable any Markdown processing so what it displays should be identical to the code you paste.

    edit: although really, I don't see anything that suggests that the code in the screenshot wouldn't work correctly, as long as the JSON itself is correct. So I'd still like to see the code at http://0.0.0.0:8000/api-response.json that produces the JSON that you're reading.


  • Discourse touched me in a no-no place

    @brie There's no actual need for that. It's quite clear that when faced with the real input, it's just a JSON document in a string in a JSON document in a string in a JSON document. With the outermost layer providing just what normal HTTP headers provide, of course! The creator of this shitshow needs to be tarred, feathered, and horse-whipped out of town, and the poor person parsing this needs to manually call the JSON deserializer in the right places.

    If only the creator had realised that they can put an object inside an object directly rather than wrapping the serialized version…



  • @dkf In theory, if the original JSON was correct, then yes... it'd just be a matter of deserializing it repeatedly (which, sure, it's ugly, but I've seen the likes of it before). But the problem is, when faced with the "real input" it crashes and burns. So my question is why the real input is wrong.

    If I remove the unescaped quote that I've highlighted:
    0d3a70a6-b869-4cdc-876a-86c9c5f92297-image.png

    then the JSON can be parsed successfully:
    5e3c16a9-8c40-481c-9305-1f97b9cc2cba-image.png

    which makes data.response equal this:
    ["{\"ip_id\":\"202\",\"ip_name\":\"Pr Mark\",\"small_desc\":\"Growth \",\"large_desc\":\"Sample description\",\"join_date\":\"\",\"vid_url\":\"https:\/\/www.thenug.app\/user_videos\/p.mp4\",\"img_url\":\"https:\/\/www.thenug.app\/user_thumbs\/pr_202.jpg\",\"current_status\":\"0\",\"rate_per_hour\":\"1300\",\"currency\":\"\u20b9\"}","{...}"]

    which is, then, valid JSON for this array:
    3ab5ccbd-091b-4371-af10-d49aebe98b52-image.png

    and parsing item 0 from the array yields this object:
    48b7c5ab-175f-4a45-8e2e-064d8abf4828-image.png

    and that, I presume, is what you want, @Sameeh-Abdul.

    So my question now is, where did that "{...}" come from? It looks like maybe OP tried to edit the JSON down by hand to make it smaller, managed to mangle it in the process, and that's been the source of the problems all along.


  • Java Dev

    @Tsaukpaetra said in How do I parse this crap Json?:

    I'm pondering on the order of escape substitution. Does \\ get processed before or after \"? 🤔

    No, it is from left to right. The \ introduces an escape sequence. The character after determines the length and, unless it is a u or U, the value it decodes to. \\" decodes as a backslash followed by end-of-string.

    I have read escaping explanations that suggest the \ only introduces an escape sequence when there is a quote at the end of the backslash list, but all things considering JSON as a standard is pretty sensible and doesn't stoop to madnesses like that.



  • @dkf said in How do I parse this crap Json?:

    If only the creator had realised that they can put an object inside an object directly rather than wrapping the serialized version…

    Sometimes there are multiple layers, and a given layer can only receive text so it is actually getting a serialized version. Sure, the layer could deserialize that text into an object before wrapping it again, but that just adds a point where the code could potentially blow up if it's given invalid JSON.

    So the question really becomes whether you want every layer to potentially blow up if the layer preceding it fucks up, or do you want it to silently feed the bad data through to the final layer and let that layer figure out what to do with the bad data? (Personally, I'd say yeah, if the layer's input is supposed to be JSON, then it should blow up immediately. Errors should be detected and raised as early as reasonably possible.)


  • Discourse touched me in a no-no place

    @brie said in How do I parse this crap Json?:

    Sometimes there are multiple layers, and a given layer can only receive text so it is actually getting a serialized version.

    Even while that's the case, I think you'll find that there's rather less thought about “but what happens if the other end sends me bad data very occasionally?” and much more about “how can I shovel this down the line as fast as possible (in terms of my own effort)?” It's not like this is the first time I've seen this sort of thing either, but I don't recall seeing three layers deep of this before.

    Similar (lack of) thought processes lead to 200 Error and 501 OK.



  • @dkf I've seen 3 layers before, but I don't think I've seen more than that.

    For added :fun:, the outermost layer had another code prefixed onto it. The prefix had to be removed before it could be parsed as JSON, and then there were 3 layers.



  • @brie said in How do I parse this crap Json?:

    "{...}"

    Yes I had only Added to represent similar arrays.
    Could you please guide me to the code to get this work will be very helpful :)



  • @Tsaukpaetra
    Can you please help me with some code?



  • @brie said in How do I parse this crap Json?:

    and that, I presume, is what you want, @Sameeh-Abdul.

    That Exactly what I want!
    Could you please tell me how I can achieve it?



  • @Sameeh-Abdul I think you were on the right track, you just shot yourself in the foot by messing up the JSON that you were trying to parse. If the JSON was valid then you should have no trouble parsing it. Once you decode the initial JSON, you'll have an object whose response property is more JSON, so you'll repeat the process to decode that, which will give you an array, and finally you'll need to iterate that array and decode each item in it.

    What's the URL you're reading... is it just a static file that you saved in a text editor? I think that's where your problem is.

    As far as code goes... I work with Javascript, so I don't really write Apple/Swift. But I don't see anything that looks obviously incorrect in the code that you've written so far. If you fix the JSON and the code is still encountering an error, post again with what you've done so far and maybe it'll be clearer what's wrong with it.


  • Notification Spam Recipient

    @Sameeh-Abdul said in How do I parse this crap Json?:

    @Tsaukpaetra
    Can you please help me with some code?

    ...No? Sorry, I'm not familiar with the language you are writing in.



  • @Sameeh-Abdul said in How do I parse this crap Json?:

    Screenshot 2020-04-27 at 1.58.11 PM.png

    Oh now that I see that you've added your dictionaries, I do see what could potentially be a problem. Your response is defined as [UserData] (an array of UserData), but actually it should be a String... it is a string which contains more JSON, and it needs to be decoded further. This is actually what your error message was saying: it expected to find an array, but it found a string.

    The problem here is that each of these layers in your JSON is actually not an array, or it's not an object... it's a string, which contains even more JSON, which encodes the array or the object... this can be seen from the fact that more and more \ characters are needed to escape everything the deeper you go. The solution is that each layer has to be decoded in a separate step.

    I think you need to

    let response: String?
    

    and then I think you'll be able to decode websiteDesc correctly.

    After decoding websiteDesc, I think you'll need to

    let response = try JSONDecoder().Decode([String].self, from: websiteDesc.response)
    

    and that should give you an array of strings (assuming the syntax works... I don't write Swift at all).

    Finally you'll need to iterate the array of strings and decode each of them... something like this maybe:

    for data in response {
        let userData = try JSONDecoder().Decode(UserData.self, from: data)
    }
    

    Then inside that for-in loop you'll have to do something with userData after it's decoded.



  • Basically it's the difference between this:

    {
        "data":
        [
            "item1",
            "item2"
        ]
    }
    

    and this:

    {
        "data": "[\"item1\", \"item2\"]"
    }
    

    Does that hopefully make sense? The first one, you can decode it all in one step, because it's an object which contains an array of strings. But the second one is an object which contains a string, that encodes an array of strings... that has to be decoded separately.

    You've got 3 layers, so you have to have 3 steps. If you could fix the code that actually generates that JSON, so that it's not encoding multiple levels, that would also simplify the decoding. For instance, if your original JSON was this:

    {
        "status": "success",
        "error": "",
        "response":
        [
            {
                "ip_id": "202",
                "ip_name": "P b ",
                "small_desc": "Growth Hacker",
                "large_desc": "Sample description",
                "join_date": "",
                "vid_url": "https://www.theug.app/user_videos/pr_202.mp4",
                "img_url": "https://www.thnug.app/user_thumbs/s.jpg",
                "current_status": "0",
                "rate_per_hour": "1300",
                "currency": "₹"
            },
            {
                "ip_id": "217",
                "ip_name": "ss dd",
                "small_desc": "Talented",
                "large_desc": "Sample description2 ",
                "join_date": "",
                "vid_url": "https: //www.thug.app/user_videos/d.mp4",
                "img_url": "https: //www.tsnug.app/user_thumbs/d.jpg",
                "current_status": "0",
                "rate_per_hour": "3850",
                "currency": "₹"
            }
        ]
    }
    

    Then I think you could decode it all in one step.



  • @brie
    Makes Sense, However, Unfortunately, I wont be able to change the Json as Ive been given that from the company.
    EDIT:
    Thanks a ton for your work around


  • Fake News

    @Sameeh-Abdul He already did in one of his posts above. Check it again, please.



  • @brie Hello, I actually went though a lot of different solutions to parse my crappy Json File. Most of them did give me a work around, However I ran into many troubles having to deal with making more service calls.
Do you know by any chance if there is a way I use Alamo Fire and Parse this Json along side making HTTP Requests as the library will reduce a lot of the pain but I don’t know how I could go about it



  • @Sameeh-Abdul I am not familiar with Alamo Fire.


Log in to reply