0

I am using boost::property_tree to process, edit, then re-output a very large json file (the file comes from a simulator and includes several thousand key-value pairs). Reading in the json, and making the proper modifications works fine, however the boost property_tree's write_json function outputs all values as strings (as described here).

In my case, within my json string, there are 10 or so (repeated) keys that will always have a numeric value (the rest of the key names are strings). So I am wondering if there is a way (likely using boost's regex matching/replacing) to search for a given key, and remove the quotes around its value. e.g.- if I have the string:

... "lots": "of", "key-value": "pairs", "numeric1": "12.3", "numeric2": "45.6", ...

I might do a search for "numeric1", then remove the next two quotes after it. Repeat the same for "numeric2" yielding:

... "lots": "of", "key-value": "pairs", "numeric1": 12.3, "numeric2": 45.6, ...

Note: I might have several hundred "numeric1" keys and several hundred "numeric2" keys that need to be converted to numbers throughout the json string. Thoughts?

MarkD
  • 4,864
  • 5
  • 36
  • 67
  • What about just doing a simple replace all using `"(\d+(?:\.\d*)?|\.\d+)"` with `$1` Stringified its `"\"(\\d+(?:\\.\\d*)?|\\.\\d+)\""` with `"$1"` –  Jul 12 '15 at 18:58
  • 1
    Why not to use proper json library, for example: header only [rapidjson](https://miloyip.github.io/rapidjson/md_doc_tutorial.html)? – doqtor Jul 12 '15 at 19:03
  • @sln: That would work nicely as all of my non-number strings are very un-numeric. Do you know of any example code that does this replacement? I am not very experienced in the way of boost regex, replacing with a piece of the found string (e.g.- $1) and am having trouble finding examples. – MarkD Jul 12 '15 at 19:05
  • @doqtor: we are currently in the air between using json and xml (a decision out of my hands) and it looked like property_tree could easily be switched between the two types of output. – MarkD Jul 12 '15 at 19:07
  • 1
    @MarkD - Sure, I'd give you a good example but this is marked as duplicate. If you open a new question that specifically states you need a regex solution for a plain string. (i.e. drop the json tag). –  Jul 13 '15 at 16:51
  • @sln: no worries. After a bit of trial an error, and with your hint, I was able to figure it out. Thanks for pointing me in the right direction! – MarkD Jul 14 '15 at 14:22

0 Answers0