Git Product home page Git Product logo

Comments (5)

mangiucugna avatar mangiucugna commented on August 19, 2024

Hi @michalkkkd I can't reproduce the issue.

When running the provided string like so:

print(repair_json('{"real_content": "Some string: Some other string Some string <a href=\\"https://domain.com\\">Some link</a>"}'))

It returns the correct json

When running it with the wrong escaping:

print(repair_json('{"real_content": "Some string: Some other string Some string <a href=\"https://domain.com\">Some link</a>"}'))

It returns a wrong dictionary but does not return a broken json.

I am running this on latest: 0.9.0

from json_repair.

michalkkkd avatar michalkkkd commented on August 19, 2024

Hi @mangiucugna :)
Github probably messed up when I pasted the snippet.

  1. I was albo to fix it temporarily using this replacement in PHP $data = str_replace('"', ''', $data);
  2. I updated the library (sorry, for not doing it earlier). Now I get this result:
    image
    So yes, json is not broken any more, but is unusable :(

from json_repair.

mangiucugna avatar mangiucugna commented on August 19, 2024

ok so this is an escaping issue and it's not trivial to address in general, I'll think about it but basically if you print that string, you can see that the escaping isn't passed to the function:

>>> print('{"real_content": "Some string: Some other string Some string <a href=\"https://domain.com\">Some link</a>"}')
{"real_content": "Some string: Some other string Some string <a href="https://domain.com">Some link</a>"}

Now, since it doesn't receive the escaping, there is no way for the library to handle the escaping and this is interpreted as a dictionary missing commas. The only (ugly) solution I can come up with is a regex for html tags that will escape those BUT it will break for cases in which the single comma is used like:

repair_json('{"real_content": \'Some string: Some other string Some string <a href="https://domain.com">Some link</a>\'}')

So need to give it more thoughts before I can deploy a fix for this type of breakage, a workaround is to use the following regex:

re.sub(r'(?<!\\\\)"', r'\\"', input_string)

That should fix the string (but again, I can't use that in json_repair because the escaping isn't passed over)

from json_repair.

michalkkkd avatar michalkkkd commented on August 19, 2024

I think, that your are extremely gifted to be able to create such an awesome string parsing library. Though I'm coding for years, low level string manipulation is an abstract concept for me.
Take your time!

from json_repair.

mangiucugna avatar mangiucugna commented on August 19, 2024

ok so I digged into this and the solution is still on your side, I will update the documentation for this problem but you should pass the string as a "raw string". Like:

json_string = r'{"real_content": "Some string: Some other string Some string <a href="https://domain.com\">Some link"}'

This will pass the escaping correctly and fix your issue, however I noticed that if the string was broken (i.e. missing last brace) the library would get confused. So I pushed a fix for that in 0.10.1

from json_repair.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.