1

Quick question on Json list manipulation.

I have json data that has following format

[
 "fruits", 
 "apple", 
 "{\n    \"color\": \"red\",\n    \"harvest\": \"ready\"\n}\n",
 "{\n    \"color\": \"green\",\n    \"harvest\": \"not ready\"\n}\n",
 "veggies",
 "spinach" 
 "{\n    \"color\": \"green\",\n    \"harvest\": \"not ready\"\n}\n",
 "{\n    \"color\": \"light green\",\n    \"harvest\": \"not ready\"\n}\n"
]

I wanted to bring any string that is before "{" (Example: apple and spinach) into into "Key-Value" and add a static "keys" like "name" and "features" to json, finally it would look something like this:

    {
     "fruits":{"name":"apple",
     "features":[
     {
     "color":"red",
     "harvest":"ready"
     }",
     {"color":"green" 
     "harvest":"not ready"
      ]},
     "veggies":{"name":"spinach",
     "features":[
     {
     "color":"green",
     "harvest":"notready"
     },
     {
     "color":"light green",
     "harvest":"not ready"
     ]}
    }
4
  • How do you get this JSON? is this in a json file? or stored in a variable via string? I ask because you have "\n" carriage returns. Commented Nov 16, 2020 at 2:58
  • i get via string Commented Nov 16, 2020 at 2:59
  • is each element on it's own line always? Commented Nov 16, 2020 at 3:19
  • yes, each element on its own line. thanks for checking Commented Nov 16, 2020 at 3:22

1 Answer 1

1

Some suggestions to give you ideas how to proceed:

With the list of strings that you provide (I think there's one comma missing, after "spinach")

strings = [
    "fruits",
    "apple",
    "{\n    \"color\": \"red\",\n    \"harvest\": \"ready\"\n}\n",
    "{\n    \"color\": \"green\",\n    \"harvest\": \"not ready\"\n}\n",
    "veggies",
    "spinach",
    "{\n    \"color\": \"green\",\n    \"harvest\": \"not ready\"\n}\n",
    "{\n    \"color\": \"light green\",\n    \"harvest\": \"not ready\"\n}\n"
]

this

import json

items = []
for string in strings:
    try:
        items.append(json.loads(string))
    except:
        items.append(string)

gives you (print(items)) the following list:

['fruits',
 'apple',
 {'color': 'red', 'harvest': 'ready'},
 {'color': 'green', 'harvest': 'not ready'},
 'veggies',
 'spinach',
 {'color': 'green', 'harvest': 'not ready'},
 {'color': 'light green', 'harvest': 'not ready'}]

Now this then

results = {}
items = iter(items)
while True:
    try:
        category = next(items)
    except StopIteration:
        break
    
    results[category] = {
        'name': next(items),
        'features': [next(items), next(items)]
    }

produces (print(results))

{'fruits': {'features': [{'color': 'red', 'harvest': 'ready'},
                         {'color': 'green', 'harvest': 'not ready'}],
            'name': 'apple'},
 'veggies': {'features': [{'color': 'green', 'harvest': 'not ready'},
                          {'color': 'light green', 'harvest': 'not ready'}],
             'name': 'spinach'}}

So far, so good, but I suspect that your list of strings is actually longer. Most likely it contains other fruits or veggies that have to be included in the result. To do this you need adjust the structure of the program (including the structure of results) accordingly.

Sign up to request clarification or add additional context in comments.

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.