Deserialize list JSON with no root/association name

1
I'm trying to deserialize the following JSON: {    "Product Name 1": {       "layout": "normal",       "name": "Product Name 1",       "cmc": 5,       "colors": [          "Blue"       ],       "type": "Type 1"    },    "Product name 2": {       "layout": "normal",       "name": "Product Name 2",       "cmc": 5,       "colors": [          "Red",          "Blue"       ],       "type": "Type 2"    } } It's a (simplified) list of thousands of products, but this is the the way that I receive it. I've looked at other JSON deserialize suggestions posted on the forums (for example: https://community.mendix.com/questions/9174/JSon-deserialize-2). But compared to this one I seem to be missing the ("items": {) part. Is this problematic or is there a way around this?
asked
1 answers
0

I believe this is improper JSON. It should be giving you a list of objects. If you can't have it fixed on the other end, I would look at using a regular expression replaceAll to correct it, then using the regular deserializer. You could do this by first simply replacing:

  • The first { with "products": [
  • The last } with ]

Then, using a regular expression, I think you need to replace each instance of:

"Product Name 1": {

with

{
   "name": "Product Name 1",

I think that should set you up with properly formatted JSON. The regular expression is the tricky piece but definitely possible.

EDIT: for the example code you posted, you can achieve the find/replace using these parameters. You can do this either using a text editor like notepadd++ (one time use), or using the Mendix ReplaceAll function:

  • Regular expression to match:

\n "[A-Za-z0-9 ]+":

  • Replacement string:

\n

Note that both the regex string and replacement string should have 3 spaces after the newline character.

answered